SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

ComponentZine(ComponentOne)

書式を持った文書をPDF化する.NET アプリケーションの作成

ComponentOne Studio Enterprise 2011JのPDF for .NETコンポーネントを使ったアプリケーションの作成

  • このエントリーをはてなブックマークに追加

PDFコンテンツの作成

 次に、処理のメインとなるPDFコンテンツを作成していきます。独自のプロシージャ(メソッド)「pdfcontents」を作成し、この中に処理を作成します。コードは、上から順番にレイアウトするように、次の順序で作成していきます。

  1. 発行者の作成
  2. 発行日の作成
  3. 発行番号の作成
  4. 見出しの作成
  5. 題名の作成

 これらは、いずれも使用するフォント、描画領域、描画色を作成/指定してDrawStringメソッドで描画します。描画領域の位置とサイズに注意してください。それぞれ、PDFドキュメントのどの位置にくるのかを計算しながら決めていきます。

 また、「RichTextBox_内容」については、改行やタブの情報も含めてPDFドキュメント化するため、一度RichTextBoxコントロールのSaveFileメソッドで入力内容をRTF形式のファイルに保存し、DrawStringRtfメソッドで描画します。こうすると、テキストを加工することなく簡単にRTFテキストを描画できます。描画した各データは、いずれもストリームに格納されます。

Visual Basic
Private Sub pdfcontents()
    '発行者の作成
    Dim newfont As Font = New Font("MS Pゴシック", 10, FontStyle.Regular)
    Dim newrect As RectangleF = New RectangleF(300, 20, 200, 30)
    C1PdfDocument1.DrawString("発行者:" + TextBox_発行者.Text, newfont, Brushes.Black, newrect)

    '発行日の作成
    newfont = New Font("MS Pゴシック", 10, FontStyle.Regular)
    newrect = New RectangleF(300, 35, 200, 30)
    C1PdfDocument1.DrawString("発行日:" + TextBox_発行日.Text, newfont, Brushes.Black, newrect)

    '発行番号の作成
    newfont = New Font("MS Pゴシック", 10, FontStyle.Regular)
    newrect = New RectangleF(300, 50, 150, 30)
    C1PdfDocument1.DrawString("発行番号:" + TextBox_発行番号.Text, newfont, Brushes.Black, newrect)

    '見出しの作成
    newfont = New Font("MS Pゴシック", 28, FontStyle.Underline)
    newrect = New RectangleF(200, 120, 200, 30)
    C1PdfDocument1.DrawString("業 務 連 絡", newfont, Brushes.Black, newrect)

    '題名の作成
    newfont = New Font("MS Pゴシック", 16, FontStyle.Regular)
    newrect = New RectangleF(100, 200, 400, 30)
    C1PdfDocument1.DrawString(TextBox_題名.Text, newfont, Brushes.Blue, newrect)

    '内容の作成
    newfont = New Font("MS Pゴシック", 12)
    newrect = New RectangleF(50, 250, 400, 400)
    RichTextBox_内容.SaveFile("temp.rtf")
    Dim st As String = File.ReadAllText("temp.rtf", System.Text.Encoding.Default)
    C1PdfDocument1.DrawStringRtf(st, newfont, Brushes.Black, newrect)
End Sub
C#
private void pdfcontents()
{
    //発行者の作成
    Font newfont = new Font("MS Pゴシック", 10, FontStyle.Regular);
    RectangleF newrect = new RectangleF(300, 20, 200, 30);
    c1PdfDocument1.DrawString("発行者:" + textBox_発行者.Text, newfont, Brushes.Black, newrect);

    //発行日の作成
    newfont = new Font("MS Pゴシック", 10, FontStyle.Regular);
    newrect = new RectangleF(300, 35, 200, 30);
    c1PdfDocument1.DrawString("発行日:" + textBox_発行日.Text, newfont, Brushes.Black, newrect);

    //発行番号の作成
    newfont = new Font("MS Pゴシック", 10, FontStyle.Regular);
    newrect = new RectangleF(300, 50, 150, 30);
    c1PdfDocument1.DrawString("発行番号:" + textBox_発行番号.Text, newfont, Brushes.Black, newrect);

    //見出しの作成
    newfont = new Font("MS Pゴシック", 28, FontStyle.Underline);
    newrect = new RectangleF(200, 120, 200, 30);
    c1PdfDocument1.DrawString("業 務 連 絡", newfont, Brushes.Black, newrect);

    //題名の作成
    newfont = new Font("MS Pゴシック", 16, FontStyle.Regular);
    newrect = new RectangleF(100, 200, 400, 30);
    c1PdfDocument1.DrawString(textBox_題名.Text, newfont, Brushes.Blue, newrect);

    //内容の作成
    newfont = new Font("MS Pゴシック", 12);
    newrect = new RectangleF(50, 250, 400, 400);
    richTextBox_内容.SaveFile("temp.rtf");
    String st = File.ReadAllText("temp.rtf", System.Text.Encoding.Default);
    c1PdfDocument1.DrawStringRtf(st, newfont, Brushes.Black, newrect);
}

 作成したPDFコンテンツ「pdfcontents」は、Button「PDF文書を作成する」のClickイベントハンドラで呼び出します。

Visual Basic
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    'PDFコンテンツの作成
    pdfcontents()
C#
private void button1_Click(object sender, EventArgs e)
{
    //PDFコンテンツの作成
    pdfcontents();

PDF文書のプロパティ作成

 続いて、PDF文書のプロパティに表示される情報を作成します。これは、既に説明したように、DocumentInfoクラスのプロパティを使って設定します。各プロパティに該当するTextBoxコントロールの入力値を代入するだけです。

Visual Basic
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    'PDFコンテンツの作成
    pdfcontents()

    'PDF文書のプロパティ作成
    C1PdfDocument1.DocumentInfo.Author = TextBox_作成者.Text
    C1PdfDocument1.DocumentInfo.Subject = TextBox_サブタイトル.Text
    C1PdfDocument1.DocumentInfo.Keywords = TextBox_キーワード.Text
    C1PdfDocument1.DocumentInfo.Title = TextBox_題名.Text
C#
private void button1_Click(object sender, EventArgs e)
{
    //PDFコンテンツの作成
    pdfcontents();

    //PDF文書のプロパティ作成
    c1PdfDocument1.DocumentInfo.Author = textBox_作成者.Text;
    c1PdfDocument1.DocumentInfo.Subject = textBox_サブタイトル.Text;
    c1PdfDocument1.DocumentInfo.Keywords = textBox_キーワード.Text;
    c1PdfDocument1.DocumentInfo.Title = textBox_題名.Text;

PDFファイルの作成

 PDFドキュメントの内容ができあがったら保存するファイル名を作成し、C1PdfDocumentクラスのSaveメソッドを実行すると、プロパティの内容を持ったPDFドキュメントが作成されます。あとは、メッセージボックスで保存した旨を表示し、ProcessクラスのStartメソッドを使って作成したPDFドキュメントを表示してできあがりです。

Visual Basic
    'PDFファイルの作成
    Dim fname As String = "業務連絡" + TextBox_発行番号.Text + " .pdf"
    C1PdfDocument1.Save(fname)
    MessageBox.Show("PDF文書を作成しました", "文書保存", MessageBoxButtons.OK, MessageBoxIcon.Information)
    System.Diagnostics.Process.Start(fname)
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Me.Close()
End Sub
C#
    //PDFファイルの作成
    String fname = "業務連絡" + textBox_発行番号.Text + " .pdf";
    c1PdfDocument1.Save(fname);
    MessageBox.Show("PDF文書を作成しました", "文書保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
    System.Diagnostics.Process.Start(fname);
}

private void button2_Click(object sender, EventArgs e)
{
    this.Close();
}

まとめ

 C1PdfDocumentコントロールは、これまで紹介してきたようにとても簡単にデータをPDFドキュメント化できます。C1PdfDocumentコントロールがRTFデータをPDF化できる機能を持っているため、RichTextBoxコントロールと組み合わせることで、書式付テキストだけでなく段落やインデント、表組みなども作成できます。ただ簡単なだけでなく、見た目にもかなりのリッチなレベルでPDFドキュメント作成アプリケーションを作ることが可能です。

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
ComponentZine(ComponentOne)連載記事一覧

もっと読む

この記事の著者

瀬戸 遥(セト ハルカ)

8ビットコンピュータの時代からBASICを使い、C言語を独習で学びWindows 3.1のフリーソフトを作成、NiftyServeのフォーラムなどで配布。Excel VBAとVisual Basic関連の解説書を中心に現在まで40冊以上の書籍を出版。近著に、「ExcelユーザーのためのAccess再...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/6342 2012/01/24 10:57

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング