Skip to content

Commit 3b318e1

Browse files
EnraHArneHansenDGEliotJones
authored
add option to strip annotation (UglyToad#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>
1 parent 377eb50 commit 3b318e1

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,31 @@ public void CanFastAddPageAndStripLinkAnnots()
129129
}
130130
}
131131

132+
[Fact]
133+
public void CanFastAddPageAndStripAllAnnots()
134+
{
135+
var first = IntegrationHelpers.GetDocumentPath("outline.pdf");
136+
var contents = File.ReadAllBytes(first);
137+
138+
byte[] results = null;
139+
using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff))
140+
using (var output = new PdfDocumentBuilder())
141+
{
142+
output.AddPage(existing, 1, false);
143+
results = output.Build();
144+
var pg = existing.GetPage(1);
145+
var annots = pg.ExperimentalAccess.GetAnnotations().ToList();
146+
Assert.NotEmpty(annots);
147+
}
148+
149+
using (var rewritten = PdfDocument.Open(results, ParsingOptions.LenientParsingOff))
150+
{
151+
var pg = rewritten.GetPage(1);
152+
var annots = pg.ExperimentalAccess.GetAnnotations().ToList();
153+
Assert.Empty(annots);
154+
}
155+
}
156+
132157
[Fact]
133158
public void CanReadSingleBlankPage()
134159
{

src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ private sealed class PageInfo(DictionaryToken page, IReadOnlyList<DictionaryToke
307307
/// </summary>
308308
/// <param name="document">Source document.</param>
309309
/// <param name="pageNumber">Page to copy.</param>
310+
/// <param name="keepAnnotations">Flag to set whether annotation of page should be kept</param>
310311
/// <returns>A builder for editing the page.</returns>
311-
public PdfPageBuilder AddPage(PdfDocument document, int pageNumber)
312+
public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, bool keepAnnotations = true)
312313
{
313314
return AddPage(document, pageNumber, null);
314315
}
@@ -453,6 +454,11 @@ public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, Func<PdfActi
453454

454455
if (kvp.Key == NameToken.Annots)
455456
{
457+
if (!keepAnnotations)
458+
{
459+
continue;
460+
}
461+
456462
var val = kvp.Value;
457463
if (kvp.Value is IndirectReferenceToken ir)
458464
{

0 commit comments

Comments
 (0)