Ces*_*uel 5 c# asp.net-mvc pdf-generation itextsharp
我正在使用ASP MVC,我使用iTextSharp在我的应用程序中生成PDF.但现在我有一个问题:我打印列表,当存在多个页面时,我想显示页码(例如:)Page 1 to 4.我找到了一些例子,但我认为它比我需要做的更复杂(如exameple).
编辑: 我发现这个例子2.我可以计算页数,但我不能打印页数.
我做了什么:
public ActionResult downloadListaISCC(DateTime? DataFimFiltro)
{
//Code to generate list to PDF
//My document
Document doc1 = new Document();
doc1.SetPageSize(iTextSharp.text.PageSize.A4);
doc1.SetMargins(0f, 0f, 0f, 0f);
doc1.NewPage();
MemoryStream pdfStream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc1, pdfStream);
//Code to create table
doc1.Add(table); //table list in document
//Follow the example 2 (link)
pdfWriter.CloseStream = true;
doc1.Close();
//E fui seguindo o exemplo do segundo link
string file = "D:/gerarPDFOleotorres/"+ nomeDoc +"";
// add page numbers
Document copyDoc = new Document();
PdfCopy copyPdf = new PdfCopy(copyDoc, new FileStream(file, FileMode.Create));
copyPdf.SetPageSize(PageSize.A4.Rotate());
copyDoc.Open();
// read the initial pdf document
PdfReader reader = new PdfReader(pdfStream.ToArray());
int totalPages = reader.NumberOfPages;
PdfImportedPage copiedPage = null;
iTextSharp.text.pdf.PdfCopy.PageStamp stamper = null;
for (int i = 0; i < totalPages; i++)
{
// get the page and create a stamper for that page
copiedPage = copyPdf.GetImportedPage(reader, (i + 1));
stamper = copyPdf.CreatePageStamp(copiedPage);
// add a page number to the page
ColumnText.ShowTextAligned(stamper.GetUnderContent(), Element.ALIGN_CENTER, new Phrase((i + 1) + "/" + totalPages, fontetexto), 820f, 15, 0);
stamper.AlterContents();
// add the altered page to the new document
copyPdf.AddPage(copiedPage);
}
copyDoc.Close();
reader.Close();
// flush and clear the document from memory
pdfStream.Flush();
pdfStream.Close();
}
Run Code Online (Sandbox Code Playgroud)
Bru*_*gie 11
基本上,您有两个选择:要么一次创建文档,要么两次创建文档.
如果您一次创建文档,则事先不知道Y的值(总页数),因此您需要创建一个PdfTemplate对象作为占位符.这在MovieCountries1示例中进行了演示.
在这个例子中,我们创建了一个TableHeader扩展的类PdfPageEventHelper.我们创建一个实例PdfTemplate类的total的OnOpenDocument()方法,我们使用这个total占位符OnEndPage(),我们添加页眉或页脚的方法,我们填写在总页数OnCloseDocument()的方法.
这种方法的缺点是很难预测所需的尺寸total.优点是您可以一次创建文档(您不需要先在内存中创建文档).
如果您通过两次传递创建文档,则首先创建没有页眉/页脚的文档,然后检查文档以查找它包含的页数.然后您使用PdfStamper将页码添加到每个页面.这在TwoPasses示例中显示.
这些例子来自我的书"iText in Action - Second Edition".您可以从以下URL免费下载第6章:http://manning.com/lowagie2/samplechapter6.pdf
如果您对特定功能有疑问,请参阅[ 官方文档 ] [4].
更新:我不明白为什么你更喜欢看非官方的例子.我给你的例子看起来像这样:
using (PdfStamper stamper = new PdfStamper(reader, ms2)) {
// Loop over the pages and add a header to each page
int n = reader.NumberOfPages;
for (int i = 1; i <= n; i++) {
// Add content
}
}
Run Code Online (Sandbox Code Playgroud)
然而出于某种原因,你用谷歌搜索了一个更复杂的例子(并且你需要的东西太过分了).
只需将// Add content部件更换为:
ColumnText.ShowTextAligned(stamper.GetUnderContent(), Element.ALIGN_CENTER, new Phrase((i + 1) + "/" + totalPages, fontetexto), 297f, 15f, 0);
Run Code Online (Sandbox Code Playgroud)
请注意,我在ShowTextAligned()方法中调整了x值.您正在创建一个A4大小的页面,这意味着您的页面宽度为595个用户单位.如果在位置x = 820处添加页码,则会添加页脚,但它将位于页面的可见区域之外.如果不知道每种方法的参数,请不要复制/粘贴代码.