来自@Mathew Leger's answer:
修剪页面的一个选项是结合使用 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);
条件2:
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.
*/