/* Antenna House PDF Tool API V7.0 .NET Interface sample program 概要:画像透かしの挿入 Copyright 2013-2021 Antenna House, Inc. */ using System; using PdfTkNet; namespace AppendImageWatermark { class Program { static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("usage: AppendImageWatermark.exe input-pdf output-pdf watermark-image"); 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[2]); // ファイルに保存します。 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.setOpacity(0.9f); // 透かしをタイリングして配置するかどうかの設定 watermarkImage.setTiling(false); // 透かしの倍率の設定 watermarkImage.setScale(0.8f); // 透かしの設定 doc.appendWaterMark(watermarkImage); } } static void appendWatermarkPDF(PtlPDFDocument doc, String pathImage) { // 透かしの設定 using (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF()) { // 透かしに使用する画像ページを設定 using (PtlParamImagePage imagepage = new PtlParamImagePage()) using (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage()) // 画像の描画に使うパラメータクラス using (PtlParamInput inputimage = new PtlParamInput(pathImage)) // 画像のパス using (PtlPDFDocument doc_img = new PtlPDFDocument()) // 画像から作成したPDF using (PtlPages pages = doc_img.getPages()) //ページコンテナの取得 { // 用紙タイプの設定 PAPER_IMAGE_SIZE /* 画像サイズに合わせる */ imagepage.setPaperType(PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE); // 入力画像ストリームの設定 paramDrawImage.setImageStream(inputimage); // ページに挿入する画像パラメータの設定。 imagepage.setImage(paramDrawImage); // 画像ページの追加 pages.append(imagepage); // 先頭ページを取得 using (PtlPage page = pages.get(0)) { // 透かしに使用するPDF文書ページを設定 watermarkpdf.setPage(page); } } // 透かしの名前の設定 watermarkpdf.setName("透かしの名前"); // 透かしを配置するときの余白の設定 watermarkpdf.setMargin(10.0f, 10.0f, 10.0f, 10.0f); // 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */ watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_LEFT); // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */ watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT); // 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */ watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ODD); // 透かしの不透明度の設定 watermarkpdf.setOpacity(0.9f); // 透かしをタイリングして配置するかどうかの設定 watermarkpdf.setTiling(false); // 透かしの倍率の設定 watermarkpdf.setScale(0.8f); // 透かしの設定 doc.appendWaterMark(watermarkpdf); } } } }