/*
Antenna House PDF Tool API V8.0
C++ Interface sample program
概要:テキスト検索してマスク処理
Copyright 2018-2026 Antenna House, Inc.
*/
#include < PdfTk.h >
#include < stdio.h >
using namespace PdfTk;
int main(int argc, char* argv[])
{
if (argc < 4) {
printf("usage: SearchTextAndMaskAuto.exe in-pdf-file out-pdf-file keyword\n");
return 1;
}
try
{
PtlParamInput input(argv[1]);
PtlParamOutput output(argv[2]);
PtlParamString keyword(argv[3]);
PtlPDFDocument doc;
// PDFファイルをロードします。
doc.load(input);
// 検索の為のパラメータ
PtlParamSearchTextAndSetMask paramSearchText;
paramSearchText.appendText(keyword);
paramSearchText.setTextType(PtlParamSearchText::TEXT_RAW);
PtlColorDeviceRGB color(1.0f, 0.0f, 0.0f);
paramSearchText.setColor(color);
paramSearchText.setOpacity(0.5f);
// 検索実行
doc.searchTextAndDoProcess(paramSearchText);
// ファイルに保存します。
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;
}
PDF Tool APIサンプルコード:テキスト検索とデータ削除
テキスト検索してマスク処理を付けます。
概要
サンプルコードの概要
指定したテキストで検索し、マスク処理を追加します。
このサンプルでは、関数による自動処理でマスク処理を追加しています。
「座標を取得してハイライト注釈を追加する」のように座標の取得とマスク追加の処理を分けることもこともできます。
- PtlParamSearchTextAndSetMask: テキスト検索してマスク処理するパラメータを表現したクラス
- PtlParamSearchTextAndSetMask.appendText(): 検索するテキストを追加
- PtlParamSearchTextAndSetMask.setColor(): マスクの色設定
- PtlParamSearchTextAndSetMask.setOpacity(): マスク色の不透明度を設定
- PtlPDFDocument.searchTextAndDoProcess(): テキスト検索して後処理
- PtlPage.searchTextAndDoProcess(): テキスト検索して後処理
サンプルコード
/*
Antenna House PDF Tool API V8.0
Java Interface sample program
概要:テキスト検索してマスク処理
Copyright 2018-2025 Antenna House, Inc.
*/
package Sample;
import jp.co.antenna.ptl.*;
public class SearchTextAndMaskAuto {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length < 3)
{
System.out.println("usage: java SearchTextAndMaskAuto in-pdf-file out-pdf-file keyword");
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument())
{
// PDFファイルをロード
doc.load(inputFile);
try (PtlParamSearchTextAndSetMask paramSearchText = new PtlParamSearchTextAndSetMask();
PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
{
// 検索設定
paramSearchText.appendText(args[2]);
paramSearchText.setColor(color);
paramSearchText.setOpacity(0.5f);
// 検索してマスク処理実行
doc.searchTextAndDoProcess(paramSearchText);
}
// ファイルに保存します。
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("-- 完了 --");
}
}
}
/*
Antenna House PDF Tool API V8.0
.NET Interface sample program
概要:テキスト検索してマスク処理
Copyright 2018-2026 Antenna House, Inc.
*/
using System;
using PdfTkNet;
namespace SearchTextAndMaskAuto
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("usage: SearchTextAndMaskAuto.exe in-pdf-file out-pdf-file keyword");
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 (PtlParamSearchTextAndSetMask paramSearchText = new PtlParamSearchTextAndSetMask()) // 検索の為のパラメータ
using (PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
{
// 検索設定
paramSearchText.appendText(args[2]);
paramSearchText.setColor(color);
paramSearchText.setOpacity(0.5f);
// 検索実行
doc.searchTextAndDoProcess(paramSearchText);
}
// ファイルに保存します。
doc.save(outputFile);
}
}
catch (PtlException pex)
{
Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
pex.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("-- 完了 --");
}
}
}
}
実行例
コマンドラインでの実行例
SearchTextAndMaskAuto.exe C:\in\gon.pdf C:\sav\outSearchTextAndMaskAuto.pdf "「ごん、お前《まい》だったのか。いつも栗をくれたのは」" 完了!
java -jar SearchTextAndMaskAuto.jar C:\in\gon.pdf C:\sav\outSearchTextAndMaskAuto.pdf "「ごん、お前《まい》だったのか。いつも栗をくれたのは」" -- 完了 --
SearchTextAndMaskAuto.exe C:\in\gon.pdf C:\sav\outSearchTextAndMaskAuto.pdf "「ごん、お前《まい》だったのか。いつも栗をくれたのは」" -- 完了 --
出力結果イメージ
このサンプルでは検索結果のテキストにマスク処理を追加します。
出力されたPDFでは"「ごん、お前《まい》だったのか。いつも栗をくれたのは」"の文字列が削除され、
不透明度50%の赤色で塗りつぶされています。

