来自@Mathew Leger 的回答:
修剪页面的一个选项是将 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);
条件 3:
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.
*/