OEM販売のご相談
ご相談ください!

PDF Tool APIサンプルコード:パス(矩形、線、多角形、折れ線描画)の描画

機能イメージ

PDF文書のページ上にパス(矩形、線、多角形、折れ線描画)を描画します。

概要

サンプルコードの概要

PDF文書のページ上にパス(丸角矩形・矩形・楕円・線・多角形・折れ線描画)を描画します。

  • PtlContent.drawRoundRect(PtlRect, float, float, PtlParamDrawShape) :丸角矩形の描画
  • PtlContent.drawRect(PtlRect, PtlParamDrawShape) :矩形の描画
  • PtlContent.drawCircle(PtlRect, PtlParamDrawShape) :円形(楕円または円)の描画
  • PtlContent.drawLine(PtlPoint, PtlPoint, PtlParamDrawShape) :線の描画

サンプルコード

/*
	Antenna House PDF Tool API V8.0
	C++ Interface sample program

	概要:パスの描画

	Copyright 2025 Antenna House, Inc.
*/

#include 
#include 

using namespace PdfTk;

void drawShape(PtlPage& page);

int main(int argc, char* argv[])
{
	if (argc < 3) {
		printf("usage: DrawShape.exe in-pdf-file out-pdf-file\n");
		return 1;
	}
	try
	{
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);

		PtlPDFDocument doc;

		// PDFファイルをロードします。
		doc.load(input);

		// ページコンテナの取得
		PtlPages& pages = doc.getPages();

		if (pages.isEmpty()){
			printf("ページコンテナが空\n");
			return 1;
		}

		// 先頭ページの取得
		PtlPage page = pages.get(0);

		// パスの描画
		drawShape(page);

		// ファイルに保存します。
		doc.save(output);

		printf("完了!\n");
	}
	catch (const PtlException &e)
	{
		fprintf(stderr, "Error code : %d\n %s\n", e.getErrorCode(), e.getErrorMessage().c_str());
		return 1;
	}
	return 0;
}

void drawShape(PtlPage& page)
{
	// ページコンテントの取得
	PtlContent& content = page.getContent();

	// ページサイズ
	PtlSize pageSize = page.getSize();

	// 矩形の描画
	PtlRect rect(10.0f, 10.0f, pageSize.getWidth()-10.0f, pageSize.getHeight()-10.0f);
	PtlColorDeviceRGB colorFill(1.0f, 0.0f, 0.0f);
	PtlColorDeviceRGB colorStroke(0.0f, 0.0f, 1.0f);
	PtlParamDrawShape::LINE_STYLE lineStyle = PtlParamDrawShape::LINE_STYLE_SOLID;
	//PtlParamDrawShape::LINE_STYLE lineStyle = PtlParamDrawShape::LINE_STYLE_DASHED;
	//PtlParamDrawShape::LINE_WIDTH lineWidth = PtlParamDrawShape::LINE_WIDTH_THIN;
	//PtlParamDrawShape::LINE_WIDTH lineWidth = PtlParamDrawShape::LINE_WIDTH_MIDDLE;
	PtlParamDrawShape::LINE_WIDTH lineWidth = PtlParamDrawShape::LINE_WIDTH_THICK;

	// 丸角矩形の描画
	PtlParamDrawShape paramDrawShape;
	paramDrawShape.setLineStyle(lineStyle);
	paramDrawShape.setLineWidth(lineWidth);
	paramDrawShape.setLineColor(colorStroke);
	paramDrawShape.setFillColor(colorFill);
	paramDrawShape.setOpacity(0.5f);
	content.drawRoundRect(rect, 10.0f, 10.0f, paramDrawShape);

	// 矩形の描画
	PtlRect rect2(30.0f, 30.0f, pageSize.getWidth()-30.0f, pageSize.getHeight()-30.0f);
	PtlColorDeviceRGB colorFill2(1.0f, 1.0f, 0.0f);
	PtlColorDeviceRGB colorStroke2(0.0f, 1.0f, 1.0f);

	paramDrawShape.setLineColor(colorStroke2);
	paramDrawShape.setFillColor(colorFill2);
	paramDrawShape.setOpacity(0.3f);
	content.drawRect(rect2, paramDrawShape);

	// 円の描画
	content.drawCircle(rect2, paramDrawShape);

	// 線の描画
	PtlColorDeviceRGB colorStroke3(0.0f, 1.0f, 0.0f);
	paramDrawShape.setLineColor(colorStroke3);
	content.drawLine(PtlPoint(30.0f,30.0f), PtlPoint(pageSize.getWidth()-30,pageSize.getHeight()-30.0f), paramDrawShape);
	content.drawLine(PtlPoint(30.0f,pageSize.getHeight()-30.0f), PtlPoint(pageSize.getWidth()-30.0f,30.0f), paramDrawShape);

	// 折れ線 PolyLine
	PtlPoints pointsMM1 = PtlPoints();
	pointsMM1.append(PtlPoint(70.0f, 30.0f));
	pointsMM1.append(PtlPoint(140.0f, 110.0f));
	pointsMM1.append(PtlPoint(110.0f, 180.0f));
	pointsMM1.append(PtlPoint(190.0f, 130.0f));
	pointsMM1.append(PtlPoint(260.0f, 160.0f));
	pointsMM1.append(PtlPoint(230.0f, 90.0f));
	pointsMM1.append(PtlPoint(270.0f, 30.0f));
	pointsMM1.append(PtlPoint(180.0f, 70.0f));
	paramDrawShape.setLineColor(PtlColorDeviceRGB(0.2f, 0.2f, 0.2f));
	paramDrawShape.setOpacity(0.9f);
	paramDrawShape.setLineWidth(lineWidth);
	content.drawPolyline(pointsMM1, paramDrawShape);

	// 多角形注釈 Polygon
	PtlPoints pointsMM2 = PtlPoints();
	pointsMM2.append(PtlPoint(30.0f, 30.0f));
	pointsMM2.append(PtlPoint(100.0f, 110.0f));
	pointsMM2.append(PtlPoint(70.0f, 180.0f));
	pointsMM2.append(PtlPoint(120.0f, 130.0f));
	pointsMM2.append(PtlPoint(220.0f, 160.0f));
	pointsMM2.append(PtlPoint(190.0f, 90.0f));
	pointsMM2.append(PtlPoint(230.0f, 30.0f));
	pointsMM2.append(PtlPoint(140.0f, 70.0f));
	paramDrawShape.setOpacity(0.3f);
	paramDrawShape.setFillColor(PtlColorDeviceRGB(1.0f,1.0f,0.0f));
	content.drawPolygon(pointsMM2, paramDrawShape);

}

            
/*
    Antenna House PDF Tool API V7.0
    Java Interface sample program

    概要:パスの描画

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.PtlColorDeviceRGB;
import jp.co.antenna.ptl.PtlContent;
import jp.co.antenna.ptl.PtlException;
import jp.co.antenna.ptl.PtlPDFDocument;
import jp.co.antenna.ptl.PtlPage;
import jp.co.antenna.ptl.PtlPages;
import jp.co.antenna.ptl.PtlParamDrawShape;
import jp.co.antenna.ptl.PtlParamInput;
import jp.co.antenna.ptl.PtlParamOutput;
import jp.co.antenna.ptl.PtlPoint;
import jp.co.antenna.ptl.PtlPoints;
import jp.co.antenna.ptl.PtlRect;
import jp.co.antenna.ptl.PtlSize;

public class DrawShape {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java DrawShape in-pdf-file out-pdf-file");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロードします。
            doc.load(inputFile);

            try (PtlPages pages = doc.getPages()) //ページコンテナの取得
            {
                // ページコンテナが空かどうか
                if (pages.isEmpty())
                {
                    System.out.println("ページコンテナが空\n");
                    return;
                }

                // ページの取得
                try (PtlPage page = pages.get(0))
                {
                    // パスの描画
                    drawShape(page);
                }
            }

            // ファイルに保存します。
            doc.save(outputFile);
        }
        catch (PtlException pex) {
             System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() + "\n  " + pex.getErrorMessage());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        catch (Error ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        finally {
            System.out.println("-- 完了 --");
        }
    }

    public static void drawShape(PtlPage page) throws PtlException, Exception, Error
    {
        try (PtlContent content = page.getContent(); // ページコンテントの取得
             PtlSize pageSize = page.getSize()) // ページサイズ
        {
            // パス出力
            try (PtlParamDrawShape paramDrawShape = new PtlParamDrawShape()) // パスの描画に使うパラメータクラス
            {
                // 丸角矩形の描画
                try (PtlRect rect = new PtlRect(10.0f, 10.0f, pageSize.getWidth()-10.0f, pageSize.getHeight()-10.0f);
                     PtlColorDeviceRGB colorFill = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);
                     PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f))
                {
                    paramDrawShape.setLineColor(colorStroke);
                    paramDrawShape.setFillColor(colorFill);

                    PtlParamDrawShape.LINE_STYLE lineStyle = PtlParamDrawShape.LINE_STYLE.LINE_STYLE_SOLID;
                    //int lineStyle = PtlParamDrawShape.LINE_STYLE.LINE_STYLE_DASHED;
                    paramDrawShape.setLineStyle(lineStyle);

                    //PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THIN;
                    //PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_MIDDLE;
                    PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THICK;
                    paramDrawShape.setLineWidth(lineWidth);

                    paramDrawShape.setOpacity(0.5f);
                    content.drawRoundRect(rect, 10.0f, 10.0f, paramDrawShape);
                }

                // 矩形/円の描画
                try (PtlRect rect = new PtlRect(30.0f, 30.0f, pageSize.getWidth() - 30.0f, pageSize.getHeight() - 30.0f);
                     PtlColorDeviceRGB colorFill = new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f);
                     PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 1.0f, 1.0f))
                {
                    paramDrawShape.setLineColor(colorStroke);
                    paramDrawShape.setFillColor(colorFill);
                	paramDrawShape.setOpacity(0.3f);
                    content.drawRect(rect, paramDrawShape);
                    content.drawCircle(rect, paramDrawShape);
                }

                // 線の描画
                try (PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
                {
                    paramDrawShape.setLineColor(colorStroke);
                    try (PtlPoint from = new PtlPoint(30.0f, 30.0f);
                         PtlPoint to = new PtlPoint(pageSize.getWidth() - 30, pageSize.getHeight() - 30.0f))
                    {
                        content.drawLine(from, to, paramDrawShape);
                    }
                    try (PtlPoint from = new PtlPoint(30.0f, pageSize.getHeight() - 30.0f);
                         PtlPoint to = new PtlPoint(pageSize.getWidth() - 30.0f, 30.0f))
                    {
                        content.drawLine(from, to, paramDrawShape);
                    }
                }
                
                // 折れ線 PolyLine
                try (PtlPoints pointsMM = new PtlPoints())
                {
                    pointsMM.append(new PtlPoint(70.0f, 30.0f));
                    pointsMM.append(new PtlPoint(140.0f, 110.0f));
                    pointsMM.append(new PtlPoint(110.0f, 180.0f));
                    pointsMM.append(new PtlPoint(190.0f, 130.0f));
                    pointsMM.append(new PtlPoint(260.0f, 160.0f));
                    pointsMM.append(new PtlPoint(230.0f, 90.0f));
                    pointsMM.append(new PtlPoint(270.0f, 30.0f));
                    pointsMM.append(new PtlPoint(180.0f, 70.0f));
                    paramDrawShape.setLineColor(new PtlColorDeviceRGB(0.2f, 0.2f, 0.2f));
                    paramDrawShape.setOpacity(0.9f);
                    paramDrawShape.setLineWidth(PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THICK);
                    content.drawPolyline(pointsMM, paramDrawShape);
                }
                
                
                // 多角形注釈 Polygon
                try (PtlPoints pointsMM = new PtlPoints())
                {
                    pointsMM.append(new PtlPoint(30.0f, 30.0f));
                    pointsMM.append(new PtlPoint(100.0f, 110.0f));
                    pointsMM.append(new PtlPoint(70.0f, 180.0f));
                    pointsMM.append(new PtlPoint(120.0f, 130.0f));
                    pointsMM.append(new PtlPoint(220.0f, 160.0f));
                    pointsMM.append(new PtlPoint(190.0f, 90.0f));
                    pointsMM.append(new PtlPoint(230.0f, 30.0f));
                    pointsMM.append(new PtlPoint(140.0f, 70.0f));
                    paramDrawShape.setOpacity(0.3f);
                    paramDrawShape.setFillColor(new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f));
                    content.drawPolygon(pointsMM, paramDrawShape);
                }
                
                
                
            }
        }
    }
}

            
/*
	Antenna House PDF Tool API V8.0
	.NET Interface sample program

	概要:パスの描画

	Copyright 2025 Antenna House, Inc.
*/

