开发者

Add page in the middle of itextsharp

开发者 https://www.devze.com 2023-01-08 23:01 出处:网络
I\'m creating a PDF using iTextSharp and after adding all my pages, I want to be able to add a table of contents at the beginning.Is there a way to \"rewind\" the PDFWr开发者_运维问答iter?

I'm creating a PDF using iTextSharp and after adding all my pages, I want to be able to add a table of contents at the beginning. Is there a way to "rewind" the PDFWr开发者_运维问答iter?

var d = new Document(PageSize.LETTER);
PdfWriter w = PdfWriter.GetInstance(d, new FileStream("test.pdf", FileMode.Create));
d.Open();
for (var i = 0; i < 200; i++) {
    d.Add(new Paragraph("Hola Senor Page:" + w.CurrentPageNumber.ToString()));
}

w.GoToPage1;
d.Add("Table of Contents here");

d.Close();

is this possible?


Not like that, no. After a writer leaves a given page, everything for that page is written to the output stream. Any page-relative links are already embedded.

There's an example or two of how to build ToC's floating around on the web somewhere, lets see if I can dig one up...

Ah! So while the individual pages are written out as you build them, you can change their order prior to writing everything out.

In the above example, they build the TOC last, and then shuffle it to the front prior to saving the PDF. Learn something new every day.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号