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

PDF Tool APIサンプルコード:下線付き文字列・取り消し線付き文字列・太字、斜体の文字列・枠で囲った文字列(背景色あり)

機能イメージ

新規のPDFを作成し、下線付き文字列・取り消し線付き文字列・太字、斜体の文字列・枠で囲った文字列(背景色あり)を追加します。

概要

サンプルコードの概要

下線付・取り消し線・太字、斜体などはフォント指定のパラメータクラスに設定します。
文字列を枠で囲ったり、囲い線の色や背景色はテキストボックスに指定します

  • PtlParamFont: フォント指定に使うパラメータクラス
  • PtlParamWriteString: TextBoxに使うパラメータクラス
  • PtlParamWriteStringTextBox.setFont(): フォントの設定
  • PtlParamWriteStringTextBox.setUnderline(): 下線を引くか引かないかの設定
  • PtlParamWriteStringTextBox.setStrikeOut(): 取り消し線を引くか引かないかの設定
  • PtlTextBox: ページに描画されるテキストボックスを表現するクラス
  • PtlTextBox.setBackColor(): 背景色を設定
  • PtlTextBox.setOutlineColor(): テキストボックスの縁取り色を設定
  • PtlTextBox.setOpacity(): 不透明度を設定
  • PtlTextBox.terminate(): テキストボックスを終了

サンプルコード

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

	概要:下線付き文字列・取り消し線付き文字列・太字、斜体の文字列・枠で囲った文字列(背景色あり)

	Copyright 2025- Antenna House, Inc.
*/

#include < PdfTk.h >

using namespace std;
using namespace PdfTk;

int main(int argc, char* argv[])
{
	if (argc != 2)
	{
		printf("usage: DrawTextBox_2.exe out-pdf-file \n");
		return 1;
	}

	try {
		PtlParamOutput output(argv[1]);
		PtlPDFDocument doc;

		PtlOption option;
		option.setUnit(PtlOption::UNIT_MM);

		float fontSize = 24.0f;
		PtlParamString fontname = "メイリオ";

		PtlParamFont fontNormal(fontname, fontSize, false, false, true);
		PtlParamFont fontBoldItalic(fontname, fontSize, true, true, true);
		PtlPages& pages = doc.getPages();
		PtlPage newpage;// 新しいページ
		PtlRect rectA4(0.0f, 0.0f, 297.0f, 210.0f);	// A4横用紙

		newpage.setViewBox(rectA4);
		pages.append(newpage, PtlPages::INSERT_OPTION::OPTION_NONE);

		PtlSize pageSize = newpage.getSize();
		PtlContent& content = newpage.getContent();

		PtlRect rect1 = PtlRect(10, 10, pageSize.getWidth() - 20, pageSize.getHeight() - 60);
		PtlTextBox& textBox = content.drawTextBox(rect1, PtlContent::ALIGN::ALIGN_TOP_LEFT, pageSize.getWidth() - 30, pageSize.getHeight() - 70);
		PtlParamWriteStringTextBox paramWriteString = PtlParamWriteStringTextBox();
		PtlParamWriteStringTextBox paramWriteStringBoldItalic = PtlParamWriteStringTextBox();

		paramWriteString.setFont(fontNormal);

		paramWriteString.setUnderline(true);
		const char* text1 = ("下線はPtlParamWriteStringTextBoxで指定して書くことができます。");
		textBox.writeStringNL(text1, paramWriteString);
		paramWriteString.setUnderline(false);

		textBox.writeNL();

		paramWriteString.setStrikeOut(true);
		const char* text2 = ("取り消し線はPtlParamWriteStringTextBoxで指定して書くことができます。");
		textBox.writeStringNL(text2, paramWriteString);
		paramWriteString.setStrikeOut(false);

		textBox.writeNL();

		paramWriteStringBoldItalic.setFont(fontBoldItalic);
		const char* text3 = ("太字、斜体のフォントスタイルはPtlParamFontで設定します。");
		textBox.writeStringNL(text3, paramWriteStringBoldItalic);
		textBox.terminate();

		PtlColorDeviceRGB colorBack = PtlColorDeviceRGB(1.0f, 0.8f, 0.8f);
		PtlColorDeviceRGB colorRed = PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);
		PtlRect rect2 = PtlRect(130, 10, pageSize.getWidth() - 10, pageSize.getHeight() - 150);
		PtlTextBox& textBox2 = content.drawTextBox(rect2, PtlContent::ALIGN::ALIGN_TOP_LEFT, pageSize.getWidth() - 140, pageSize.getHeight() - 160);

		textBox2.setBackColor(colorBack);    // 背景色を設定
		textBox2.setOutlineColor(colorRed);  // テキストボックスの縁取り色を設定
		textBox2.setOpacity(0.6f);           // 不透明度を設定
		textBox2.fitToBBox(false);           // TextBoxのサイズをテキストに合わせるかどうか(true:合わせる)

		paramWriteString.setFont(fontNormal);

		const char* text4 = ("枠で囲った文字列(背景色あり)は PtlTextBox に色を指定して書くことができます。");
		textBox2.writeStringNL(text4, paramWriteString);

		textBox2.terminate();

		doc.save(output);

		printf("-- 完了 --\n");

	} catch (PtlException e) {
		fprintf(stderr, "Error code : %d\n %s\n", e.getErrorCode(), e.getErrorMessage().c_str());

	} catch (...) {
		printf("Exception\n");
	}
}
            
