add option to strip annotation (#492)

* add option to strip annotation

* fix implementation and tests

---------

Co-authored-by: arne.hansen <arne.hansen@digitecgalaxus.ch>
Co-authored-by: Eliot Jones <elioty@hotmail.co.uk>
This commit is contained in:
EnraH 2025-07-20 18:10:15 +02:00 committed by GitHub
parent 377eb507e8
commit 3b318e1944
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View File

@ -129,6 +129,31 @@
}
}
[Fact]
public void CanFastAddPageAndStripAllAnnots()
{
var first = IntegrationHelpers.GetDocumentPath("outline.pdf");
var contents = File.ReadAllBytes(first);
byte[] results = null;
using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff))
using (var output = new PdfDocumentBuilder())
{
output.AddPage(existing, 1, false);
results = output.Build();
var pg = existing.GetPage(1);
var annots = pg.ExperimentalAccess.GetAnnotations().ToList();
Assert.NotEmpty(annots);
}
using (var rewritten = PdfDocument.Open(results, ParsingOptions.LenientParsingOff))
{
var pg = rewritten.GetPage(1);
var annots = pg.ExperimentalAccess.GetAnnotations().ToList();
Assert.Empty(annots);
}
}
[Fact]
public void CanReadSingleBlankPage()
{

View File

@ -307,8 +307,9 @@ namespace UglyToad.PdfPig.Writer
/// </summary>
/// <param name="document">Source document.</param>
/// <param name="pageNumber">Page to copy.</param>
/// <param name="keepAnnotations">Flag to set whether annotation of page should be kept</param>
/// <returns>A builder for editing the page.</returns>
public PdfPageBuilder AddPage(PdfDocument document, int pageNumber)
public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, bool keepAnnotations = true)
{
return AddPage(document, pageNumber, null);
}
@ -453,6 +454,11 @@ namespace UglyToad.PdfPig.Writer
if (kvp.Key == NameToken.Annots)
{
if (!keepAnnotations)
{
continue;
}
var val = kvp.Value;
if (kvp.Value is IndirectReferenceToken ir)
{