Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Draw Text on PDF Example

By utilizing the DrawText method available for the PdfDocument object, you can add text to an existing PDF without altering its original content.

:path=/static-assets/pdf/content-code-examples/how-to/draw-text-and-bitmap-draw-text.cs
using IronPdf; using IronSoftware.Drawing; // Create a ChromePdfRenderer instance to generate PDFs from HTML ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render a simple HTML string as a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // Draw text on the first page of the PDF // Parameters: // - The text to draw ("Some text") // - The font type (Times New Roman) // - The font size (12) // - The index of the page to draw on (0, since we have only one page) // - X and Y coordinates for the text (100, 100) // - The color of the text (Black) // - The rotation of the text (0 degrees) pdf.DrawText("Some text", "Times New Roman", 12, 0, 100, 100, IronSoftware.Drawing.Color.Black, 0); // Save the modified PDF document to a file // The file will be saved with the name "drawText.pdf" pdf.SaveAs("drawText.pdf");
Imports IronPdf Imports IronSoftware.Drawing ' Create a ChromePdfRenderer instance to generate PDFs from HTML Private renderer As New ChromePdfRenderer() ' Render a simple HTML string as a PDF document Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' Draw text on the first page of the PDF ' Parameters: ' - The text to draw ("Some text") ' - The font type (Times New Roman) ' - The font size (12) ' - The index of the page to draw on (0, since we have only one page) ' - X and Y coordinates for the text (100, 100) ' - The color of the text (Black) ' - The rotation of the text (0 degrees) pdf.DrawText("Some text", "Times New Roman", 12, 0, 100, 100, IronSoftware.Drawing.Color.Black, 0) ' Save the modified PDF document to a file ' The file will be saved with the name "drawText.pdf" pdf.SaveAs("drawText.pdf")
$vbLabelText   $csharpLabel

Available Fonts in FontTypes Class

The DrawText method currently supports all Standard Fonts in IronPDF, including Courier, Arial (or Helvetica), Times New Roman, Symbol, and ZapfDingbats. Visit the 'Standard Fonts in IronPDF' section in the Manage Fonts article for italic, bold, and oblique variants of these font types.

The ZapfDingbats font, in particular, can be used to display symbols such as ✖❄▲❪ ❫. For a comprehensive list of supported symbols, you can visit Wikipedia on Zapf Dingbats.

Output fonts sample on PDF

Fonts Sample on PDF

Draw Text with Newline

The draw text action supports newline characters, allowing you to render text with built-in newlines for better formatting and visual clarity.

To achieve this, add newline characters (\n) to the text string. Using the example above, you can draw:

string textWithNewlines = "Some text\nSecond line"; pdfDoc.DrawText(textWithNewlines, font, position);
string textWithNewlines = "Some text\nSecond line"; pdfDoc.DrawText(textWithNewlines, font, position);
Imports Microsoft.VisualBasic Dim textWithNewlines As String = "Some text" & vbLf & "Second line" pdfDoc.DrawText(textWithNewlines, font, position)
$vbLabelText   $csharpLabel

Use Custom Font

We also support using custom fonts with the DrawText method; below is an example with the Pixelify Sans Font added for the text.

:path=/static-assets/pdf/content-code-examples/how-to/draw-text-and-bitmap-draw-custom-font.cs
using IronPdf; using IronSoftware.Drawing; // Used for color and font manipulations using System.IO; // Initialize the PDF renderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render an HTML string into a PDF document // This example uses a simple HTML string "<h1>testing</h1>" PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // Load a custom font from a file // Ensure the path to the font file is correct byte[] fontBytes = File.ReadAllBytes(@".\PixelifySans-VariableFont_wght.ttf"); // Add the font to the PDF document's font collection // It returns an object containing the details about the added font PdfFont addedFont = pdf.Fonts.Add(fontBytes); // Draw text on the first page of the PDF at coordinates (100, 600) // Parameters: text to draw, font name, font size, page number, x-coordinate, y-coordinate, color, and rotation angle pdf.DrawText("Iron Software", addedFont.Name, 12, 0 /* First Page */, 100, 600, Color.Black, 0); // Save the modified PDF to disk with the specified filename pdf.SaveAs("drawCustomFont.pdf");
Imports IronPdf Imports IronSoftware.Drawing ' Used for color and font manipulations Imports System.IO ' Initialize the PDF renderer Private renderer As New ChromePdfRenderer() ' Render an HTML string into a PDF document ' This example uses a simple HTML string "<h1>testing</h1>" Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' Load a custom font from a file ' Ensure the path to the font file is correct Private fontBytes() As Byte = File.ReadAllBytes(".\PixelifySans-VariableFont_wght.ttf") ' Add the font to the PDF document's font collection ' It returns an object containing the details about the added font Private addedFont As PdfFont = pdf.Fonts.Add(fontBytes) ' Draw text on the first page of the PDF at coordinates (100, 600) ' Parameters: text to draw, font name, font size, page number, x-coordinate, y-coordinate, color, and rotation angle pdf.DrawText("Iron Software", addedFont.Name, 12, 0, 100, 600, Color.Black, 0) ' Save the modified PDF to disk with the specified filename pdf.SaveAs("drawCustomFont.pdf")
$vbLabelText   $csharpLabel

