I'm trying to create a new PDF using iText7. First page of PDF contains name of the document. Each topic beings with topic title followed by several paragraphs related to that topic. The following code works great when each topic is only one page long. How do I add destination near TopicTitle (specific paragraph) when topic content could be multi-page and I don't know in advance how long each topic content is.
public ActionResult CreatePdf()
{
byte[] pdfBytes;
using (var stream = new MemoryStream())
using (var wri = new PdfWriter(stream))
using (var pdf = new PdfDocument(wri))
using (var doc = new Document(pdf))
{
List<PdfExplicitDestination> destinations = new List<PdfExplicitDestination>();
// Iterate through your collection (col1) and add content and destinations
for (int i = 0; i < col1.Count; i++)
{
var item = col1[i];
//adding Topic Title as a paragraph here
doc.Add(new Paragraph(TopicTitle));
PdfExplicitDestination destination = PdfExplicitDestination.CreateFitH(i + 1, doc.GetPdfDocument().GetPage(i + 1));
destinations.Add(destination);
//I'll be adding number of paragraphs here making this topic multi-page
doc.Add(new Paragraph("TopicContentPara1"));
...
doc.Add(new Paragraph("TopicContentPara10"));
doc.Add(new AreaBreak());
}
PdfOutline root = pdf.GetOutlines(false);
for (int i = 0; i < col1.Count; i++)
{
var item = col1[i];
root.AddOutline(item.Name).AddDestination(destinations[i]);
}
doc.Close();
doc.Flush();
pdfBytes = stream.ToArray();
}
return File(pdfBytes, "application/pdf", "test.pdf");
}