/*
	Antenna House PDF Tool API V8.0
	Java Interface sample program

	概要:テキストボックス

	Copyright 2021-2025 Antenna House,Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class DrawTextBox_2 {

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

        try (PtlParamOutput outputFile = new PtlParamOutput(args[0]);
             PtlPDFDocument doc = new PtlPDFDocument();
             PtlOption option = new PtlOption())
        {
            option.setUnit(PtlOption.UNIT.UNIT_MM);


            String fontName = "メイリオ";
            float fontSize = 24.0f;

            try (PtlParamFont fontNormal = new PtlParamFont(fontName, fontSize, false, false, true);
                 PtlParamFont fontBoldItalic = new PtlParamFont(fontName, fontSize, true, true, true);
        		PtlPages pages = doc.getPages();
                PtlPage newpage = new PtlPage();	// 新しいページ
                PtlRect rectA4 = new PtlRect(0.0f, 0.0f, 297.0f, 210.0f);)		// A4横用紙
            {
        		newpage.setViewBox(rectA4);
        		//pages.append(newpage, PtlPages.INSERT_OPTION.OPTION_NONE);
        		pages.append(newpage, PtlPages.OPTION_NONE);
        		
        		PtlSize pageSize = newpage.getSize();
        		PtlContent content = newpage.getContent();
        		
                try (PtlRect rect1 = new PtlRect(10, 10, pageSize.getWidth() - 20, pageSize.getHeight() - 60);
            		 PtlTextBox textBox = content.drawTextBox(rect1, PtlContent.ALIGN.ALIGN_TOP_LEFT, pageSize.getWidth() - 30, pageSize.getHeight() - 70);
            		 PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox();
                     PtlParamWriteStringTextBox paramWriteStringBoldItalic = new PtlParamWriteStringTextBox();)
                {

            		paramWriteString.setFont(fontNormal);

            		paramWriteString.setUnderline(true);
            		String text1 = ("下線はPtlParamWriteStringTextBoxで指定して書くことができます。");
            		textBox.writeStringNL(text1, paramWriteString);
            		paramWriteString.setUnderline(false);

            		textBox.writeNL();

            		paramWriteString.setStrikeOut(true);
            		String text2 = ("取り消し線はPtlParamWriteStringTextBoxで指定して書くことができます。");
            		textBox.writeStringNL(text2, paramWriteString);
            		paramWriteString.setStrikeOut(false);

            		textBox.writeNL();

            		paramWriteStringBoldItalic.setFont(fontBoldItalic);
            		String text3 = ("太字、斜体のフォントスタイルはPtlParamFontで設定します。");
            		textBox.writeStringNL(text3, paramWriteStringBoldItalic);
            		textBox.terminate();
                }
                	
                try (PtlColorDeviceRGB colorBack = new PtlColorDeviceRGB(1.0f, 0.8f, 0.8f);
            		PtlColorDeviceRGB colorRed = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);
            		PtlRect rect2 = new PtlRect(130, 10, pageSize.getWidth() - 10, pageSize.getHeight() - 150);
            		PtlTextBox textBox2 = content.drawTextBox(rect2, PtlContent.ALIGN.ALIGN_TOP_LEFT, pageSize.getWidth() - 140, pageSize.getHeight() - 160);
            		PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox();)
            	{
                		textBox2.setBackColor(colorBack);    // 背景色を設定
                		textBox2.setOutlineColor(colorRed);  // テキストボックスの縁取り色を設定
                		textBox2.setOpacity(0.6f);           // 不透明度を設定
                		textBox2.fitToBBox(false);           // TextBoxのサイズをテキストに合わせるかどうか(true:合わせる)

                		paramWriteString.setFont(fontNormal);

                		String text4 = ("枠で囲った文字列(背景色あり)は PtlTextBox に色を指定して書くことができます。");
                		textBox2.writeStringNL(text4, paramWriteString);

                		textBox2.terminate();
           		}

            	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 2025- Antenna House,Inc.
*/

