これは比較的簡単に実行できますが、いくつかの方法があります。内部に他のドキュメントがあり、他に何もない新しいドキュメントを作成する場合、最も簡単に使用できるのはおそらくPdfWriter.GetImportedPage(PdfReader, Int)
. これにより、PdfImportedPage
(から継承PdfTemplate
)が得られます。それができたら、 を使用して新しいドキュメントに追加できますPdfWriter.DirectContent.AddTemplate(PdfImportedPage, Matrix)
。
いくつかのオーバーロードがありますAddTemplate()
が、最も簡単なのは (少なくとも私にとっては) System.Drawing.Drawing2D.Matrix
. これを使用すると、「行列」の用語で考える必要なく、簡単にスケーリングおよび変換 (x、y を変更) できます。
以下は、これを示すサンプル コードです。using
iTextSharp 5.4.0 を対象としていますが、ステートメントを削除すると 4.1.6 でもほぼ同じように動作するはずです。最初に、ランダムな背景色を持つ 12 ページのサンプル PDF を作成します。次に、2 番目のドキュメントを作成し、最初の PDF の各ページを 50% に拡大して追加し、古い 4 ページが新しい 1 ページに収まるようにします。詳細については、コードのコメントを参照してください。このコードは、すべてのページが同じサイズであることを前提としています。状況が異なる場合は、さらに計算を実行する必要がある場合があります。
//Test files that we'll be creating
var file1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File1.pdf");
var file2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File2.pdf");
//For test purposes we'll fill the pages with a random background color
var R = new Random();
//Standard PDF creation, nothing special here
using (var fs = new FileStream(file1, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//Create 12 pages with text on each one
for (int i = 1; i <= 12; i++) {
doc.NewPage();
//For test purposes fill the page with a random background color
var cb = writer.DirectContentUnder;
cb.SaveState();
cb.SetColorFill(new BaseColor(R.Next(0, 256), R.Next(0, 256), R.Next(0, 256)));
cb.Rectangle(0, 0, doc.PageSize.Width, doc.PageSize.Height);
cb.Fill();
cb.RestoreState();
//Add some text to the page
doc.Add(new Paragraph("This is page " + i.ToString()));
}
doc.Close();
}
}
}
//Create our combined file
using (var fs = new FileStream(file2, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
//Bind a reader to the file that we created above
using (var reader = new PdfReader(file1)) {
doc.Open();
//Get the number of pages in the original file
int pageCount = reader.NumberOfPages;
//Loop through each page
for (int i = 0; i < pageCount; i++) {
//We're putting four original pages on one new page so add a new page every four pages
if (i % 4 == 0) {
doc.NewPage();
}
//Get a page from the reader (remember that PdfReader pages are one-based)
var imp = writer.GetImportedPage(reader, (i + 1));
//A transform matrix is an easier way of dealing with changing dimension and coordinates on an rectangle
var tm = new System.Drawing.Drawing2D.Matrix();
//Scale the image by half
tm.Scale(0.5f, 0.5f);
//PDF coordinates put 0,0 in the bottom left corner.
if (i % 4 == 0) {
tm.Translate(0, doc.PageSize.Height); //The first item on the page needs to be moved up "one square"
} else if (i % 4 == 1) {
tm.Translate(doc.PageSize.Width, doc.PageSize.Height); //The second needs to be moved up and over
} else if (i % 4 == 2) {
//Nothing needs to be done for the third
} else if (i % 4 == 3) {
tm.Translate(doc.PageSize.Width, 0); //The fourth needs to be moved over
}
//Add our imported page using the matrix that we set above
writer.DirectContent.AddTemplate(imp,tm);
}
doc.Close();
}
}
}
}