using PdfTkNet;
using System;
using static PdfTkNet.PtlParamDrawShape;

namespace DrawShape
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("usage: DrawShape.exe in-pdf-file out-pdf-file");
                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);

                    using (PtlPages pages = doc.getPages())
                    {
                        // ページコンテナが空かどうか
                        if (pages.isEmpty())
                        {
                            Console.WriteLine("ページコンテナが空");
                            return;
                        }

                        using (PtlPage page = pages.get(0)) // 先頭ページ
                        {
                            // パスの描画
                            drawShape(page);
                        }
                    }

                    // ファイルに保存します。
                    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 drawShape(PtlPage page)
        {
            using (PtlContent content = page.getContent())  // ページコンテントの取得
            using (PtlSize pageSize = page.getSize()) // ページサイズ
            {
                // パス出力
                using (PtlParamDrawShape paramDrawShape = new PtlParamDrawShape()) // パスの描画に使うパラメータクラス
                {
                    // 丸角矩形の描画
                    using (PtlRect rect = new PtlRect(10.0f, 10.0f, pageSize.getWidth()-10.0f, pageSize.getHeight()-10.0f))
                    using (PtlColorDeviceRGB colorFill = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                    using (PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f))
                    {
                        paramDrawShape.setLineColor(colorStroke);
	                    paramDrawShape.setFillColor(colorFill);

                        PtlParamDrawShape.LINE_STYLE lineStyle = PtlParamDrawShape.LINE_STYLE.LINE_STYLE_SOLID;  // 実線
						//PtlParamDrawShape.LINE_STYLE lineStyle = PtlParamDrawShape.LINE_STYLE.LINE_STYLE_DASHED;  // 点線
						paramDrawShape.setLineStyle(lineStyle);

                        //PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THIN;  // 細
                        //PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_MIDDLE;  // 中
                        PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THICK;  // 太
                        paramDrawShape.setLineWidth(lineWidth);  // 線の太さ

                        paramDrawShape.setOpacity(0.5f);
                        content.drawRoundRect(rect, 10.0f, 10.0f, paramDrawShape); // 角丸矩形の描画
                    }

                    // 矩形/円の描画
                    using (PtlRect rect = new PtlRect(30.0f, 30.0f, pageSize.getWidth() - 30.0f, pageSize.getHeight() - 30.0f))
                    using (PtlColorDeviceRGB colorFill = new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f))
                    using (PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 1.0f, 1.0f))
                    {
                        paramDrawShape.setLineColor(colorStroke);
                        paramDrawShape.setFillColor(colorFill);
                        paramDrawShape.setOpacity(0.3f);
                        content.drawRect(rect, paramDrawShape);  // 矩形の描画
                        content.drawCircle(rect, paramDrawShape); // 円の描画
                    }

                    // 線の描画
                    using (PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
                    {
                        paramDrawShape.setLineColor(colorStroke);
                        using (PtlPoint from = new PtlPoint(30.0f, 30.0f))
                        using (PtlPoint to = new PtlPoint(pageSize.getWidth() - 30, pageSize.getHeight() - 30.0f))
                        {
                            content.drawLine(from, to, paramDrawShape);  // 線の描画
                        }
                        using (PtlPoint from = new PtlPoint(30.0f, pageSize.getHeight() - 30.0f))
                        using (PtlPoint to = new PtlPoint(pageSize.getWidth() - 30.0f, 30.0f))
                        {
                            content.drawLine(from, to, paramDrawShape);  // 線の描画
                        }
                    }

                    // 折れ線 PolyLine
                    using (PtlPoints pointsMM = new PtlPoints())
                    {
                        pointsMM.append(new PtlPoint(70.0f, 30.0f));
                        pointsMM.append(new PtlPoint(140.0f, 110.0f));
                        pointsMM.append(new PtlPoint(110.0f, 180.0f));
                        pointsMM.append(new PtlPoint(190.0f, 130.0f));
                        pointsMM.append(new PtlPoint(260.0f, 160.0f));
                        pointsMM.append(new PtlPoint(230.0f, 90.0f));
                        pointsMM.append(new PtlPoint(270.0f, 30.0f));
                        pointsMM.append(new PtlPoint(180.0f, 70.0f));
                        paramDrawShape.setLineColor(new PtlColorDeviceRGB(0.2f, 0.2f, 0.2f));
                        paramDrawShape.setOpacity(0.9f);
                        paramDrawShape.setLineWidth(PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THICK);
                        content.drawPolyline(pointsMM, paramDrawShape);
                    }

                    // 多角形注釈 Polygon
                    using (PtlPoints pointsMM = new PtlPoints())
                    {
                        pointsMM.append(new PtlPoint(30.0f, 30.0f));
                        pointsMM.append(new PtlPoint(100.0f, 110.0f));
                        pointsMM.append(new PtlPoint(70.0f, 180.0f));
                        pointsMM.append(new PtlPoint(120.0f, 130.0f));
                        pointsMM.append(new PtlPoint(220.0f, 160.0f));
                        pointsMM.append(new PtlPoint(190.0f, 90.0f));
                        pointsMM.append(new PtlPoint(230.0f, 30.0f));
                        pointsMM.append(new PtlPoint(140.0f, 70.0f));
                        paramDrawShape.setOpacity(0.3f);
                        paramDrawShape.setFillColor(new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f));
                        content.drawPolygon(pointsMM, paramDrawShape);
                    }
                }
            }
        }
    }
}

            

サンプルコードのダウンロードはこちら

実行例

コマンドラインでの実行例

DrawShape.exe C:\in\test.pdf C:\sav\outDrawShape.pdf
完了!
java -jar DrawShape.jar C:\in\test.pdf C:\sav\outDrawShape.pdf
-- 完了 --
DrawShape.exe C:\in\test.pdf C:\sav\outDrawShape.pdf
-- 完了 --

出力結果イメージ

出力されたPDFの1ページ目に丸角矩形・矩形・楕円・線・多角形・折れ線描画が追加されている。

出力イメージ

サンプルコードのダウンロード

サンプルコード

サンプルで使用した入出力PDF