Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my C# project, I want to read a pdf file line by line, and when the line contains a specific string, I want to add a named destination to this line (not to the page).

What I have tried:

I have found the following example, but this is adding destination to outlines. How can I adapt this to specific lines?
C#
static void Main(string[] args)
{
    String InputPdf = @"test.pdf";
    String OutputPdf = "out.pdf";

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(InputPdf), 
                         new PdfWriter(OutputPdf));

    PdfOutline outlines = pdfDoc.GetOutlines(false);
    // first level     
    foreach (var outline in outlines.GetAllChildren())
    {
        // second level
        foreach (var second in outline.GetAllChildren())
        {
            String title = second.GetTitle();
            PdfDestination dest = second.GetDestination();

            pdfDoc.AddNamedDestination(title, dest.GetPdfObject());
        }
    }
    pdfDoc.Close();
}
Posted
Updated 1-Nov-23 10:16am
v3
Comments
Pete O'Hanlon 31-Oct-23 11:40am    
Which library are you using to read the document with?
Member 15994899 31-Oct-23 11:41am    
I am using itext7
M Imran Ansari 2-Nov-23 17:24pm    
which version you are using for itext7?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900