/* Antenna House PDF Tool API V7.0 .NET Interface sample program 概要:ページ分割 Copyright 2015-2021 Antenna House, Inc. */ using System; using PdfTkNet; namespace DividePage { class Program { static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("usage: DividePage.exe in-pdf-file out-pdf-file 分割方向\n"); Console.WriteLine("分割方向\n0 : ページを上下に分割 1 : ページを左右に分割\n"); return; } String direction = args[2]; switch (direction) { case "0": case "1": break; default: Console.WriteLine("usage: DividePage.exe in-pdf-file out-pdf-file 分割方向\n"); Console.WriteLine("分割方向\n0 : ページを上下に分割 1 : ページを左右に分割\n"); return; } try { using (PtlParamInput inputFile = new PtlParamInput(args[0])) using (PtlParamOutput outputFile = new PtlParamOutput(args[1])) using (PtlPDFDocument doc = new PtlPDFDocument()) using (PtlPDFDocument docNew = new PtlPDFDocument()) using (PtlPages pagesNew = docNew.getPages()) { //PDFファイルをロードします。 doc.load(inputFile); // ページ数の取得 int numPages = doc.getPageCount(); Console.WriteLine("ページ数:" + numPages); using (PtlPages pages = doc.getPages()) { //ページコンテナが空かどうか if (pages.isEmpty()) { Console.WriteLine("ページコンテナが空"); return; } for (int i = 0; i < numPages; i++) { // 読み込んだページの取得 using (PtlPage page = pages.get(i)) using (PtlSize size = page.getSize()) using (PtlRect viewBox = page.getViewBox()) // 読み込んだページの表示矩形取得 { float width = size.getWidth(); float height = size.getHeight(); // ページを分割していくので注釈のLinkアクションを削除しておく removeLinkAction(page); // 読み込んだページを新規PDFに2ページ分追加 pagesNew.append(page, PtlPages.INSERT_OPTION.OPTION_NONE); pagesNew.append(page, PtlPages.INSERT_OPTION.OPTION_NONE); int numPagesNew = pagesNew.getCount(); using (PtlPage pageNew1 = pagesNew.get(numPagesNew - 2)) // 追加されたページを取得 using (PtlPage pageNew2 = pagesNew.get(numPagesNew - 1)) // 追加されたページを取得 using (PtlRect rect1 = new PtlRect(viewBox)) // 追加されたページに設定する表示矩形 using (PtlRect rect2 = new PtlRect(viewBox)) // 追加されたページに設定する表示矩形 { switch (direction) { case "0": { // 半分の高さ float halfHeight = size.getHeight() / 2; // 矩形の上半分 rect1.setBottom(viewBox.getBottom() + halfHeight); // 矩形の下半分 rect2.setTop(viewBox.getTop() - halfHeight); } break; case "1": { // 半分の幅 float halfWidth = size.getWidth() / 2; // 矩形の左半分 rect1.setRight(viewBox.getRight() - halfWidth); // 矩形の右半分 rect2.setLeft(viewBox.getLeft() + halfWidth); } break; } // 追加されたページの表示矩形を設定 pageNew1.setViewBox(rect1); pageNew2.setViewBox(rect2); } } } } // 別のファイルに保存します。 docNew.save(outputFile); } } catch (PtlException pex) { Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP()); pex.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("-- 完了 --"); } } static void removeLinkAction(PtlPage page) { if (!page.hasAnnots()) { return; } PtlAnnots annots = page.getAnnots(); int numAnnots = annots.getCount(); for (int i = 0; i < numAnnots; ++i) { PtlAnnot annot = annots.get(i); switch (annot.getType()) { case PtlAnnot.ANNOT_TYPE.TYPE_LINK: { PtlAnnotLink annotLink = (PtlAnnotLink)annot; PtlAction action = annotLink.getAction(); switch (action.getType()) { case PtlAction.ACTION_TYPE.TYPE_GOTO: annotLink.removeAction(); break; default: break; } } break; default: break; } } } } }