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 2 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
197 changes: 111 additions & 86 deletions src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,123 +1,123 @@
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))
{
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))
{
// 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))
Expand All @@ -126,7 +126,32 @@ 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]
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]
Expand Down Expand Up @@ -1133,4 +1158,4 @@ private static void WriteFile(string name, byte[] bytes, string extension = "pdf
}
}
}
}
}
9 changes: 8 additions & 1 deletion src/UglyToad.PdfPig/Writer/PdfDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,15 @@ internal class PageInfo
new ConditionalWeakTable<IPdfTokenScanner, Dictionary<IndirectReference, IndirectReferenceToken>>();
private readonly ConditionalWeakTable<PdfDocument, Dictionary<int, PageInfo>> existingTrees =
new ConditionalWeakTable<PdfDocument, Dictionary<int, PageInfo>>();

/// <summary>
/// Add a new page with the specified size, this page will be included in the output when <see cref="Build"/> is called.
/// </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)
{
if (!existingCopies.TryGetValue(document.Structure.TokenScanner, out var refs))
{
Expand Down Expand Up @@ -397,6 +399,11 @@ public PdfPageBuilder AddPage(PdfDocument document, int pageNumber)

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

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