PDF lets you add text to the document, measure the text and do much more in a document. The following sections discuss the text-related operations that can be performed using PDF.
You can add text to PDF document using DrawString method of the C1PdfDocument class. The DrawString method allows you to draw a given string at a specified location using font and brush. Moreover, you can also use other overloads for DrawString method.
To draw text in Pdf document, use the following code. This example uses the sample created in Quick Start.
C# |
コードのコピー
|
---|---|
//テキストを追加します。 pdf.DrawString("Hello World!", font2, Brushes.Black, rect2); |
By default, the DrawString method aligns the text on the top left of the given rectangle, wraps the string within it, and does not clip the output to the rectangle. You can change all these options by specifying a StringFormat parameter in the DrawString method. The StringFormat parameter has members that allows you to specify the horizontal alignment (Alignment), vertical alignment (LineAligmnent), and flags that control wrapping and clipping (FormatFlags).
The following image displays a Pdf document with text aligned at the center of rectangular area.
In the following example, the code creates a StringFormat object and uses it to align the text to the center of the rectangle, both vertically and horizontally:
C# |
コードのコピー
|
---|---|
// C1PdfDocument オブジェクトを作成します。 C1PdfDocument pdf = new C1PdfDocument(); Font font = new Font("Arial", 12); RectangleF rect = new RectangleF(72, 72, 100, 50); string text = "Some long string to be rendered into a small rectangle. "; text = text + text + text + text + text + text; // 文字列を中央揃えにします。 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; pdf.DrawString(text, font, Brushes.Black, rect, sf); pdf.DrawRectangle(Pens.Gray, rect); // ドキュメントをファイルに保存します。 pdf.Save("PDF_Text.pdf"); |
You can determine whether a string fits on the page well before rendering it using the MeasureString method. This method returns the SizeF structure, which provides the string's width and height (in points). This allows you to understand the space you would require to render the desired string in a PDF document. The MeasureString method can be useful in the scenarios where you would want to add a small amount of text into a PDF document and understand the space actually used by the text in the page.
You can use the following code to determine if a paragraph fits on the current page.
C# |
コードのコピー
|
---|---|
C1PdfDocument pdf; public Form1() { InitializeComponent(); pdf = new C1.Pdf.C1PdfDocument(); Font font = new Font("Arial", 10); RectangleF rectPage = pdf.PageRectangle; rectPage.Inflate(-72, -72); RectangleF rect = rectPage; List<string> myStringList = new List<string>(); for(int i=0;i<=30;i++) myStringList.Add("Data "+i); foreach (string s in myStringList) { rect = RenderParagraph(s, font, rect, rectPage); } pdf.Save("MeasureString.pdf"); ProcessStartInfo psInfo = new ProcessStartInfo { FileName = "MeasureString.pdf", UseShellExecute = true }; Process.Start(psInfo); } private RectangleF RenderParagraph(string text, Font font, RectangleF rect, RectangleF rectPage) { // 必要な高さを計算します。 SizeF sz = pdf.MeasureString(text, font, rect.Width); rect.Height = sz.Height; // このページに収まらない場合は、改ページします。 if (rect.Bottom > rectPage.Bottom) { pdf.NewPage(); rect.Y = rectPage.Top; } // 文字列を描画します。 pdf.DrawString(text, font, Brushes.Black, rect); // 長方形を更新します。 rect.Offset(0, rect.Height); return rect; } |
Just like the regular text, you can also display paragraphs with rich format, mixed fonts and colors using DrawStringRtf method as demonstrated in the following code:
C# |
コードのコピー
|
---|---|
string rtfHdr = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}} {\*\generator Riched20 10.0.19041}\viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs22\lang9 Hello World\par}"; C1PdfDocument pdf = new C1PdfDocument(); RectangleF rect = pdf.PageRectangle; rect.Inflate(-72, -72); pdf.DrawStringRtf(rtfHdr, Font, Brushes.Black, rect); // 保存します。 pdf.Save("Rtf_Content.pdf"); ProcessStartInfo psInfo = new ProcessStartInfo { FileName = "Rtf_Content.pdf", UseShellExecute = true }; Process.Start(psInfo); |
DrawStringRtf method is similar to DrawString except that it interprets the string as RTF.
The DrawString method returns an integer that can be used to set the text flow from page to page or from one frame to another within a page. You can use the following code to set the text flow in a PDF document.
C# |
コードのコピー
|
---|---|
string text = "Render a string spanning multiple pages."; // 複数のページにまたがる文字列をレンダリングします。 while(true) { // 長方形に収まる範囲でレンダリングします。 int nextChar = pdf.DrawString(text, font, Brushes.Black, rect); // 完了したら中断します。 if(nextChar >= text.Length) { break; } // レンダリングされた部分を取り除きます。 Text = text.Substring(nextChar); // 次のページに移動します。 pdf.NewPage(); } |
You can also combine the MeasureString and DrawString methods to develop rendering routines that provide extensive control over how paragraphs are rendered.