0
votes

I have multiple copies of a .pdf document that are commented by different users. I would like to merge all these comments into a new pdf "merged".

I wrote this sub inside a class called document with properties "path" and "directory".

Public Sub MergeComments(ByVal pdfDocuments As String())

    Dim oSavePath As String = Directory & "\" & FileName & "_Merged.pdf"
    Dim oPDFdocument As New iText.Kernel.Pdf.PdfDocument(New PdfReader(Path),
                                                         New PdfWriter(New IO.FileStream(oSavePath, IO.FileMode.Create)))

    For Each oFile As String In pdfDocuments
        Dim oSecundairyPDFdocument As New iText.Kernel.Pdf.PdfDocument(New PdfReader(oFile))
        Dim oAnnotations As New PDFannotations
        For i As Integer = 1 To oSecundairyPDFdocument.GetNumberOfPages
            Dim pdfPage As PdfPage = oSecundairyPDFdocument.GetPage(i)
            For Each oAnnotation As Annot.PdfAnnotation In pdfPage.GetAnnotations()


                oPDFdocument.GetPage(i).AddAnnotation(oAnnotation)

            Next
        Next
    Next

    oPDFdocument.Close()

End Sub

This code results in an exception that I am failing to solve.

iText.Kernel.PdfException: 'Pdf indirect object belongs to other PDF document. Copy object to current pdf document.'

What do I need to change in order to perform this task? Or am I completely off with my code block?

2

2 Answers

0
votes

You need to explicitly copy the underlying PDF object to the destination document. After that you will be easily able to add that object to the list of page annotations.

Instead of adding the annotation directly:

oPDFdocument.GetPage(i).AddAnnotation(oAnnotation)

Copy the object to the destination document first, wrap it into PdfAnnotation class with makeAnnotation method and then add it as usual. Code is in Java but you will easily be able to convert it into VB:

PdfObject annotObject = oAnnotation.getPdfObject().copyTo(pdfDocument);
pdfDocument.getPage(i).addAnnotation(PdfAnnotation.makeAnnotation(annotObject));
0
votes

Here is a working Java code, with annotations copied from one document to other using the copyTo method.

PdfReader reader = new PdfReader(new 
RandomAccessSourceFactory().createBestSource(sourceFileName), null);

        PdfDocument document = new PdfDocument(reader);

        PdfReader toMergeReader = new PdfReader(new RandomAccessSourceFactory().createBestSource(targetFileName), null);
        PdfDocument toMergeDocument = new PdfDocument(toMergeReader);

        PdfWriter writer = new PdfWriter(targetFileName + "_MergedVersion.pdf");
        PdfDocument writeDocument = new PdfDocument(writer);

        int pageCount = toMergeDocument.getNumberOfPages();
        for (int i = 1; i <= pageCount; i++) {
            PdfPage page = document.getPage(i);
            writeDocument.addPage(page.copyTo(writeDocument));
            PdfPage pdfPage = toMergeDocument.getPage(i);
            List<PdfAnnotation> pageAnnots = pdfPage.getAnnotations();
            if (pageAnnots != null) {
                for (PdfAnnotation pdfAnnotation : pageAnnots) {
                    PdfObject annotObject = pdfAnnotation.getPdfObject().copyTo(writeDocument);
                    writeDocument.getPage(i).addAnnotation(PdfAnnotation.makeAnnotation(annotObject));
                }
            }
        }
        reader.close();
        toMergeReader.close();
        toMergeDocument.close();
        document.close();
        writeDocument.close();
        writer.close();