/* Antenna House PDF Tool API V8.0 .NET Interface sample program 概要:ページ抽出 別々のPDFとして保存 Copyright 2013-2026 Antenna House, Inc. */ using PdfTkNet; using System; using System.Xml; namespace ExtractDividePage { class ExtractDividePage { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("usage: ExtractDividePage.exe input-pdf out-pdf page-renge"); return; } try { using (PtlParamInput inputFile = new PtlParamInput(args[0])) using (PtlPDFDocument doc = new PtlPDFDocument()) { string outputfile = (string)args[1]; // PDFファイルをロードします。 doc.load(inputFile); // 文書プロパティの取得 using (PtlDocProperty docproperty = doc.getDocProperty()) // 文書情報の取得 using (PtlDocInfo docinfo = docproperty.getDocInfo()) { // 出力ページ範囲 string pageRange = (string)args[2]; string[] Ranges = pageRange.Split(","); int rc = Ranges.Count(); for (int i = 0; i < rc; i++) { using (PtlPDFDocument doc_ext = new PtlPDFDocument()) using (PtlPages pages = doc_ext.getPages()) using (PtlDocProperty docproperty_ext = doc_ext.getDocProperty()) using (PtlDocInfo docinfo_ext = docproperty_ext.getDocInfo()) { // タイトルをコピー docinfo_ext.setTitle(docinfo.getTitle()); // 著者をコピー docinfo_ext.setAuthor(docinfo.getAuthor()); // サブジェクトをコピー docinfo_ext.setSubject(docinfo.getSubject()); // キーワードをコピー docinfo_ext.setKeywords(docinfo.getKeywords()); // クリエータをコピー docinfo_ext.setCreator(docinfo.getCreator()); // プロデューサをコピー docinfo_ext.setProducer(docinfo.getProducer()); // 作成日付をコピー docinfo_ext.setCreationDate(docinfo.getCreationDate()); // 更新日付をコピー docinfo_ext.setModDate(docinfo.getModDate()); // ページ挿入オプション //OPTION_NONE = 0x00000000 オプションはありません。 PtlPages.INSERT_OPTION insertoption = (PtlPages.INSERT_OPTION.OPTION_NONE); string suffix; string tmp = Ranges[i]; if (tmp.Contains("-")) { string[] pg = tmp.Split("-"); int tmpStrt, tmpEnd; if (int.TryParse(pg[0], out tmpStrt) && int.TryParse(pg[1], out tmpEnd)) { // ページの追加(tmpStrtページからtmpEndページ) pages.append(doc, tmpStrt, (tmpEnd - tmpStrt + 1), insertoption); suffix = "_" + tmpStrt + "-" + tmpEnd + ".pdf"; } else { Console.WriteLine("invalid pageRange value."); return; } } else { int tmpStrt; if (int.TryParse(tmp, out tmpStrt)) { // ページの追加(tmpStrtページから1P) pages.append(doc, tmpStrt, 1, insertoption); suffix = "_" + tmpStrt + ".pdf"; } else { Console.WriteLine("invalid pageRange value."); return; } } // 出力ファイル名 PtlParamOutput output = new PtlParamOutput(outputfile.Substring(0, outputfile.Length - 5) + suffix); // ファイルに保存します。 doc_ext.save(output); } } } } } catch (PtlException pex) { Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP()); pex.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("-- 完了 --"); } } } }