mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-08-20 08:31:43 +08:00
i merged a pr which broke the build, this updates the build to work
move all arguments to add page to a setting object so it can be extended in future in a non-breaking api change
This commit is contained in:
parent
e636212ec8
commit
efb8c2a803
@ -139,7 +139,10 @@
|
||||
using (var existing = PdfDocument.Open(contents, ParsingOptions.LenientParsingOff))
|
||||
using (var output = new PdfDocumentBuilder())
|
||||
{
|
||||
output.AddPage(existing, 1, false);
|
||||
output.AddPage(existing, 1, new PdfDocumentBuilder.AddPageOptions
|
||||
{
|
||||
KeepAnnotations = false
|
||||
});
|
||||
results = output.Build();
|
||||
var pg = existing.GetPage(1);
|
||||
var annots = pg.ExperimentalAccess.GetAnnotations().ToList();
|
||||
|
||||
@ -307,11 +307,11 @@ 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>
|
||||
/// <param name="options">Control how copying for the page occurs.</param>
|
||||
/// <returns>A builder for editing the page.</returns>
|
||||
public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, bool keepAnnotations = true)
|
||||
public PdfPageBuilder AddPage(PdfDocument document, int pageNumber)
|
||||
{
|
||||
return AddPage(document, pageNumber, null);
|
||||
return AddPage(document, pageNumber, new AddPageOptions());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -319,9 +319,9 @@ namespace UglyToad.PdfPig.Writer
|
||||
/// </summary>
|
||||
/// <param name="document">Source document.</param>
|
||||
/// <param name="pageNumber">Page to copy.</param>
|
||||
/// <param name="copyLink">If set, links are copied based on the result of the delegate.</param>
|
||||
/// <param name="options">Control how copying for the page occurs.</param>
|
||||
/// <returns>A builder for editing the page.</returns>
|
||||
public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, Func<PdfAction, PdfAction?>? copyLink)
|
||||
public PdfPageBuilder AddPage(PdfDocument document, int pageNumber, AddPageOptions options)
|
||||
{
|
||||
if (!existingCopies.TryGetValue(document.Structure.TokenScanner, out var refs))
|
||||
{
|
||||
@ -454,7 +454,7 @@ namespace UglyToad.PdfPig.Writer
|
||||
|
||||
if (kvp.Key == NameToken.Annots)
|
||||
{
|
||||
if (!keepAnnotations)
|
||||
if (!options.KeepAnnotations)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@ -490,7 +490,7 @@ namespace UglyToad.PdfPig.Writer
|
||||
|
||||
if (tk.TryGet(NameToken.Subtype, out var st) && st is NameToken nm && nm == NameToken.Link)
|
||||
{
|
||||
if (copyLink is null)
|
||||
if (options.CopyLinkFunc is null)
|
||||
{
|
||||
// ignore link if don't know how to copy
|
||||
continue;
|
||||
@ -503,7 +503,7 @@ namespace UglyToad.PdfPig.Writer
|
||||
continue;
|
||||
}
|
||||
|
||||
var copiedLink = copyLink(link);
|
||||
var copiedLink = options.CopyLinkFunc(link);
|
||||
if (copiedLink is null)
|
||||
{
|
||||
// ignore if caller wants to skip the link
|
||||
@ -1248,5 +1248,21 @@ namespace UglyToad.PdfPig.Writer
|
||||
|
||||
context.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Options for how a page should be copied to the current builder when using AddPage.
|
||||
/// </summary>
|
||||
public class AddPageOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether to preserve annotation objects in the copied page.
|
||||
/// </summary>
|
||||
public bool KeepAnnotations { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Intercept how actions are copied from the source to destination page.
|
||||
/// </summary>
|
||||
public Func<PdfAction, PdfAction?>? CopyLinkFunc { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,23 +1,17 @@
|
||||
namespace UglyToad.PdfPig.Writer
|
||||
namespace UglyToad.PdfPig.Writer;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Actions;
|
||||
using Outline.Destinations;
|
||||
|
||||
/// <summary>
|
||||
/// Merges PDF documents into each other.
|
||||
/// </summary>
|
||||
public static class PdfMerger
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Filters;
|
||||
using Logging;
|
||||
using System.Linq;
|
||||
using UglyToad.PdfPig.Actions;
|
||||
using UglyToad.PdfPig.Outline.Destinations;
|
||||
|
||||
/// <summary>
|
||||
/// Merges PDF documents into each other.
|
||||
/// </summary>
|
||||
public static class PdfMerger
|
||||
{
|
||||
private static readonly ILog Log = new NoOpLog();
|
||||
|
||||
private static readonly IFilterProvider FilterProvider = DefaultFilterProvider.Instance;
|
||||
|
||||
/// <summary>
|
||||
/// Merge two PDF documents together with the pages from <paramref name="file1"/> followed by <paramref name="file2"/>.
|
||||
/// </summary>
|
||||
@ -186,7 +180,12 @@
|
||||
{
|
||||
for (var i = 1; i <= existing.NumberOfPages; i++)
|
||||
{
|
||||
document.AddPage(existing, i, link => CopyLink(link, n => basePageNumber + n));
|
||||
document.AddPage(existing,
|
||||
i,
|
||||
new PdfDocumentBuilder.AddPageOptions
|
||||
{
|
||||
CopyLinkFunc = link => CopyLink(link, n => basePageNumber + n)
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -199,8 +198,14 @@
|
||||
|
||||
foreach (var i in pages)
|
||||
{
|
||||
document.AddPage(existing, i, link => CopyLink(
|
||||
link, n => pageNumbers.TryGetValue(n, out var pageNumber) ? pageNumber : null));
|
||||
document.AddPage(existing,
|
||||
i,
|
||||
new PdfDocumentBuilder.AddPageOptions
|
||||
{
|
||||
CopyLinkFunc = link => CopyLink(
|
||||
link,
|
||||
n => pageNumbers.TryGetValue(n, out var pageNumber) ? pageNumber : null)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -225,12 +230,11 @@
|
||||
var newDestination = new ExplicitDestination(newPageNumber.Value, link.Destination.Type, link.Destination.Coordinates);
|
||||
|
||||
return action switch {
|
||||
GoToAction goToAction => new GoToAction(newDestination),
|
||||
GoToAction => new GoToAction(newDestination),
|
||||
GoToEAction goToEAction => new GoToEAction(newDestination, goToEAction.FileSpecification),
|
||||
GoToRAction goToRAction => new GoToRAction(newDestination, goToRAction.Filename),
|
||||
_ => action
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user