/* Antenna House PDF Tool API V7.0 .NET Interface sample program 概要:画像の描画 Copyright 2013-2021 Antenna House, Inc. */ using System; using PdfTkNet; namespace DrawImage { class Program { static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("usage: DrawImage.exe in-pdf-file out-pdf-file insert-image-file"); return; } try { using (PtlParamInput inputFile = new PtlParamInput(args[0])) //元PDF using (PtlParamOutput outputFile = new PtlParamOutput(args[1])) //出力PDF using (PtlPDFDocument doc = new PtlPDFDocument()) { // PDFファイルをロードします。 doc.load(inputFile); using (PtlPages pages = doc.getPages()) { //ページコンテナが空かどうか if (pages.isEmpty()) { Console.WriteLine("ページコンテナが空"); return; } using (PtlPage page = pages.get(0)) // 先頭ページ { // パスの描画 drawImage(page, 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 drawImage(PtlPage page, string pathImage) { using (PtlContent content = page.getContent()) // ページコンテントの取得 using (PtlSize pageSize = page.getSize()) // ページサイズ { using (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage()) // 画像の描画に使うパラメータクラス using (PtlParamInput insertImage = new PtlParamInput(pathImage)) //挿入する画像 using (PtlRect rect = new PtlRect(10.0f, 10.0f, pageSize.getWidth() - 10.0f, pageSize.getHeight() - 10.0f)) // 描画矩形 { //入力画像ストリームの設定 paramDrawImage.setImageStream(insertImage); //マルチTiffのページ番号の設定(Tiffファイルにのみ有効) paramDrawImage.setImagePageNumber(0); // 画像の描画 content.drawImage(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, paramDrawImage); } } } } }