gpt4 book ai didi

c# - itextsharp 修剪 pdf 文档的页面

转载 作者:太空狗 更新时间:2023-10-29 17:30:11 26 4
gpt4 key购买 nike

我有一个 pdf 文档,其中包含我正在使用 C# 以编程方式填写的表单字段。根据三种情况,我需要修剪(删除)该文档中的一些页面。

这可能吗?

对于条件 1:我需要保留第 1-4 页,但删除第 5 和第 6 页

对于条件 2:我需要保留第 1-4 页,但删除第 5 页并保留第 6 页

对于条件 3:我需要保留第 1-5 页但删除第 6 页

最佳答案

结合使用 PdfReader.SelectPages() 和 PdfStamper。下面的代码使用 iTextSharp 5.5.1。

public void SelectPages(string inputPdf, string pageSelection, string outputPdf)
{
using (PdfReader reader = new PdfReader(inputPdf))
{
reader.SelectPages(pageSelection);

using (PdfStamper stamper = new PdfStamper(reader, File.Create(outputPdf)))
{
stamper.Close();
}
}
}

然后您使用针对每个条件的正确页面选择来调用此方法。

条件一:

SelectPages(inputPdf, "1-4", outputPdf);

条件二:

SelectPages(inputPdf, "1-4,6", outputPdf);

SelectPages(inputPdf, "1-6,!5", outputPdf);

条件三:

SelectPages(inputPdf, "1-5", outputPdf);

这是来自 iTextSharp 源代码的关于页面选择内容的注释。这是在用于处理页面选择的 SequenceList 类中:

/**
* This class expands a string into a list of numbers. The main use is to select a
* range of pages.
* <p>
* The general systax is:<br>
* [!][o][odd][e][even]start-end
* <p>
* You can have multiple ranges separated by commas ','. The '!' modifier removes the
* range from what is already selected. The range changes are incremental, that is,
* numbers are added or deleted as the range appears. The start or the end, but not both, can be ommited.
*/

关于c# - itextsharp 修剪 pdf 文档的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7246137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com