From 6b1efd37f79297dbcc144e2b4b40fb0b8446aff7 Mon Sep 17 00:00:00 2001 From: "arne.hansen" Date: Thu, 15 Sep 2022 15:55:26 +0200 Subject: [PATCH 1/2] add option to strip annotation --- .../Writer/PdfDocumentBuilderTests.cs | 200 ++++++++++-------- .../Writer/PdfDocumentBuilder.cs | 6 +- 2 files changed, 118 insertions(+), 88 deletions(-) diff --git a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs index 84a0f02f2..ee634779c 100644 --- a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs +++ b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs @@ -1,73 +1,73 @@ -namespace UglyToad.PdfPig.Tests.Writer -{ +namespace UglyToad.PdfPig.Tests.Writer +{ using System.IO; - using System.Linq; - using Content; - using Integration; - using PdfPig.Core; - using PdfPig.Fonts.Standard14Fonts; - using PdfPig.Tokens; - using PdfPig.Writer; - using Tests.Fonts.TrueType; + using System.Linq; + using Content; + using Integration; + using PdfPig.Core; + using PdfPig.Fonts.Standard14Fonts; + using PdfPig.Tokens; + using PdfPig.Writer; + using Tests.Fonts.TrueType; using Xunit; - public class PdfDocumentBuilderTests - { - [Fact] - public void CanWriteSingleBlankPage() - { - var result = CreateSingleBlankPage(); - - WriteFile(nameof(CanWriteSinglePageHelloWorld), result); - - Assert.NotEmpty(result); - - var str = OtherEncodings.BytesAsLatin1String(result); - Assert.StartsWith("%PDF", str); - Assert.EndsWith("%%EOF", str); - } - - [Fact] - public void CanCreateSingleCustomPageSize() - { - var builder = new PdfDocumentBuilder(); - - var page = builder.AddPage(120, 250); - - var font = builder.AddStandard14Font(Standard14Font.Helvetica); - - page.AddText("Small page.", 12, new PdfPoint(25, 200), font); - - var bytes = builder.Build(); - - WriteFile(nameof(CanCreateSingleCustomPageSize), bytes); - - using (var document = PdfDocument.Open(bytes, ParsingOptions.LenientParsingOff)) - { - Assert.Equal(1, document.NumberOfPages); - - var page1 = document.GetPage(1); - - Assert.Equal(120, page1.Width); - Assert.Equal(250, page1.Height); - - Assert.Equal("Small page.", page1.Text); - } + public class PdfDocumentBuilderTests + { + [Fact] + public void CanWriteSingleBlankPage() + { + var result = CreateSingleBlankPage(); + + WriteFile(nameof(CanWriteSinglePageHelloWorld), result); + + Assert.NotEmpty(result); + + var str = OtherEncodings.BytesAsLatin1String(result); + Assert.StartsWith("%PDF", str); + Assert.EndsWith("%%EOF", str); + } + + [Fact] + public void CanCreateSingleCustomPageSize() + { + var builder = new PdfDocumentBuilder(); + + var page = builder.AddPage(120, 250); + + var font = builder.AddStandard14Font(Standard14Font.Helvetica); + + page.AddText("Small page.", 12, new PdfPoint(25, 200), font); + + var bytes = builder.Build(); + + WriteFile(nameof(CanCreateSingleCustomPageSize), bytes); + + using (var document = PdfDocument.Open(bytes, ParsingOptions.LenientParsingOff)) + { + Assert.Equal(1, document.NumberOfPages); + + var page1 = document.GetPage(1); + + Assert.Equal(120, page1.Width); + Assert.Equal(250, page1.Height); + + Assert.Equal("Small page.", page1.Text); + } } - [Fact] - public void CanFastAddPageAndInheritProps() - { - var first = IntegrationHelpers.GetDocumentPath("inherited_mediabox.pdf"); - var contents = File.ReadAllBytes(first); - + [Fact] + public void CanFastAddPageAndInheritProps() + { + var first = IntegrationHelpers.GetDocumentPath("inherited_mediabox.pdf"); + var contents = File.ReadAllBytes(first); + - byte[] results = null; - using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff)) - using (var output = new PdfDocumentBuilder()) + byte[] results = null; + using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff)) + using (var output = new PdfDocumentBuilder()) { output.AddPage(existing, 1); - results = output.Build(); + results = output.Build(); } using (var rewritted = PdfDocument.Open(results, ParsingOptions.LenientParsingOff)) @@ -75,22 +75,22 @@ public void CanFastAddPageAndInheritProps() var pg = rewritted.GetPage(1); Assert.Equal(200, pg.MediaBox.Bounds.Width); Assert.Equal(100, pg.MediaBox.Bounds.Height); - } + } } - [Fact] - public void CanFastAddPageWithStreamSubtype() - { - var first = IntegrationHelpers.GetDocumentPath("steam_in_page_dict.pdf"); - var contents = File.ReadAllBytes(first); - + [Fact] + public void CanFastAddPageWithStreamSubtype() + { + var first = IntegrationHelpers.GetDocumentPath("steam_in_page_dict.pdf"); + var contents = File.ReadAllBytes(first); + - byte[] results = null; - using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff)) - using (var output = new PdfDocumentBuilder()) + byte[] results = null; + using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff)) + using (var output = new PdfDocumentBuilder()) { output.AddPage(existing, 1); - results = output.Build(); + results = output.Build(); } using (var rewritted = PdfDocument.Open(results, ParsingOptions.LenientParsingOff)) @@ -98,26 +98,54 @@ public void CanFastAddPageWithStreamSubtype() // really just checking for no exception... var pg = rewritted.GetPage(1); Assert.NotNull(pg.Content); - } + } } - [Fact] - public void CanFastAddPageAndStripLinkAnnots() - { - var first = IntegrationHelpers.GetDocumentPath("outline.pdf"); - var contents = File.ReadAllBytes(first); - + [Fact] + public void CanFastAddPageAndStripLinkAnnots() + { + var first = IntegrationHelpers.GetDocumentPath("outline.pdf"); + var contents = File.ReadAllBytes(first); + var annotCount = 0; - byte[] results = null; - using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff)) - using (var output = new PdfDocumentBuilder()) + byte[] results = null; + using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff)) + using (var output = new PdfDocumentBuilder()) { output.AddPage(existing, 1); results = output.Build(); var pg = existing.GetPage(1); var annots = pg.ExperimentalAccess.GetAnnotations().ToList(); annotCount = annots.Count; - Assert.Contains(annots, x => x.Type == Annotations.AnnotationType.Link); + Assert.Contains(annots, x => x.Type == Annotations.AnnotationType.Link); + } + + using (var rewritten = PdfDocument.Open(results, ParsingOptions.LenientParsingOff)) + { + var pg = rewritten.GetPage(1); + var annots = pg.ExperimentalAccess.GetAnnotations().ToList(); + Assert.Equal(annotCount - 1, annots.Count); + Assert.DoesNotContain(annots, x => x.Type == Annotations.AnnotationType.Link); + } + } + + [Fact] + public void CanFastAddPageAndStripAllAnnots() + { + var first = IntegrationHelpers.GetDocumentPath("outline.pdf"); + var contents = File.ReadAllBytes(first); + + var annotCount = 0; + 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(); + annotCount = annots.Count; + Assert.Contains(annots, x => x.Type == Annotations.AnnotationType.Link); } using (var rewritten = PdfDocument.Open(results, ParsingOptions.LenientParsingOff)) @@ -126,7 +154,7 @@ public void CanFastAddPageAndStripLinkAnnots() var annots = pg.ExperimentalAccess.GetAnnotations().ToList(); Assert.Equal(annotCount - 1, annots.Count); Assert.DoesNotContain(annots, x => x.Type == Annotations.AnnotationType.Link); - } + } } [Fact] @@ -1133,4 +1161,4 @@ private static void WriteFile(string name, byte[] bytes, string extension = "pdf } } } -} +} diff --git a/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs index 039223bfd..c76ebdbd1 100644 --- a/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs @@ -290,13 +290,15 @@ internal class PageInfo new ConditionalWeakTable>(); private readonly ConditionalWeakTable> existingTrees = new ConditionalWeakTable>(); + /// /// Add a new page with the specified size, this page will be included in the output when is called. /// /// Source document. /// Page to copy. + /// Flag to set whether annotation of page should be kept /// A builder for editing the page. - public PdfPageBuilder AddPage(PdfDocument document, int pageNumber) + public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, bool keepAnnotations = true) { if (!existingCopies.TryGetValue(document.Structure.TokenScanner, out var refs)) { @@ -395,7 +397,7 @@ public PdfPageBuilder AddPage(PdfDocument document, int pageNumber) continue; } - if (kvp.Key == NameToken.Annots) + if (kvp.Key == NameToken.Annots && keepAnnotations) { var val = kvp.Value; if (kvp.Value is IndirectReferenceToken ir) From 46a23a63696e3c156117d309724add5794a3811f Mon Sep 17 00:00:00 2001 From: "arne.hansen" Date: Wed, 19 Oct 2022 17:50:04 +0200 Subject: [PATCH 2/2] fix implementation and tests --- .../Writer/PdfDocumentBuilderTests.cs | 7 ++----- src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs | 7 ++++++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs index ee634779c..d78526502 100644 --- a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs +++ b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs @@ -135,7 +135,6 @@ public void CanFastAddPageAndStripAllAnnots() var first = IntegrationHelpers.GetDocumentPath("outline.pdf"); var contents = File.ReadAllBytes(first); - var annotCount = 0; byte[] results = null; using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff)) using (var output = new PdfDocumentBuilder()) @@ -144,16 +143,14 @@ public void CanFastAddPageAndStripAllAnnots() results = output.Build(); var pg = existing.GetPage(1); var annots = pg.ExperimentalAccess.GetAnnotations().ToList(); - annotCount = annots.Count; - Assert.Contains(annots, x => x.Type == Annotations.AnnotationType.Link); + Assert.NotEmpty(annots); } using (var rewritten = PdfDocument.Open(results, ParsingOptions.LenientParsingOff)) { var pg = rewritten.GetPage(1); var annots = pg.ExperimentalAccess.GetAnnotations().ToList(); - Assert.Equal(annotCount - 1, annots.Count); - Assert.DoesNotContain(annots, x => x.Type == Annotations.AnnotationType.Link); + Assert.Empty(annots); } } diff --git a/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs index c76ebdbd1..84621f731 100644 --- a/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs @@ -397,8 +397,13 @@ public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, bool keepAnn continue; } - if (kvp.Key == NameToken.Annots && keepAnnotations) + if (kvp.Key == NameToken.Annots) { + if (!keepAnnotations) + { + continue; + } + var val = kvp.Value; if (kvp.Value is IndirectReferenceToken ir) {