ComponentOne PDF for .NET
In This Topic
    Text
    In This Topic

    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.

    Add Text

    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#
    Copy Code
    //Add text
    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#
    Copy Code
    // Create the C1PdfDocument object.
    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;
    // Center align string
    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);
            
    // Save the document to a file.
    pdf.Save("PDF_Text.pdf");
    

    Measure Text

    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#
    Copy Code
    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)
    {
        // Calculate the necessary height.
        SizeF sz = pdf.MeasureString(text, font, rect.Width);
        rect.Height = sz.Height;
        // If it won't fit this page, do a page break.
        if (rect.Bottom > rectPage.Bottom)
        {
            pdf.NewPage();
            rect.Y = rectPage.Top;
        }
        // Draw the string.
        pdf.DrawString(text, font, Brushes.Black, rect);
        // Update rectangle for next time.
        rect.Offset(0, rect.Height);
        return rect;
    }
    

    Draw RTF Text

    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#
    Copy Code
                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);
                // Save.
                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.

    Set Text Flow

    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#
    Copy Code
                string text = "Render a string spanning multiple pages.";
    
                // Render a string spanning multiple pages.
                while(true)
    {
                    // Render as much as will fit into the rectangle.
                    int nextChar = pdf.DrawString(text, font, Brushes.Black, rect);
                    // Break when done.
                    if(nextChar >= text.Length)
                    {
                        break;
                    }
                    // Get rid of the part that was rendered.
                    Text = text.Substring(nextChar);
                    // Move on to the next page.
                    pdf.NewPage();
                }
    

    You can also combine the MeasureString and DrawString methods to develop rendering routines that provide extensive control over how paragraphs are rendered.