Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding a new page using iTextSharp
    primarykey
    data
    text
    <p>I have a class that build the content for my table of contents and that works, fine and dandy. Here's the code:</p> <pre><code>public void Build(string center,IDictionary&lt;string,iTextSharp.text.Image&gt; images) { iTextSharp.text.Image image = null; XPathDocument rapportTekst = new XPathDocument(Application.StartupPath + @"..\..\..\RapportTemplates\RapportTekst.xml"); XPathNavigator nav = rapportTekst.CreateNavigator(); XPathNodeIterator iter; iter = nav.Select("//dokument/brevhoved"); iter.MoveNext(); var logo = iTextSharp.text.Image.GetInstance(iter.Current.GetAttribute("url", "")); iter = nav.Select("//dokument/gem_som"); iter.MoveNext(); string outputPath = iter.Current.GetAttribute("url", "")+center+".pdf"; iter = nav.Select("//dokument/titel"); iter.MoveNext(); this.titel = center; Document document = new Document(PageSize.A4, 30, 30, 100, 30); var outputStream = new FileStream(outputPath, FileMode.Create); var pdfWriter = PdfWriter.GetInstance(document, outputStream); pdfWriter.SetLinearPageMode(); var pageEventHandler = new PageEventHandler(); pageEventHandler.ImageHeader = logo; pdfWriter.PageEvent = pageEventHandler; DateTime timeOfReport = DateTime.Now.AddMonths(-1); pageWidth = document.PageSize.Width - (document.LeftMargin + document.RightMargin); pageHight = document.PageSize.Height - (document.TopMargin + document.BottomMargin); document.Open(); var title = new Paragraph(titel, titleFont); title.Alignment = Element.ALIGN_CENTER; document.Add(title); List&lt;TableOfContentsEntry&gt; _contentsTable = new List&lt;TableOfContentsEntry&gt;(); nav.MoveToRoot(); iter = nav.Select("//dokument/indhold/*"); Chapter chapter = null; int chapterCount = 1; while (iter.MoveNext()) { _contentsTable.Add(new TableOfContentsEntry("Test", pdfWriter.CurrentPageNumber.ToString())); XPathNodeIterator innerIter = iter.Current.SelectChildren(XPathNodeType.All); chapter = new Chapter("test", chapterCount); while(innerIter.MoveNext()) { if (innerIter.Current.Name.ToString().ToLower().Equals("billede")) { image = images[innerIter.Current.GetAttribute("navn", "")]; image.Alignment = Image.ALIGN_CENTER; image.ScaleToFit(pageWidth, pageHight); chapter.Add(image); } if (innerIter.Current.Name.ToString().ToLower().Equals("sektion")) { string line = ""; var afsnit = new Paragraph(); line += (innerIter.Current.GetAttribute("id", "") + " "); innerIter.Current.MoveToFirstChild(); line += innerIter.Current.Value; afsnit.Add(line); innerIter.Current.MoveToNext(); afsnit.Add(innerIter.Current.Value); chapter.Add(afsnit); } } chapterCount++; document.Add(chapter); } document = CreateTableOfContents(document, pdfWriter, _contentsTable); document.Close(); } </code></pre> <p>I'm then calling the method <code>CreateTableOfContents()</code>, and as such it is doing what it is supposed to do. Here's the code for the method:</p> <pre><code>public Document CreateTableOfContents(Document _doc, PdfWriter _pdfWriter, List&lt;TableOfContentsEntry&gt; _contentsTable) { _doc.NewPage(); _doc.Add(new Paragraph("Table of Contents", FontFactory.GetFont("Arial", 18, Font.BOLD))); _doc.Add(new Chunk(Environment.NewLine)); PdfPTable _pdfContentsTable = new PdfPTable(2); foreach (TableOfContentsEntry content in _contentsTable) { PdfPCell nameCell = new PdfPCell(_pdfContentsTable); nameCell.Border = Rectangle.NO_BORDER; nameCell.Padding = 6f; nameCell.Phrase = new Phrase(content.Title); _pdfContentsTable.AddCell(nameCell); PdfPCell pageCell = new PdfPCell(_pdfContentsTable); pageCell.Border = Rectangle.NO_BORDER; pageCell.Padding = 6f; pageCell.Phrase = new Phrase(content.Page); _pdfContentsTable.AddCell(pageCell); } _doc.Add(_pdfContentsTable); _doc.Add(new Chunk(Environment.NewLine)); /** Reorder pages so that TOC will will be the second page in the doc * right after the title page**/ int toc = _pdfWriter.PageNumber - 1; int total = _pdfWriter.ReorderPages(null); int[] order = new int[total]; for (int i = 0; i &lt; total; i++) { if (i == 0) { order[i] = 1; } else if (i == 1) { order[i] = toc; } else { order[i] = i; } } _pdfWriter.ReorderPages(order); return _doc; } </code></pre> <p>The problem is however. I want to insert a page break before the table of contents, for the sake of reordering the pages, so that the table of contents is the first page, naturally. But the output of the pdf-file is not right.</p> <p>Here's a picture of what it looks like: <img src="https://i.stack.imgur.com/PUARt.jpg" alt="Picture"></p> <p>It seems like the <code>_doc.NewPage()</code> in the <code>CreateTableOfContents()</code> method does not execute correctly. Meaning that the image and the table of contents is still on the same page when the method starts the reordering of pages.</p> <p>EDIT: To clarify the above, the <code>_doc.NewPage()</code> gets executed, but the blank page is added after the picture and the table of contents.</p> <p>I've read a couple of places that this could be because one is trying to insert a new page after an already blank page. But this is not the case.</p> <p>I'll just link to the pdf files aswell, to better illustrate the problem.</p> <p>The pdf with table of contents: <a href="http://freepdfhosting.com/326fe4967c.pdf" rel="nofollow noreferrer">with table of contents</a></p> <p>The pdf without table of contents: <a href="http://freepdfhosting.com/e4d8601349.pdf" rel="nofollow noreferrer">without table of contents</a></p> <p>Thank you in advance for your help :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload