Skip to content

PdfDocumentBuilder creates broken annotation highlights when copying pages from source PDFs #1000

@cremor

Description

@cremor

I'm using PdfDocumentBuilder.AddPage to merge multiple PDF files. When the source PDF contains annotations that highlight regions then the created (merged) PDF has some problems with those annotations. The problems differ based on the PDF reader application:
With Acrobat Reader: The annotations change their color and can't be removed any more.
With Firefox: The annotations don't show a tooltip any more.

But this problem only happens if at least a page is inserted before the copied page. So maybe there is a problem with page numbers/references in the created file?

If you don't have any time to look at this right now, could you then please point me to the right place where I could start looking to fix this myself?

Sample input file: Input.pdf

Sample code:

string inputFile = @"C:\Data\Input.pdf";
string outputFile = @"C:\Data\Output.pdf";

using var targetStream = File.Open(outputFile, FileMode.Create, FileAccess.Write);
using var outputDocument = new PdfDocumentBuilder(targetStream);
using var inputDocument = PdfDocument.Open(inputFile);

// This line is important to reproduce the problem!
outputDocument.AddPage(PageSize.A4);

for (int i = 1; i <= inputDocument.NumberOfPages; i++)
{
    outputDocument.AddPage(inputDocument, i);
}

I've tested the following versions of PdfPig, all are affected:

  • 0.1.8
  • 0.1.9
  • 0.1.10
  • 0.1.11-alpha-20250711-b11f9

Expected output:
The annotations should show tooltips in Firefox.
It should be possible to delete the annotations in Acrobat Reader (via the delete icon that appears after clicking on the annotation Image)
The annotation colors should look like this:
Image

Actual output:
The annotations don't show a tooltip in Firefox.
The annotations can't be deleted in Acrobat Reader.
The annotation colors change shortly after scrolling and look like this in Acrobat Reader:
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    document-editingRelated to creating or editing/modifying documents

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions