PDFコンテンツの作成
次に、処理のメインとなるPDFコンテンツを作成していきます。独自のプロシージャ(メソッド)「pdfcontents」を作成し、この中に処理を作成します。コードは、上から順番にレイアウトするように、次の順序で作成していきます。
- 発行者の作成
- 発行日の作成
- 発行番号の作成
- 見出しの作成
- 題名の作成
これらは、いずれも使用するフォント、描画領域、描画色を作成/指定してDrawStringメソッドで描画します。描画領域の位置とサイズに注意してください。それぞれ、PDFドキュメントのどの位置にくるのかを計算しながら決めていきます。
また、「RichTextBox_内容」については、改行やタブの情報も含めてPDFドキュメント化するため、一度RichTextBoxコントロールのSaveFileメソッドで入力内容をRTF形式のファイルに保存し、DrawStringRtfメソッドで描画します。こうすると、テキストを加工することなく簡単にRTFテキストを描画できます。描画した各データは、いずれもストリームに格納されます。
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
  
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イベントハンドラで呼び出します。
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'PDFコンテンツの作成
    pdfcontents()
  
private void button1_Click(object sender, EventArgs e)
{
    //PDFコンテンツの作成
    pdfcontents();
  PDF文書のプロパティ作成
続いて、PDF文書のプロパティに表示される情報を作成します。これは、既に説明したように、DocumentInfoクラスのプロパティを使って設定します。各プロパティに該当するTextBoxコントロールの入力値を代入するだけです。
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
  
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ドキュメントを表示してできあがりです。
    '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
  
    //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ドキュメント作成アプリケーションを作ることが可能です。

 
              
               
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                          
                           
                              
                               
                              
                               
                              
                               
                              
                               
                              
                               
                      
                     
                      
                     
                      
                     
                      
                     
                      
                     
                      
                     
                      
                     
															
														 
															
														.png) 
     
     
     
     
     
													 
													 
													 
													 
													 
										
									

 
                    