我正在尝试按照https://developers.itextpdf.com/examples/page-events/clone-page-events-headers-and-footers#2656-variableheader.java中给出的示例创建带有变量标头的PDF文档。但事件没有正确解雇。这是我测试过的代码 -
class Program
{
public static String DEST = "test.pdf";
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
manipulatePdf(DEST);
}
public static List<int> getFactors(int n)
{
List<int> factors = new List<int>();
for (int i = 2; i <= n; i++)
{
while (n % i == 0)
{
factors.Add(i);
n /= i;
}
}
return factors;
}
protected static void manipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
Document doc = new Document(pdfDoc, PageSize.A4, true);
VariableHeaderEventHandler handler = new VariableHeaderEventHandler();
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, handler);
List<int> factors;
for (int i = 2; i < 4; i++)
{
factors = getFactors(i);
if (factors.Count == 1)
{
doc.Add(new Paragraph("This is a prime number!"));
}
foreach (int factor in factors)
{
doc.Add(new Paragraph("Factor: " + factor));
}
handler.setHeader(String.Format("THE FACTORS OF {0}", i));
if (300 != i)
{
doc.Add(new AreaBreak());
}
}
doc.Close();
}
protected class VariableHeaderEventHandler : IEventHandler
{
protected String header;
public void setHeader(String header)
{
this.header = header;
}
public void HandleEvent(Event @event)
{
PdfDocumentEvent documentEvent = (PdfDocumentEvent)@event;
try
{
new PdfCanvas(documentEvent.GetPage())
.BeginText()
.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 12)
.MoveText(450, 806)
.ShowText(header)
.EndText()
.Stroke();
}
catch (IOException e)
{
}
}
}
}
如果我运行此代码,则所有页面都显示标题为&#34; 3&#34;的因素。但他们应该表明&#34;因素2&#34;第一页&#34; 3&#34;的因素第二页和&#34; 4&#34;的因素第三页。我不知道如何解决它。有什么建议吗?
答案 0 :(得分:0)
正如在earlier answer iText 7页面中已经提到的那样,事件会有一些延迟触发 - 通常不会在文档关闭之前,正如您在问题标题中所假设的那样,但页面在处理页面 n 的事件之前, n + 1 可能已经接近完成。
因此,将新页眉设置为事件处理程序的属性是不够的,还必须告诉事件处理程序何时开始使用它。所以...
protected class ImprovedVariableHeaderEventHandler : IEventHandler
{
Dictionary<PdfPage, String> headers = new Dictionary<PdfPage, string>();
protected String header = "";
public void setHeaderFor(String header, PdfPage page)
{
headers[page] = header;
}
public void HandleEvent(Event @event)
{
PdfDocumentEvent documentEvent = (PdfDocumentEvent)@event;
PdfPage page = documentEvent.GetPage();
if (headers.ContainsKey(page))
{
header = headers[page];
headers.Remove(page);
}
new PdfCanvas(page)
.BeginText()
.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 12)
.MoveText(450, 806)
.ShowText(header)
.EndText()
.Stroke();
}
}
这个改进的处理程序可以像这样使用:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, PageSize.A4, true);
ImprovedVariableHeaderEventHandler handler = new ImprovedVariableHeaderEventHandler();
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, handler);
List<int> factors;
for (int i = 2; i < 40; i++)
{
if (2 != i)
{
doc.Add(new AreaBreak());
}
factors = getFactors(i);
if (factors.Count == 1)
{
doc.Add(new Paragraph("This is a prime number!"));
}
foreach (int factor in factors)
{
doc.Add(new Paragraph("Factor: " + factor));
}
handler.setHeaderFor(String.Format("THE FACTORS OF {0}", i), pdfDoc.GetLastPage());
}
doc.Close();
结果是您期望的输出。
我们可能不想为这么少的内容浪费这么多页面。在这种情况下,多个数字的分解应该适合单个页面,我们希望在标题中提到所有这些。这可以使用像这样的替代事件监听器来实现:
protected class ImprovedVariableHeaderEventHandlerAlt : IEventHandler
{
Dictionary<PdfPage, String> headers = new Dictionary<PdfPage, string>();
protected String header = "";
public void addHeaderDetailFor(string header, PdfPage page)
{
if (headers.ContainsKey(page))
headers[page] += ", " + header;
else
headers[page] = header;
}
public void HandleEvent(Event @event)
{
PdfDocumentEvent documentEvent = (PdfDocumentEvent)@event;
PdfPage page = documentEvent.GetPage();
if (headers.ContainsKey(page))
{
header = String.Format("THE FACTORS OF {0}", headers[page]);
headers.Remove(page);
}
new PdfCanvas(page)
.BeginText()
.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 12)
.MoveText(150, 806)
.ShowText(header)
.EndText()
.Stroke();
}
}
使用此代码:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, PageSize.A4, true);
ImprovedVariableHeaderEventHandlerAlt handler = new ImprovedVariableHeaderEventHandlerAlt();
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, handler);
List<int> factors;
for (int i = 2; i < 40; i++)
{
doc.Add(new Paragraph(String.Format("The factors of {0}", i)).SetBold());
handler.addHeaderDetailFor(i.ToString(), pdfDoc.GetLastPage());
factors = getFactors(i);
if (factors.Count == 1)
{
doc.Add(new Paragraph("This is a prime number!"));
}
foreach (int factor in factors)
{
doc.Add(new Paragraph("Factor: " + factor));
}
}
doc.Close();
通过在绘制新标题的节标题后立即通知事件监听器,将在打印此节标题的页面的页眉中提及该数字,而实际的分解可能在下一页上。< / p>