Add text to PDF page in any locations in Visual Studio C# programming.

Lots of people think that PDFs are "read-only" and can't be edited. Not true! With the right PDF library and control, you can easily edit a PDF document. So if you want to add text (plain text or text annotation or text form) to PDF or edit its content, you need to use the appropriate application for it.

In this article, it shows how to insert text content to PDF in C# program development. Developers can format the adding text as paragraph, head, list or underline sentence. The text font, size, color and location can all be modified.


Add long sentence as paragraph to PDF in C# code.
// Create a new PDF document.
PdfDocument document = new PdfDocument();

// Create an empty page in this document.
PdfPage page = document.AddPage();

// Obtain an XGraphics object to render to
XGraphics g = XGraphics.FromPdfPage(page);

// Set the font style
XFont font = new XFont("Times New Roman", 16, XFontStyle.BoldItalic);

// Set a long long text
string text = "This is the 1st sentence! This is the 2nd sentence! " +
       "This is the 3rd sentence! This is the 4th sentence! This is the 5th sentence!";
            
// Create a rectangle to render the long sentence in.
// You only need to set the rectangle x, y and width, the height 
// will be measured by XTextFormatter object automatically. 
// So you can input any value of height, here we set to 0.
XRect rect = new XRect(50, 50, page.Width - 50 * 2, 0);

// Create a text formatter object
XTextFormatter tf = new XTextFormatter(g);

// Add the long sentence to the page. And this api will 
// returen the actual height used by the formatter 
double neededHeight = tf.DrawString(text, font, XBrushes.Black, rect);

//Draw a rectangle to show the real space this long text used
rect = new XRect(50, 50, page.Width - 50 * 2, neededHeight);
g.DrawRectangle(XPens.Red, rect);                      

// Save and show the document
document.Save("LongText.pdf");
Process.Start("LongText.pdf");

If you only want to add a short text (even single text character), such as a title or head in PDF, you don't need the XTextFormatter (which can only layout in the TopLeft), just using the DrawLine() in the XGraphics object. Then you can set all the Alignment of this String, like Center.

Add short text as title or head to PDF in C# code.
// Create a new PDF document.
PdfDocument document = new PdfDocument();

// Create an empty page in this document.
PdfPage page = document.AddPage();

// Obtain an XGraphics object to render to
XGraphics g = XGraphics.FromPdfPage(page);

// Set the font style
XFont font = new XFont("Times New Roman", 36, XFontStyle.BoldItalic);

// Set a short text, no wider than the page width
string text = "XsPDF title!";

// Create a rectangle to draw the text in and draw in it
XRect rect = new XRect(0, 50, page.Width, 36);
g.DrawString(text, font, XBrushes.Black, rect, XStringFormats.Center);

// Save and show the document
document.Save("ShortText.pdf");
Process.Start("ShortText.pdf");

More PDF tutorial