Add text to PDFs in C#
Adding text to PDFs
The DrawText
method allows you to add simple text to a PDF file. It requires the following parameters:
FontResName
— The font to be used.DstX
— The horizontal distance between the coordinate origin and the lower-left corner of the text body.DstY
— The vertical distance between the coordinate origin and the lower-left corner of the text body.Text
— The body of the text.
To add text to a PDF file, follow the steps outlined below.
Create a
GdPicturePDF
object.Load the PDF file with the
LoadFromFile
method.Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePdfOrigin
enumeration, which accepts the following values:
PdfOriginTopLeft
PdfOriginTopRight
PdfOriginBottomRight
PdfOriginBottomLeft
Set the measurement unit, which is used to specify dimensions.
Specify which font type to use.
Select the page where you want the text to be added with the
SelectPage
method.Optional: Set the text size (
12pt
by default).Optional: Specify the text color (black by default).
To add pagination to a PDF file, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Set the origin of the coordinate system to the bottom-right corner.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomRight);// Set the measurement unit to centimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);// Specify the font type.string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold);for (int i = 1; i <= gdpicturePDF.GetPageCount(); i++){ // Select the PDF page. gdpicturePDF.SelectPage(i); // Set the text size to 16 pt. gdpicturePDF.SetTextSize(16); // Set the fill color to gray. gdpicturePDF.SetFillColor(128, 128, 128); // Draw the page number in the bottom-right corner. gdpicturePDF.DrawText(fontName, 1, 1, i.ToString());}gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system to the bottom-right corner. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomRight) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Specify the font type. Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold) For i As Integer = 1 To gdpicturePDF.GetPageCount() ' Select the PDF page. gdpicturePDF.SelectPage(i) ' Set the text size to 16 pt. gdpicturePDF.SetTextSize(16) ' Set the fill color to gray. gdpicturePDF.SetFillColor(128, 128, 128) ' Draw the page number in the bottom-right corner. gdpicturePDF.DrawText(fontName, 1, 1, i.ToString()) Next gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Adding text to PDFs at an angle
To add simple text to a PDF page at a specific angle, use the DrawRotatedText
method. It accepts the same parameters
as the DrawText
method, along with the Angle
parameter. It specifies the counterclockwise angle (in degrees) at which the text is displayed.
To add text to the bottom-left corner at a 90-degree angle, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Set the origin of the coordinate system to the bottom-left corner.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);// Set the measurement unit to centimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);// Specify the font type.string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold);for (int i = 1; i <= gdpicturePDF.GetPageCount(); i++){ // Select the PDF page. gdpicturePDF.SelectPage(i); // Set the text size to 10 pt. gdpicturePDF.SetTextSize(10); // Set the fill color to gray. gdpicturePDF.SetFillColor(128, 128, 128); // Draw the page number in the bottom-left corner. gdpicturePDF.DrawRotatedText(fontName, 1, 1, "Do not copy or scan this document.", 90);}gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system to the bottom-left corner. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Specify the font type. Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold) For i As Integer = 1 To gdpicturePDF.GetPageCount() ' Select the PDF page. gdpicturePDF.SelectPage(i) ' Set the text size to 10 pt. gdpicturePDF.SetTextSize(10) ' Set the fill color to gray. gdpicturePDF.SetFillColor(128, 128, 128) ' Draw the page number in the bottom-left corner. gdpicturePDF.DrawRotatedText(fontName, 1, 1, "Do not copy or scan this document.", 90) Next gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics
Adding text boxes to PDFs
The DrawTextBox
method allows you to add text to a PDF file in the form of a text box. It supports multiline text inside the dimensions specified in the input parameters. If a part of your text isn’t visible, it means that the text box is too small to contain the entire text. This method requires the following parameters:
FontResName
— The font to be used.Left
— The distance between the coordinate origin and the left side of the text box.Top
— The distance between the coordinate origin and the top of the text box.Right
— The distance between the coordinate origin and the right side of the text box.Bottom
— The distance between the coordinate origin and the bottom of the text box.HorizontalAlignment
— Specifies horizontal text alignment.VerticalAlignment
— Specifies vertical text alignment.Text
— The body of the text.
The HorizontalAlignment
and VerticalAlignment
parameters are defined by the TextAlignment
enumeration, which accepts the following values:
TextAlignmentCenter
— Aligns text to the center.TextAlignmentNear
— Aligns text to the left side in left-to-right writing systems. In right-to-left systems, it’s aligned to the right.TextAlignmentFar
— Aligns text to the right side in left-to-right writing systems. In right-to-left systems, it’s aligned to the left.
To add a text box to a PDF file, follow the steps outlined below.
Create a
GdPicturePDF
object.Load the PDF file with the
LoadFromFile
method.Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePdfMeasurement
enumeration, which accepts the following values:
PdfOriginTopLeft
PdfOriginTopRight
PdfOriginBottomRight
PdfOriginBottomLeft
Set the measurement unit, which is used to specify dimensions.
Specify which font type to use.
Select the page you want the text to be added to with the
SelectPage
method.Optional: Set the text size (
12pt
by default).Optional: Specify the text color (black by default).
To add pagination to a PDF file, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Set the origin of the coordinate system to the bottom-left corner.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);// Set the measurement unit to centimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);// Specify the font type.string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold);for (int i = 1; i <= gdpicturePDF.GetPageCount(); i++){ // Select the PDF page. gdpicturePDF.SelectPage(i); // Set the text size to 8 pt. gdpicturePDF.SetTextSize(8); // Set the fill color to gray. gdpicturePDF.SetFillColor(128, 128, 128); // Set the text alignment to center to a variable. var alignCenter = TextAlignment.TextAlignmentCenter; float pageWidth = gdpicturePDF.GetPageWidth(); string multiline = "This Agreement supersedes any prior written or verbal communication or understanding." + "\nWe may change the terms of this Agreement at any time." + "\nAny later version of this document shall supersede all previous versions."; // Add the multiline text to the bottom of the page. gdpicturePDF.DrawTextBox(fontName, 1, 2, pageWidth - 1, 1, alignCenter, alignCenter, multiline);}gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system to the bottom-left corner. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Specify the font type. Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold) For i As Integer = 1 To gdpicturePDF.GetPageCount() ' Select the PDF page. gdpicturePDF.SelectPage(i) ' Set the text size to 8 pt. gdpicturePDF.SetTextSize(8) ' Set the fill color to gray. gdpicturePDF.SetFillColor(128, 128, 128) ' Set the text alignment to center to a variable. Dim alignCenter = TextAlignment.TextAlignmentCenter Dim pageWidth As Single = gdpicturePDF.GetPageWidth() Dim multiline = "This Agreement supersedes any prior written or verbal communication or understanding." & vbLf & "We may change the terms of this Agreement at any time." & vbLf & "Any later version of this document shall supersede all previous versions." ' Add the multiline text to the bottom of the page. gdpicturePDF.DrawTextBox(fontName, 1, 2, pageWidth - 1, 1, alignCenter, alignCenter, multiline) Next gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics
Adding wrapped text to PDFs
To automatically wrap the added text depending on the specified area dimensions, use the DrawWrappedText
method. It accepts the following parameters:
FontResName
— The font to be used.Left
— The distance between the coordinate origin and the left side of the text box.Top
— The distance between the coordinate origin and the top of the text box.Right
— The distance between the coordinate origin and the right side of the text box.Bottom
— The distance between the coordinate origin and the bottom of the text box.HorizontalAlignment
— Specifies horizontal text alignment.Text
— The body of the text.UseFontBBox
— A Boolean value to use the font boundary box when calculating the line height. The font boundary box is an imaginary box that encloses all glyphs from the font, usually as tight as possible. WhenUseFontBBox
is set tofalse
, the line height calculation uses the font’s ascent and descent properties. The ascent property is the distance from the baseline to the highest grid coordinate used to place an outline point. The descent property is the distance from the baseline to the lowest grid coordinate.StartPos
— Counts both as an input and output parameter. As an input, it specifies the index of the first character of the string used as the text body. After using theDrawWrappedText
method, this parameter holds the index of the first character that wasn’t drawn due to the specified area’s boundaries. If the entire text is placed in the specified dimensions, this parameter returns-1
.
The HorizontalAlignment
parameter is defined by the TextAlignment
enumeration, which accepts the following values:
TextAlignmentCenter
— Aligns text to the center.TextAlignmentNear
— Aligns text to the left side in left-to-right writing systems. In right-to-left systems, it’s aligned to the right.TextAlignmentFar
— Aligns text to the right side in left-to-right writing systems. In right-to-left systems, it’s aligned to the left.
To add wrapped text to a PDF file, follow the steps outlined below.
- Create a
GdPicturePDF
object. - Load the PDF file with the
LoadFromFile
method. - Set the origin of the coordinate system with the
SetOrigin
method. This method requires thePdfMeasurement
enumeration, which accepts the following values:
PdfOriginTopLeft
PdfOriginTopRight
PdfOriginBottomRight
PdfOriginBottomLeft
- Set the measurement unit, which is used to specify dimensions.
- Specify which font type to use.
- Set the
StartPos
parameter to0
. - Select the page you want the text to be added to with the
SelectPage
method. - Optional: Set the text size (
12pt
by default). - Optional: Specify the text color (black by default).
Use the StartPos
parameter if you aren’t sure whether the text will fit in the PDF page dimensions. Before adding the final text to a page, you can draw a test text outside the page area and check if the long version of the text fits. If the StartPos
parameter has a value different from -1
, use the short version for this page. The code below demonstrates this situation:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Set the origin of the coordinate system to the bottom-left corner.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);// Set the measurement unit to centimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);// Specify the font type.string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold);// Set the `StartPos` parameter to `0`.int StartPos = 0;string shortMultiline = "This Agreement supersedes any prior written or " + "verbal communication or understanding.";string multiline = "This Agreement supersedes any prior written or " + "verbal communication or understanding. " + "We may change the terms of this Agreement at any time. " + "Any later version of this document shall supersede all previous versions.";for (int i = 1; i <= gdpicturePDF.GetPageCount(); i++){ // Select the PDF page. gdpicturePDF.SelectPage(i); // Set the fill color to gray. gdpicturePDF.SetFillColor(128, 128, 128); float pageWidth = gdpicturePDF.GetPageWidth(); // Add the test text outside the page area. gdpicturePDF.DrawWrappedText(fontName, 4, -1, pageWidth - 4, -2, TextAlignment.TextAlignmentCenter, multiline, false, ref StartPos); // If the full text does not fit, use the short version. if (StartPos != -1) multiline = shortMultiline; StartPos= 0; // Add the final text to the desired location. gdpicturePDF.DrawWrappedText(fontName, 4, 2, pageWidth - 4, 1, TextAlignment.TextAlignmentCenter, multiline, false, ref StartPos);}gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system to the bottom-left corner. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Specify the font type. Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold) ' Set the `StartPos` parameter to `0`. Dim StartPos = 0 Dim shortMultiline = "This Agreement supersedes any prior written or " & "verbal communication or understanding." Dim multiline = "This Agreement supersedes any prior written or " & "verbal communication or understanding. " & "We may change the terms of this Agreement at any time. " & "Any later version of this document shall supersede all previous versions." For i As Integer = 1 To gdpicturePDF.GetPageCount() ' Select the PDF page. gdpicturePDF.SelectPage(i) ' Set the fill color to gray. gdpicturePDF.SetFillColor(128, 128, 128) Dim pageWidth As Single = gdpicturePDF.GetPageWidth() ' Add the test text outside the page area. gdpicturePDF.DrawWrappedText(fontName, 4, -1, pageWidth - 4, -2, TextAlignment.TextAlignmentCenter, multiline, False, StartPos) ' If the full text does not fit, use the short version. If StartPos <> -1 Then multiline = shortMultiline StartPos = 0 ' Add the final text to the desired location. gdpicturePDF.DrawWrappedText(fontName, 4, 2, pageWidth - 4, 1, TextAlignment.TextAlignmentCenter, multiline, False, StartPos) Next gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics
Text settings in PDFs
This section explains how to configure general text settings, such as measurement units and font settings, in a PDF file.
Measurement unit
Some methods require parameters that represent distance values. To specify the units of those parameters, use the SetMeasurementUnit
method, which uses the PdfMeasurementUnit
enumeration as an argument. It accepts the following values:
PdfMeasurementUnitCentimeter
PdfMeasurementUnitMillimeter
PdfMeasurementUnitInch
PdfMeasurementUnitPoint
PdfMeasurementUnitUndefined
Throughout your code, you can change the distance units by calling the SetMeasurementUnit
method with a different unit type.
Font type
To specify the font of the text, use one of the methods below.
AddStandardFont
method. This method requires thePdfStandardFont
enumeration, which accepts the following values:PdfStandardFontCourier
PdfStandardFontCourierBold
PdfStandardFontCourierBoldOblique
PdfStandardFontCourierOblique
PdfStandardFontHelvetica
PdfStandardFontHelveticaBold
PdfStandardFontHelveticaBoldOblique
PdfStandardFontHelveticaOblique
PdfStandardFontSymbol
PdfStandardFontTimesRoman
PdfStandardFontTimesBold
PdfStandardFontTimesBoldItalic
PdfStandardFontTimesItalic
PdfStandardFontZapfDingbats
AddTrueTypeFont
method. It allows loading any standard font format of the Microsoft Windows operating system and accepts the following parameters:FontName
— The name of the font.Bold
— Makes the text bold if set totrue
.Italic
— Makes the text italic if set totrue
.Embedded
— Embeds the text if set totrue
.
Embedded texts significantly increase the file size.
Text size
To specify the text size, use the SetTextSize
method. It takes an integer as its parameter, which is the text size expressed in points (pt).
Text color
To set the text color, use the SetFillColor
method. It accepts one of the following:
- RGB code.
- CMYK code.
- A
Color
object. For more information, refer to theColor Object
topic. - An
ARGB
method. For more information, refer to theARGB Method
topic.