@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();
}
}
}
次に、条件ごとに正しいページ選択でこのメソッドを呼び出すだけです。
条件 1:
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.
*/