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

PDF Tool APIサンプル集:ページサイズに合わせて折り返す文字列(新規のページを作成)

PDFの出力先となるファイルパスを指定して 新規のPDFのページを用意し
ページサイズに合わせて折り返す文字列を書きこんで出力するコンソールアプリケーションです。

概要

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

sample.exe c:\sav\out.pdf

ダウンロード

出力結果イメージ

出力結果イメージ

サンプルコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
   Antenna House PDF Tool API 7.0
   C# Interface sample program
 
   概要:ページサイズに合わせて折り返す文字列(新規のページを作成)
 
   Copyright 2021 Antenna House,Inc.
*/
using System;
using PdfTkNet;
 
namespace sample04cs
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("PDF Tool API V7.0 C# サンプル");
 
            // 出力ファイル名の初期値を設定
            string outFilePath = @"C:\sav\out.pdf";
 
            // 出力ファイル名
            if (args.Length > 0)
            {
                outFilePath = args[0];
            }
            try
            {
                using (PtlParamOutput output = new PtlParamOutput(outFilePath))
                using (PtlPDFDocument doc = new PtlPDFDocument())
                using (var option = new PtlOption())
                {
                    option.setUnit(PtlOption.UNIT.UNIT_PT);
 
                    string fontName = "メイリオ";
                    float fontSize = 24.0f;
 
                    using (PtlParamFont fontNormal = new PtlParamFont(fontName, fontSize, false, false, true))
                    using (PtlColorDeviceRGB colorBlack = new PtlColorDeviceRGB(0.0f, 0.0f, 0.0f))
                    using (PtlPages pages = doc.getPages())
                    using (PtlPage newpage = new PtlPage())
                    using (PtlSize pageSize = newpage.getSize())
                    {
                        pages.append(newpage, PtlPages.INSERT_OPTION.OPTION_NONE);  // 新規ページの追加
 
                        using (PtlContent content = newpage.getContent())
                        using (PtlRect rect = new PtlRect(0, 0, pageSize.getWidth(), pageSize.getHeight()))
                        using (PtlTextBox textBox = content.drawTextBox(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, pageSize.getWidth(), pageSize.getHeight()))
                        using (PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox())
                        {
                            paramWriteString.setFont(fontNormal);
                            paramWriteString.setTextColor(colorBlack);
 
                            string text1 = "そのあくる日もごんは、栗をもって、兵十の家へ出かけました。兵十は物置で縄《なわ》をなっていました。それでごんは家の裏口から、こっそり中へはいりました。そのとき兵十は、ふと顔をあげました。と狐が家の中へはいったではありませんか。こないだうなぎをぬすみやがったあのごん狐めが、またいたずらをしに来たな。「ようし。」兵十は立ちあがって、納屋《なや》にかけてある火縄銃《ひなわじゅう》をとって、火薬をつめました。そして足音をしのばせてちかよって、今戸口を出ようとするごんを、ドンと、うちました。ごんは、ばたりとたおれました。兵十はかけよって来ました。家の中を見ると、土間《どま》に栗が、かためておいてあるのが目につきました。「おや」と兵十は、びっくりしてごんに目を落しました。「ごん、お前だったのか。いつも栗をくれたのは」ごんは、ぐったりと目をつぶったまま、うなずきました。兵十は火縄銃をばたりと、とり落しました。青い煙が、まだ筒口から細く出ていました。(新美南吉「ごん狐」)";
                            textBox.writeStringNL(text1, 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);
            }
        }
    }
}