/* Antenna House PDF Tool API V8.0 .NET Interface sample program 概要:画像透かし・PDF透かし:透かし画像・PDFをカスタム角度で挿入 Copyright 2025- Antenna House, Inc. */ using PdfTkNet; using System; using System.Xml; namespace AppendWatermarkSetAngle { class Program { static void Main(string[] args) { if (args.Length < 4) { Console.WriteLine("usage: AppendWatermarkSetAngle.exe in-pdf-file out-pdf-file watermark-image watermark-pdf"); return; } try { using (PtlParamInput inputFile = new PtlParamInput(args[0])) using (PtlParamOutput outputFile = new PtlParamOutput(args[1])) using (PtlPDFDocument doc = new PtlPDFDocument()) { //PDFファイルをロードします。 doc.load(inputFile); // 透かしの設定 appendWatermarkImage(doc, args[2]); appendWatermarkPDF(doc, args[3]); // ファイルに保存します。 doc.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 appendWatermarkImage(PtlPDFDocument doc, String pathImage) { // 透かしの設定 using (PtlParamWaterMarkImage watermarkImage = new PtlParamWaterMarkImage()) using (PtlParamInput inputimage = new PtlParamInput(pathImage)) // 画像のパス { // 入力画像ストリームの設定 watermarkImage.setImageStream(inputimage); // 透かしの名前の設定 watermarkImage.setName("透かしの名前"); // 透かしを配置するときの余白の設定 watermarkImage.setMargin(10.0f, 10.0f, 10.0f, 10.0f); // 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */ watermarkImage.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_LEFT); // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */ watermarkImage.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT); // 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */ watermarkImage.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ODD); // 透かしの角度の設定 watermarkImage.setAngle(-15); // 透かしの不透明度の設定 watermarkImage.setOpacity(0.9f); // 透かしをタイリングして配置するかどうかの設定 watermarkImage.setTiling(false); // 透かしの倍率の設定 watermarkImage.setScale(0.8f); // 透かしの設定 doc.appendWaterMark(watermarkImage); } } static void appendWatermarkPDF(PtlPDFDocument doc, String pathPdf) { // 透かしの設定 using (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF()) { // 透かしの名前の設定 watermarkpdf.setName("透かしの名前"); // 透かしの配置の設定 ALIGN_BOTTOM_RIGHT = 9 /* 右下 */ watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_BOTTOM_RIGHT); // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */ watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT); // 透かしを入れるページの範囲の設定 PAGE_RANGE_EVEN = 4 /* 偶数ページ */ watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_EVEN); // 先頭ページに透かしを配置しない設定の範囲の設定 watermarkpdf.setNotInFirst(false); // 最終ページに透かしを配置しない設定の設定 watermarkpdf.setNotInLast(false); // PDF表示時に透かしを表示する指定の設定 watermarkpdf.setDisplayWaterMark(true); // PDF印刷時に透かしを印刷する指定の設定 watermarkpdf.setPrintWaterMark(true); // 透かしの角度の設定 watermarkpdf.setAngle(15); // 透かしの不透明度の設定 watermarkpdf.setOpacity(0.5f); // 透かしをタイリングして配置するかどうかの設定 watermarkpdf.setTiling(false); using (PtlParamInput watermark = new PtlParamInput(pathPdf)) using (PtlPDFDocument doc_watermark = new PtlPDFDocument()) { // 透かしに使用するPDFファイルをロードします。 doc_watermark.load(watermark); // 透かしに使用するページの取得 using (PtlPages pages = doc_watermark.getPages()) using (PtlPage page = pages.get(0)) { // 透かしに使用するPDF文書ページを設定 watermarkpdf.setPage(page); } } // 透かしの倍率の設定 watermarkpdf.setScale(0.5f); // 透かしの設定 doc.appendWaterMark(watermarkpdf); } } } }