Draw Image Example

With IronPDF's DrawBitmap method, you can easily add bitmaps to an existing PDF document. This method functions similarly to the Image Stamper feature, allowing you to stamp images onto an existing PDF.

Please note
The DrawBitmap method works best with large images. When attempting to use smaller resolution images, you may encounter the following exception: IronPdf.Exceptions.IronPdfNativeException: 'Error while drawing image: data length (567000) is less than expected (756000)'. To overcome this issue, you can use the Image Stamper, which seamlessly handles images of all sizes.

Sample image

1200 x 627 image

Code

:path=/static-assets/pdf/content-code-examples/how-to/draw-text-and-bitmap-draw-bitmap.cs
using IronPdf; using IronSoftware.Drawing; // Create a new instance of the ChromePdfRenderer class, // which is part of the IronPdf library. This class is used to render HTML into a PDF document. ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render the HTML string as a PDF document. This method will take the HTML content // provided and convert it to a PDF format. PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // Open an image from a specified file path using the AnyBitmap class, // part of IronSoftware.Drawing. Replace "ironSoftware.png" with your actual image file path. AnyBitmap bitmap = AnyBitmap.FromFile("ironSoftware.png"); // Draw the bitmap (image) on the PDF document. // Parameters are as follows: // - bitmap: the image to be drawn. // - x and y: the position on the PDF where the image should be drawn. // - width and height: set the image's dimensions. // The following example draws the image at position (0, 50) with a width of 250 and height of 300. pdf.DrawBitmap(bitmap, 0, 50, 250, 300); // Save the modified PDF document to a file named "drawImage.pdf". // Make sure your application has the necessary permissions to write in the output directory. pdf.SaveAs("drawImage.pdf");
Imports IronPdf Imports IronSoftware.Drawing ' Create a new instance of the ChromePdfRenderer class, ' which is part of the IronPdf library. This class is used to render HTML into a PDF document. Private renderer As New ChromePdfRenderer() ' Render the HTML string as a PDF document. This method will take the HTML content ' provided and convert it to a PDF format. Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' Open an image from a specified file path using the AnyBitmap class, ' part of IronSoftware.Drawing. Replace "ironSoftware.png" with your actual image file path. Private bitmap As AnyBitmap = AnyBitmap.FromFile("ironSoftware.png") ' Draw the bitmap (image) on the PDF document. ' Parameters are as follows: ' - bitmap: the image to be drawn. ' - x and y: the position on the PDF where the image should be drawn. ' - width and height: set the image's dimensions. ' The following example draws the image at position (0, 50) with a width of 250 and height of 300. pdf.DrawBitmap(bitmap, 0, 50, 250, 300) ' Save the modified PDF document to a file named "drawImage.pdf". ' Make sure your application has the necessary permissions to write in the output directory. pdf.SaveAs("drawImage.pdf")
$vbLabelText   $csharpLabel

Output PDF

Frequently Asked Questions

What is this software used for?

IronPDF is used to draw text and images onto existing PDF documents using C# .NET, allowing users to customize PDFs with watermarks, logos, annotations, and more.

How can I add text to a PDF using this tool?

To add text to a PDF using IronPDF, use the 'DrawText' method of the PdfDocument object, specifying the text, font, and position.

What fonts are supported for adding text?

IronPDF supports standard fonts such as Courier, Arial (Helvetica), Times New Roman, Symbol, and ZapfDingbats. Custom fonts can also be used.

Can I use custom fonts?

Yes, IronPDF allows the use of custom fonts by specifying the custom font file when using the 'DrawText' method.

How do I add an image to a PDF?

To add an image to a PDF using IronPDF, use the 'DrawBitmap' method, providing the image file and the specified position on the PDF.

What should I do if I encounter an error with small images?

If you encounter errors with small images, consider using the Image Stamper feature of IronPDF, which handles images of all sizes seamlessly.

Does this software support text with newlines?

Yes, IronPDF supports text with newlines by including newline characters '\n' in the text string when using the 'DrawText' method.

How can I start using this tool?

To start using IronPDF, download the C# library from NuGet, then follow the steps to import the PDF, add text or images, and export the edited document.

Chaknith related to Output PDF
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.