Y问题的第X页 [英] Page X of Y issue
问题描述
我尝试了3种不同的方式来显示页码, OnCloseDocument
内容没有显示在页面中,但都没有显示。
I tried 3 different ways of displaying Page numbers, OnCloseDocument
content is not displaying in the page, none of them worked.
我的意图显示页码,如下所示
My Intention is displaying Page numbers like this
1 of 10 zh
2 0f 10
..............
............
$ b每页10美元10美元b $ b每小时10美元
我知道如何展示
1
2
3 zh
4 zh
....
10
但不知道如何显示总页数
我正在使用 OnCloseDocument 来显示页数计数,但其中的内容是
未显示。
I`m using OnCloseDocument to display No.of pages count,but the content in it is not displaying.
public class MyPdfPageEventHelpPageNo : iTextSharp.text.pdf.PdfPageEventHelper
{
protected PdfTemplate total;
protected BaseFont helv;
private bool settingFont = false;
public override void OnOpenDocument(PdfWriter writer, Document document)
{
template= writer.DirectContent.CreateTemplate(100, 100);
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
//See below
}
1ST WAY:
public override void OnCloseDocument(PdfWriter writer, Document document)
{
//I create a table with one column like below.
PdfPTable pageNumber2 = new PdfPTable(1);
pageNumber2.TotalWidth = 50;
pageNumber2.HorizontalAlignment = Element.ALIGN_RIGHT;
pageNumber2.AddCell(BuildTable2RightCells("Page " + writer.PageNumber));
pageNumber.AddCell(BuildTable2LeftCells(writer.PageCount));
pageNumber2.WriteSelectedRows(0, -1, 500,
(document.PageSize.GetBottom(140)), cb);
}
2ND WAY:
public override void OnCloseDocument(PdfWriter writer, Document document)
{
ColumnText.ShowTextAligned(template,Element.ALIGN_CENTER,new
Phrase(writer.PageNumber.ToString()), 500, 140, 0);
}
3RD WAY:
public override void OnCloseDocument(PdfWriter writer, Document document)
{
template.BeginText();
template.SetFontAndSize(bf, 8);
template.SetTextMatrix(500, 140);
template.ShowText(Convert.ToString((writer.PageNumber - 1)));
template.EndText();
}
我做错了什么?
推荐答案
你的第二种方式可能是最简单的方法。下面是一个非常非常精简但正在工作的版本:
Your second way is probably the simplest way. Below is a very, very slimmed down but working version:
public class MyPdfPageEventHelpPageNo : iTextSharp.text.pdf.PdfPageEventHelper {
public override void OnEndPage(PdfWriter writer, Document document) {
ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase(writer.PageNumber.ToString()), 500, 140, 0);
}
}
使用它:
//Create a file on our desktop
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "OnCloseTest.pdf");
//Standard PDF creation, adjust as needed
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document doc = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
//Find our custom event handler
writer.PageEvent = new MyPdfPageEventHelpPageNo();
doc.Open();
//Add text the first page
doc.Add(new Paragraph("Test"));
//Add a new page with more text
doc.NewPage();
doc.Add(new Paragraph("Another Test"));
doc.Close();
}
}
}
编辑
很抱歉,我认为您在事件的基本设置方面遇到了问题,我的错误。
我只看到了两种方法来做你想要做的事情,要么做两次通过,要么使用 PdfTemplate语法,根据我的知识呈现图像。
I've only seen two ways to do what you are trying to do, either do two passes or use the PdfTemplate syntax which renders an image as far as I know.
我建议只运行两次传递,第一次只是为了创建您的PDF和第二个添加您的页码。您可以将第一次传递到 MemoryStream
,这样就不必在需要时两次击中磁盘。
I'd recommend just running two passes, the first just to create your PDF and the second to add your page numbers. You can run your first pass to a MemoryStream
so you don't have to hit the disk twice if you want.
PdfReader reader = new PdfReader(outputFile);
using (FileStream fs = new FileStream(secondFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (PdfStamper stamper = new PdfStamper(reader, fs)) {
int PageCount = reader.NumberOfPages;
for (int i = 1; i <= PageCount; i++) {
ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_CENTER, new Phrase(String.Format("Page {0} of {1}", i, PageCount)), 500, 140, 0);
}
}
}
这篇关于Y问题的第X页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!