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

PDF Tool APIサンプルコード:行間を広くした文字列・文字間隔をかえた文字列

機能イメージ

指定したPDFに行間を広くした文字列・文字間隔をかえた文字列を追加します。

概要

サンプルコードの概要

PtlParamWriteStringTextBoxを使用して行間の設定や文字間の設定を行います。

  • PtlParamWriteStringTextBox: TextBoxに使うパラメータクラス
  • PtlParamWriteStringTextBox.setLineSpacing(): 行間の設定
  • PtlParamWriteStringTextBox.setCharSpacing(): 文字間の設定

サンプルコード

/*
	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 != 3)
	{
		printf("usage: DrawTextBox_5.exe in-pdf-file out-pdf-file \n");
		return 1;
	}

	try {
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);
		PtlPDFDocument doc;
		doc.load(input);

		PtlPages& pages = doc.getPages();
		PtlPage page0 = pages.get(0);
		PtlSize pageSize = page0.getSize();
		PtlContent& content = page0.getContent();
		PtlColorDeviceRGB colorRed = PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);
		PtlParamFont fontNormal("メイリオ", 18.0f, false, false, true);
		PtlRect rect(10, 10, pageSize.getWidth() - 10, pageSize.getHeight() - 10);
		PtlTextBox& textBox = content.drawTextBox(rect, PtlContent::ALIGN::ALIGN_TOP_LEFT, pageSize.getWidth() - 20, pageSize.getHeight() - 20);

		// TextBoxの縁取りを付ける
		textBox.setOutlineColor(colorRed);

		PtlParamWriteStringTextBox paramWriteString;

		paramWriteString.setFont(fontNormal);

		const char* text50 = ("通常文字。");
		textBox.writeStringNL(text50, paramWriteString);
		const char* text51 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
		textBox.writeStringNL(text51, paramWriteString);
		textBox.writeNL();

		paramWriteString.setLineSpacing(1.5f);
		const char* text60 = ("行間を広くします。");
		textBox.writeStringNL(text60, paramWriteString);
		const char* text61 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
		textBox.writeStringNL(text61, paramWriteString);
		textBox.writeNL();

		paramWriteString.setLineSpacing(1.0f);
		const char* text40 = ("行間を狭くします。");
		textBox.writeStringNL(text40, paramWriteString);
		const char* text41 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
		textBox.writeStringNL(text41, paramWriteString);

		textBox.writeNL();

		const char* text7 = ("文字間隔をかえてみます。");
		textBox.writeStringNL(text7, paramWriteString);

		const char* text72 = ("文字間隔狭い(-1)。");
		paramWriteString.setCharSpacing(-1);
		textBox.writeStringNL(text72, paramWriteString);

		const char* text73 = ("文字間隔広い(1)。");
		paramWriteString.setCharSpacing(1);
		textBox.writeStringNL(text73, paramWriteString);

		const char* text74 = ("文字間隔広い(2)。");
		paramWriteString.setCharSpacing(2);
		textBox.writeStringNL(text74, paramWriteString);

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

package Sample;

import jp.co.antenna.ptl.*;

public class DrawTextBox_5 {

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

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument();)
        {
            doc.load(inputFile);

            try (PtlPages pages = doc.getPages();
                    PtlPage page = pages.get(0);     //1ページ目
                    PtlSize pageSize = page.getSize();
                    PtlContent content = page.getContent();
                    PtlColorDeviceRGB colorRed = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);
            		PtlParamFont fontNormal = new PtlParamFont("メイリオ", 18.0f, false, false, true);
            		PtlRect rect = new PtlRect(10, 10, pageSize.getWidth() - 10, pageSize.getHeight() - 10);
            		PtlTextBox textBox = content.drawTextBox(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, pageSize.getWidth() - 20, pageSize.getHeight() - 20))
           {
	            // TextBoxの縁取りを付ける
	            textBox.setOutlineColor(colorRed);
	
	            try (PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox();)
               {
	                paramWriteString.setFont(fontNormal);
	        	
	                String text50 = ("通常文字。");
	                textBox.writeStringNL(text50, paramWriteString);
	                String text51 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
	                textBox.writeStringNL(text51, paramWriteString);
	                textBox.writeNL();
	
	                paramWriteString.setLineSpacing(1.5f);
	                String text60 = ("行間を広くします。");
	                textBox.writeStringNL(text60, paramWriteString);
	                String text61 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
	                textBox.writeStringNL(text61, paramWriteString);
	                textBox.writeNL();
	
	                paramWriteString.setLineSpacing(1.0f);
	                String text40 = ("行間を狭くします。");
	                textBox.writeStringNL(text40, paramWriteString);
	                String text41 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
	                textBox.writeStringNL(text41, paramWriteString);
	
	                textBox.writeNL();
	
	                String text7 = ("文字間隔をかえてみます。");
	                textBox.writeStringNL(text7, paramWriteString);
	
	                String text72 = ("文字間隔狭い(-1)。");
	                paramWriteString.setCharSpacing(-1);
	                textBox.writeStringNL(text72, paramWriteString);
	
	                String text73 = ("文字間隔広い(1)。");
	                paramWriteString.setCharSpacing(1);
	                textBox.writeStringNL(text73, paramWriteString);
	
	                String text74 = ("文字間隔広い(2)。");
	                paramWriteString.setCharSpacing(2);
	                textBox.writeStringNL(text74, paramWriteString);
               }
	            textBox.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_5
{
    class DrawTextBox_5
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("usage: DrawTextBox_5.exe in-pdf-file out-pdf-file\n");
                return;
            }

            try
            {
                using (PtlParamInput input = new PtlParamInput(args[0]))
                using (PtlParamOutput output = new PtlParamOutput(args[1]))
                using (PtlPDFDocument doc = new PtlPDFDocument())
                {
                    doc.load(input);

                    using (PtlPages pages = doc.getPages())
                    using (PtlPage page = pages.get(0))     //1ページ目
                    using (PtlSize pageSize = page.getSize())
                    using (PtlContent content = page.getContent())
                    using (PtlColorDeviceRGB colorRed = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                    using (PtlParamFont fontNormal = new PtlParamFont("メイリオ", 18.0f, false, false, true))
                    using (PtlRect rect = new PtlRect(10, 10, pageSize.getWidth() - 10, pageSize.getHeight() - 10))
                    using (PtlTextBox textBox = content.drawTextBox(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, pageSize.getWidth() - 20, pageSize.getHeight() - 20))
                    {
                        // TextBoxの縁取りを付ける
                        textBox.setOutlineColor(colorRed);

                        using (PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox())
                        {
                            paramWriteString.setFont(fontNormal);

                            string text50 = ("通常文字。");
                            textBox.writeStringNL(text50, paramWriteString);
                            string text51 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
                            textBox.writeStringNL(text51, paramWriteString);
                            textBox.writeNL();

                            paramWriteString.setLineSpacing(1.5f);
                            string text60 = ("行間を広くします。");
                            textBox.writeStringNL(text60, paramWriteString);
                            string text61 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
                            textBox.writeStringNL(text61, paramWriteString);
                            textBox.writeNL();

                            paramWriteString.setLineSpacing(1.0f);
                            string text40 = ("行間を狭くします。");
                            textBox.writeStringNL(text40, paramWriteString);
                            string text41 = ("「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」(新美南吉「ごん狐」)");
                            textBox.writeStringNL(text41, paramWriteString);

                            textBox.writeNL();

                            string text7 = ("文字間隔をかえてみます。");
                            textBox.writeStringNL(text7, paramWriteString);

                            string text72 = ("文字間隔狭い(-1)。");
                            paramWriteString.setCharSpacing(-1);
                            textBox.writeStringNL(text72, paramWriteString);

                            string text73 = ("文字間隔広い(1)。");
                            paramWriteString.setCharSpacing(1);
                            textBox.writeStringNL(text73, paramWriteString);

                            string text74 = ("文字間隔広い(2)。");
                            paramWriteString.setCharSpacing(2);
                            textBox.writeStringNL(text74, paramWriteString);
                        }
                        textBox.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_5.exe C:\in\blank.pdf C:\sav\outDrawTextBox_5.pdf
-- 完了 --
java -jar DrawTextBox_5.jar C:\in\blank.pdf C:\sav\outDrawTextBox_5.pdf
-- 完了 --
DrawTextBox_5.exe C:\in\blank.pdf C:\sav\outDrawTextBox_5.pdf
-- 完了 --

出力結果イメージ

行間を広くした文字列・文字間隔をかえた文字列が追加されたPDFが出力されます。

出力イメージ

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

サンプルコード

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