Add a group of annotations from one pdf to all pages of another PDF and create a new pdf output

I created a reader for the input file and one for the Markup file. I'm not sure if I should loop through the annotations and then add them one by one to the output, or if there is a way to pull all the annotations from the markup file and add them to the input file while keeping their x, z coordinates.

I have the code below and I'm not sure what to do in the comment section. The AddAnnotation method only accepts PdfAnnotation, but I'm not sure how to convert the PdfDictionary to PdfAnnotaiton.

class Program
{
    public static string inputFile = @"E:\pdf-sample.pdf";
    public static string markupFile = @"E:\StampPdf.pdf";
    public static string outputFile = @"E:\pdf.pdf";

    public static PdfReader inputReader = new PdfReader(inputFile);
    public static PdfReader markupReader = new PdfReader(markupFile);

    static void Main(string[] args)
    {
        PdfDocument inputDoc = new PdfDocument(inputReader, new PdfWriter(outputFile));

        PdfDocument markupDoc = new PdfDocument(markupReader);

        int n = inputDoc.GetNumberOfPages();
        for (int i = 1; i <= n; i++)
        {
            PdfPage page = inputDoc.GetPage(i);

            PdfDictionary markupPage = markupDoc.GetFirstPage().GetPdfObject();
            PdfArray annots = markupPage.GetAsArray(PdfName.Annots);

            if(annots != null)
            {
                for(int j=0; j < annots.Size(); j++)
                {
                    PdfDictionary annotItem = annots.GetAsDictionary(i);
                    //******
                    //page.AddAnnotation(?);
                    //******
                }
            }
        }
        inputDoc.Close();
    }
}

      

I tried another option after finding the new GetAnnotations method in iText7. Here the code works fine, but I can't open the O / P file and get the error that the file is corrupted. Also, when I ran inputDoc.Close () instead of the last line below, I got the error "The indirect pdf object belongs to another PDF document. Copy the object to the current pdf document."

        PdfReader ireader = new PdfReader(inputFile);
        PdfDocument inputDoc = new PdfDocument(ireader, new PdfWriter(outputFile));
        PdfReader mreader = new PdfReader(markupFile);
        PdfDocument markupDoc = new PdfDocument(mreader);
        var annots = markupDoc.GetFirstPage().GetAnnotations();
        if (annots != null)
        {
            for (int j = 0; j < annots.Count(); j++)
            {
                inputDoc.GetFirstPage().AddAnnotation(annots[j]);
            }
        }
        ireader.Close();
        mreader.Close();
        markupDoc.Close();
        inputDoc.SetCloseWriter(true);

      

+3


source to share


1 answer


Maybe try this:

    if (annots != null)
    {
        for (int j = 0; j < annots.Size(); j++)
        {
            PdfDictionary annotItem = annots.GetAsDictionary(i);
            PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(annotItem);
            page.AddAnnotation(lineAnnotation);
        }
    }

      

If that doesn't work, here is some documentation (in Java unfortunately)



http://developers.itextpdf.com/examples/actions-and-annotations/clone-creating-and-adding-annotations

If you can post the Pdf with the annotations you want to copy, maybe I can debug and try something else.

+1


source







All Articles