Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to strip annotation #492

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ public void CanFastAddPageAndStripLinkAnnots()
}
}

[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()
{
Expand Down
8 changes: 7 additions & 1 deletion src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,9 @@ private class PageInfo
/// </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);
}
Expand Down Expand Up @@ -422,6 +423,11 @@ public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, Func<PdfActi

if (kvp.Key == NameToken.Annots)
{
if (!keepAnnotations)
{
continue;
}

var val = kvp.Value;
if (kvp.Value is IndirectReferenceToken ir)
{
Expand Down