'Get/Set Annotation from/in existsing PDF using itextSharp

I'm working on a project where i have to read an existing pdf using iTextSharp.PdfReader, then getAnnotations in order to insert them back in a new generated pdf.

My problem is that i managed to get Annotations from the input pdf, but i can't find the annotation details (such as destination page number, Action..), for now, i did found RECT, DEST, SUBTYPE, but the other one are null

Code to get annotation :

for (int i = 1; i <= pagesNbr; i++)
{
    iTextSharp.text.pdf.PdfReader read = new iTextSharp.text.pdf.PdfReader("TEST_mod.pdf");
    iTextSharp.text.pdf.PdfDictionary pageDict = read.GetPageN(i);

    iTextSharp.text.pdf.PdfArray annotArray = pageDict.GetAsArray(iTextSharp.text.pdf.PdfName.ANNOTS);

    for (int j = 0; j < annotArray.Size; ++j)
    {
        iTextSharp.text.pdf.PdfDictionary curAnnot = annotArray.GetAsDict(j);

        foreach (iTextSharp.text.pdf.PdfObject A in annotArray.ArrayList)
        {
            iTextSharp.text.pdf.PdfDictionary AnnotationDictionary = (iTextSharp.text.pdf.PdfDictionary)iTextSharp.text.pdf.PdfReader.GetPdfObject(A);

            sw.WriteLine("\n ANNOTS pour la page n°: "+i+"\n");
            sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.TYPE));
            sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.SUBTYPE));
            sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.RECT));
            sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.BORDER));
            sw.WriteLine(AnnotationDictionary.Get(iTextSharp.text.pdf.PdfName.DEST));
        }

    }
}

My other problem, is how to re insert them in my new generated pdf, after getting them of course, For now im using this code only to test if i can really insert an annoation manually

public void setAnnots(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Rectangle linkLocation,PdfDestination destination,int destPage)
{
    PdfAnnotation link = PdfAnnotation.CreateLink(
            writer,
            linkLocation, 
            PdfAnnotation.HIGHLIGHT_INVERT,
            destPage, destination);
    
    writer.AddAnnotation(link);
}

Please can anyone help me solve this problem, thanks in advance.



Solution 1:[1]

I can offer such a piece of code written in Itext7 java version

public void test() throws IOException {
    try(
            PdfDocument pdfDocument = new PdfDocument(
                    new PdfReader(new File("old.pdf")));

            PdfDocument newpdfDocument = new PdfDocument(new PdfWriter(new File("new.pdf")));

    ) {
        PdfPage page = pdfDocument.getPage(1);
        java.util.List<PdfAnnotation> annotations = page.getAnnotations();
        for (PdfAnnotation annotation : annotations) {
            System.out.println(("ANNOTS pour la page n°: "));
            System.out.println(annotation.getSubtype());
            System.out.println(annotation.getRectangle());
            System.out.println(annotation.getBorder());
            System.out.println(annotation.getPdfObject().get(PdfName.Dest));
            System.out.println(annotation.getPdfObject().get(PdfName.Type));
            System.out.println();
        }
        System.out.println(annotations);

        newpdfDocument.addNewPage();
        PdfAnnotation pdfAnnotation = annotations.get(0);

        PdfDictionary object = new PdfDictionary();
        object.put(PdfName.Subtype, pdfAnnotation.getSubtype());
        object.put(PdfName.Rect, pdfAnnotation.getRectangle());
        object.put(PdfName.Border, pdfAnnotation.getBorder());
        object.put(PdfName.Type, pdfAnnotation.getPdfObject().get(PdfName.Type));
        PdfAnnotation result = PdfAnnotation.makeAnnotation(object);

        PdfPage newPage = newpdfDocument.getPage(1);
        newPage.addAnnotation(result);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zhenya Prudnikov