/* Antenna House PDF Tool API V7.0 .NET Interface sample program 概要:ページ割り付け Copyright 2015-2021 Antenna House, Inc. */ using System; using PdfTkNet; namespace AllocatePages { class Program { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("usage: AllocatePages.exe input-pdf output-pdf"); return; } PtlRect[] rect = null; try { float width = 210.0f; // A4横長 float height = 297.0f; // A4縦長 rect = new PtlRect[] { new PtlRect(0.0f, height / 2, width / 2, height), // 上左 new PtlRect(width / 2, height / 2, width, height), // 上右 new PtlRect(0.0f, 0.0f, width / 2, height / 2), // 下左 new PtlRect(width / 2, 0.0f, width, height / 2) }; // 下右 using (PtlParamInput inputFile = new PtlParamInput(args[0])) using (PtlParamOutput outputFile = new PtlParamOutput(args[1])) using (PtlPDFDocument doc = new PtlPDFDocument()) using (PtlPDFDocument docNew = new PtlPDFDocument()) { // PDFファイルをロードします。 doc.load(inputFile); using (PtlPages pagesNew = docNew.getPages()) //新規PDFページコンテナの取得 using (PtlPages pages = doc.getPages()) //入力PDFページコンテナの取得 using (PtlRect rectA4V = new PtlRect(0.0f, 0.0f, width, height)) // A4縦用紙 { int numPages = doc.getPageCount(); int pageNumber = 0; int i = 0; while (i < numPages) { using (PtlPage pageTemp = new PtlPage()) // 割り付けを行うA4縦ページ { // 割り付けを行うA4縦ページの追加 pagesNew.append(pageTemp, PtlPages.INSERT_OPTION.OPTION_NONE); // 追加したページの取得 using (PtlPage pageNew = pagesNew.get(pageNumber)) { pageNew.setViewBox(rectA4V); // 割り付けを行うA4縦ページのコンテント取得 using (PtlContent content = pageNew.getContent()) { // 4ページを1ページに割り付けます。 for (int j = 0; j < 4 && i < numPages; ++j, ++i) { // 割り付けするページ using (PtlPage page = pages.get(i)) { // ページ割り付け content.drawForm(rect[j], PtlContent.ALIGN.ALIGN_CENTER, page); } } } } } ++pageNumber; } } // ファイルに保存します。 docNew.save(outputFile); } } catch (PtlException pex) { Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP()); pex.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (rect != null) { for (int i = 0; i < rect.Length; ++i) { rect[i].Dispose(); } } Console.WriteLine("-- 完了 --"); } } } }