Hello
I can't adding the page numbers on the header on my pdf document using iText7 and C#.
This is the tutorial https://kb.itextpdf.com/home/it7kb/examples/page-numbers-and-pdf-a
Error
iText.Kernel.PdfException
HResult=0x80131500
Message=Cannot draw elements on already flushed pages.
itext.layout
in iText.Layout.Renderer.DocumentRenderer.FlushSingleRenderer(IRenderer resultRenderer)
in iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer)
in iText.Layout.RootElement`1.CreateAndAddRendererSubTree(IElement element)
in iText.Layout.RootElement`1.Add(IBlockElement element)
in iText.Layout.Document.Add(IBlockElement element)
in iText.Layout.RootElement`1.ShowTextAligned(Paragraph p, Single x, Single y, Int32 pageNumber, Nullable`1 textAlign, Nullable`1 vertAlign, Single radAngle)
in _Default.TableHeaderEventHandler.HandleEvent(Event currentEvent) in C:\Users\admin\source\repos\newpdf2\newpdf2\Default.aspx.cs: riga 103
in iText.Kernel.Events.EventDispatcher.DispatchEvent(Event event, Boolean delayed)
in iText.Kernel.Events.EventDispatcher.DispatchEvent(Event event)
in iText.Kernel.Pdf.PdfDocument.DispatchEvent(Event event)
in iText.Kernel.Pdf.PdfPage.Flush(Boolean flushResourcesContentStreams)
in iText.Kernel.Pdf.PdfPage.Flush()
in iText.Layout.Renderer.DocumentRenderer.MoveToNextPage()
in iText.Layout.Renderer.DocumentRenderer.UpdateCurrentArea(LayoutResult overflowResult)
in iText.Layout.Renderer.RootRenderer.UpdateCurrentAndInitialArea(LayoutResult overflowResult)
in iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer)
in iText.Layout.Document.Add(AreaBreak areaBreak)
in _Default.btn_Click(Object sender, EventArgs e) in C:\Users\admin\source\repos\newpdf2\newpdf2\Default.aspx.cs: riga 940
The error is on these lines
int n = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= n; i++)
{
doc.ShowTextAligned(new Paragraph(String .Format("pagina " + i + " di " + n))
.SetFontSize(8)
.SetMarginLeft(20), 559, 806, i, TextAlignment.RIGHT, VerticalAlignment.MIDDLE, 0);
}
//and on
document.Add(new AreaBreak());
How to do resolve this?
My code as follow.
private class TableHeaderEventHandler : IEventHandler
{
private Table table;
private float tableHeight;
private Document doc;
public TableHeaderEventHandler(Document doc)
{
this.doc = doc;
InitTable();
TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();
renderer.SetParent(new DocumentRenderer(doc));
LayoutResult result = renderer.Layout(new LayoutContext(new LayoutArea(0, PageSize.A4)));
tableHeight = result.GetOccupiedArea().GetBBox().GetHeight();
}
public void HandleEvent(Event currentEvent)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
int pageNum = docEvent.GetDocument().GetPageNumber(page);
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
PageSize pageSize = pdfDoc.GetDefaultPageSize();
float coordX = pageSize.GetX() + doc.GetLeftMargin();
float coordY = pageSize.GetTop() - doc.GetTopMargin();
float width = pageSize.GetWidth() - doc.GetRightMargin() - doc.GetLeftMargin();
float height = GetTableHeight();
Rectangle rect = new Rectangle(coordX, coordY, width, height);
new Canvas(canvas, rect)
.Add(table)
.Close();
int n = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= n; i++)
{
doc.ShowTextAligned(new Paragraph(String
.Format("page " + i + " of " + n))
.SetFontSize(8)
.SetMarginLeft(20),
559, 806, i, TextAlignment.RIGHT,
VerticalAlignment.MIDDLE, 0);
}
}
public float GetTableHeight()
{
return tableHeight;
}
private void InitTable()
{
Paragraph newline = new Paragraph(new Text("\n"));
table = new Table(1).UseAllAvailableWidth();
Image img = new Image(ImageDataFactory
.Create(@"C:\\Users\\admin\\source\\repos\\newpdf2\\newpdf2\\img\\logo.png"))
.SetTextAlignment(TextAlignment.CENTER);
img.ScaleToFit(150, 150);
table.AddCell(img).SetBorder(Border.NO_BORDER);
}
}