using System;

using PdfTkNet;

namespace DrawTextBox_2
{
    class DrawTextBox_2
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("usage: drawTextBox_2.exe out-pdf-file\n");
                return;
            }
            try
            {
                using (PtlParamOutput output = new PtlParamOutput(args[0]))
                using (PtlPDFDocument doc = new PtlPDFDocument())
                using (PtlOption option = new PtlOption())
                {
                    option.setUnit(PtlOption.UNIT.UNIT_MM);

                    string fontName = "メイリオ";
                    float fontSize = 24.0f;

                    using (PtlParamFont fontNormal = new PtlParamFont(fontName, fontSize, false, false, true))
                    using (PtlParamFont fontBoldItalic = new PtlParamFont(fontName, fontSize, true, true, true))
                    using (PtlPages pages = doc.getPages())
                    using (PtlPage newpage = new PtlPage())     // 新しいページ
                    using (PtlRect rectA4 = new PtlRect(0.0f, 0.0f, 297.0f, 210.0f)) // A4横用紙
                    {
                        newpage.setViewBox(rectA4);
                        pages.append(newpage, PtlPages.INSERT_OPTION.OPTION_NONE);

                        using (PtlSize pageSize = newpage.getSize())
                        using (var content = newpage.getContent())
                        {
                            using (PtlRect rect = new PtlRect(10, 10, pageSize.getWidth() - 20, pageSize.getHeight() - 60))
                            using (PtlTextBox textBox = content.drawTextBox(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, pageSize.getWidth() - 30, pageSize.getHeight() - 70))
                            using (PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox())
                            using (PtlParamWriteStringTextBox paramWriteStringBoldItalic = new PtlParamWriteStringTextBox())
                            {
                                paramWriteString.setFont(fontNormal);

                                paramWriteString.setUnderline(true);
                                string text1 = ("下線はPtlParamWriteStringTextBoxで指定して書くことができます。");
                                textBox.writeStringNL(text1, paramWriteString);
                                paramWriteString.setUnderline(false);

                                textBox.writeNL();

                                paramWriteString.setStrikeOut(true);
                                string text2 = ("取り消し線はPtlParamWriteStringTextBoxで指定して書くことができます。");
                                textBox.writeStringNL(text2, paramWriteString);
                                paramWriteString.setStrikeOut(false);

                                textBox.writeNL();

                                paramWriteStringBoldItalic.setFont(fontBoldItalic);
                                string text3 = ("太字、斜体のフォントスタイルはPtlParamFontで設定します。");
                                textBox.writeStringNL(text3, paramWriteStringBoldItalic);
                                textBox.terminate();
                            }

                            using (PtlColorDeviceRGB colorBack = new PtlColorDeviceRGB(1.0f, 0.8f, 0.8f))
                            using (PtlColorDeviceRGB colorRed = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                            using (PtlRect rect = new PtlRect(130, 10, pageSize.getWidth() - 10, pageSize.getHeight() - 150))
                            using (PtlTextBox textBox2 = content.drawTextBox(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, pageSize.getWidth() - 140, pageSize.getHeight() - 160))
                            using (PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox())
                            {
                                textBox2.setBackColor(colorBack);    // 背景色を設定
                                textBox2.setOutlineColor(colorRed);  // テキストボックスの縁取り色を設定
                                textBox2.setOpacity(0.6f);           // 不透明度を設定
                                textBox2.fitToBBox(false);           // TextBoxのサイズをテキストに合わせるかどうか(true:合わせる)

                                paramWriteString.setFont(fontNormal);

                                string text1 = ("枠で囲った文字列(背景色あり)は PtlTextBox に色を指定して書くことができます。");
                                textBox2.writeStringNL(text1, paramWriteString);

                                textBox2.terminate();
                            }
                        }
                        doc.save(output);
                        Console.WriteLine("-- 完了 --");
                    }
                }
            }
            catch (PtlException pex)
            {
                Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
                pex.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}



            

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

実行例

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

DrawTextBox_2.exe C:\sav\outDrawTextBox_2.pdf
-- 完了 --
java -jar DrawTextBox_2.jar C:\sav\outDrawTextBox_2.pdf
-- 完了 --
DrawTextBox_2.exe C:\sav\outDrawTextBox_2.pdf
-- 完了 --

出力結果イメージ

下線付き文字列・取り消し線付き文字列・太字、斜体の文字列・枠で囲った文字列(背景色あり)が表示されます。

出力イメージ

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

サンプルコード

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