/*
Antenna House PDF Tool API V7.0
C++ Interface sample program
概要:パスの描画
Copyright 2015-2021 Antenna House, Inc.
*/
#include < PdfTk.h >
#include < stdio.h >
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);
}
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 V7.0
Java Interface sample program
概要:パスの描画
Copyright 2015-2021 Antenna House, Inc.
*/
package Sample;
import jp.co.antenna.ptl.*;
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);
}
}
}
}
}
}
/*
Antenna House PDF Tool API V7.0
.NET Interface sample program
概要:パスの描画
Copyright 2015-2021 Antenna House, Inc.
*/
using System;
using PdfTkNet;
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;
//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);
}
// 矩形/円の描画
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);
}
}
}
}
}
}
}
実行例
コマンドラインでの実行例
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ページ目に丸角矩形・矩形・楕円・線が追加されている。

