1 year ago

#362912

test-img

cmt1

Using iText and C#, create named destinations at exact same page coordinate as each PDF bookmark

I can iterate through all the bookmarks in my PDF file and create named destinations but positioned at the top of the same page as each bookmark and not at the same coordinates.

The code below works but how do I set the named destination to also be at the exact same page coordinates as the bookmark (there can be a few bookmarks per page):

string file = @"C:\MyDocs\MyDoc1.pdf";
string newFile = @"C:\MyDocs\MyDoc2.pdf";

PdfDocument pdf = new PdfDocument(new PdfReader(file), new PdfWriter(newFile));

// Get the bookmarks
PdfOutline outlines = pdf.GetOutlines(false);
List<PdfOutline> bookmarks = outlines.GetAllChildren().ToList<PdfOutline>();

PdfNameTree destsTree = pdf.GetCatalog().GetNameTree(PdfName.Dests);
IDictionary<String, PdfObject> Tnames = destsTree.GetNames();

foreach (var item in bookmarks)
{
     string title = item.GetTitle();

     int pgn = pdf.GetPageNumber((PdfDictionary)item.GetDestination().GetDestinationPage(Tnames));

     PdfPage pdfPage = pdf.GetPage(pgn);
     iText.Kernel.Geom.Rectangle pageRect = pdfPage.GetPageSize();
     float getLeft = pageRect.GetLeft();
     float getTop = pageRect.GetTop();
     PdfExplicitDestination destObj = PdfExplicitDestination.CreateXYZ(pdfPage, getLeft, getTop, 1);
     pdf.AddNamedDestination(title, destObj.GetPdfObject());
}

pdf.Close();

c#

pdf

itext

itext7

bookmarks

0 Answers

Your Answer

Accepted video resources