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
Used Methods
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.
Adding Text to Images
To add simple, one-line text to an image file, use the DrawText
method. It requires the parameters outlined below.
-
ImageID
— The GdPicture image ID of the loaded image. -
Text
— The body of the text. -
DstLeft
— The horizontal distance between the coordinate origin and the upper-left corner of the text (in pixels). -
DstTop
— The vertical distance between the coordinate origin and the upper-left corner of the text (in pixels). -
FontSize
— The font size in units specified by theFontSetUnit
method. The possible values are the following:-
UnitPoint
-
UnitInch
-
UnitPixel
-
UnitDocument
(1/300 inch)
-
-
FontStyle
— The font size set by theFontStyle
enumeration. The possible values are the following:-
Regular
-
Bold
-
Italic
-
Underline
-
Strikeout
-
-
TextColor
— The text color specified by theARGB
method or theColor
object. -
FontName
—The name of the font used. -
AntiAlias
— Applies the antialiasing algorithm if set totrue
.
To add text to an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint); // Add the text to the image. gdpictureImaging.DrawText(imageID, "GdPicture.NET", 5, 5, 40, FontStyle.Regular, gdpictureImaging.ARGB(100, 128, 128, 128), "Arial", true); gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text to the image. gdpictureImaging.DrawText(imageID, "GdPicture.NET", 5, 5, 40, FontStyle.Regular, gdpictureImaging.ARGB(100, 128, 128, 128), "Arial", True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Used Methods
Adding Gradient Text to Images
To add text with a linear gradient to an image, use the DrawTextGradient
method. This method uses the same parameters as the DrawText
method, but the TextColor
parameter is replaced by the StartColor
and EndColor
parameters. These parameters accept either the ARGB
method or the Color
object.
To add gradient text to an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint); // Add the text with a black-to-white gradient to the image. gdpictureImaging.DrawTextGradient(imageID, "GdPicture.NET", 100, 50, gdpictureImaging.ARGB(255, 0, 0, 0), gdpictureImaging.ARGB(255, 255, 255, 255), 40, FontStyle.Regular, "Arial", true); gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text with a black-to-white gradient to the image. gdpictureImaging.DrawTextGradient(imageID, "GdPicture.NET", 100, 50, gdpictureImaging.ARGB(255, 0, 0, 0), gdpictureImaging.ARGB(255, 255, 255, 255), 40, FontStyle.Regular, "Arial", True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Used Methods
Adding Textured Text
To add textured text to an image, use one of the following methods:
-
DrawTextTextureFromFile
— Texture source as a file. -
DrawTextTextureFromGdPictureImage
— Texture source as a GdPicture image.
These methods use the same parameters as the DrawText
method, but the TextColor
parameter is replaced by the following:
-
TextureFilePath
in theDrawTextTextureFromFile
. This parameter specifies the path to the file that’s used as the texture of the text. -
ImageTexture
in theDrawTextTextureFromGdPictureImage
. This parameter specifies the image ID of the file loaded in your project.
To add textured text to an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint); // Add the text with a black-to-white gradient to the image. gdpictureImaging.DrawTextTextureFromFile(imageID, @"C:\temp\texture.jpg", 100, 50, 40, FontStyle.Regular, "Arial", true); gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text with a black-to-white gradient to the image. gdpictureImaging.DrawTextTextureFromFile(imageID, "C:\temp\texture.jpg", 100, 50, 40, FontStyle.Regular, "Arial", true) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Used Methods
Related Topics
Adding Rotated Text to Images
To add text rotated at an angle to an image, use the DrawRotatedText
method. It requires the parameters outlined below.
-
ImageID
— The GdPicture image ID of the loaded image. -
Angle
— The clockwise angle at which the text is rotated (in degrees). -
Text
— The body of the text. -
DstLeft
— The horizontal distance between the coordinate origin and the upper-left corner of the text (in pixels). -
DstTop
— The vertical distance between the coordinate origin and the upper-left corner of the text (in pixels). -
FontSize
— The font size in units specified by theFontSetUnit
method. The possible values are the following:-
UnitPoint
-
UnitInch
-
UnitPixel
-
UnitDocument
(1/300 inch)
-
-
FontStyle
— The font size set by theFontStyle
enumeration. The possible values are the following:-
Regular
-
Bold
-
Italic
-
Underline
-
Strikeout
-
-
TextColor
— The text color specified by theARGB
method or theColor
object. -
FontName
— The name of the font used. -
AntiAlias
— Applies the antialiasing algorithm if set totrue
.
To add text to an image rotated at a 90-degree angle, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint); // Add the text to the image rotated at 90 degrees clockwise. gdpictureImaging.DrawRotatedText(imageID, 90, "GdPicture.NET", 100, 50, 40, FontStyle.Regular, gdpictureImaging.ARGB(100, 128, 128, 128), "Arial", true); gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text to the image rotated at 90 degrees clockwise. gdpictureImaging.DrawRotatedText(imageID, 90, "GdPicture.NET", 100, 50, 40, FontStyle.Regular, gdpictureImaging.ARGB(100, 128, 128, 128), "Arial", True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Used Methods
Related Topics
Adding Text with a Background to Images
To add text with a background to an image, use the following methods:
-
DrawTextBackColor
adds simple text with a background. -
DrawRotatedTextBackColor
adds simple text with a background at a specified clockwise angle (in degrees).
Both of these methods accept the same parameters as the DrawText
and DrawRotatedText
methods, respectively. They also accept the BackColor
parameter, which specifies the color of the background. This parameter is declared after the TextColor
parameter.
To add white text with a black background (semitransparent) rotated at a 90-degree angle, use the following method:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint); // Declare a `Color` object for the text font. Color textColor = Color.White; // Add the text to the image rotated at 90 degrees clockwise with a semitransparent black background. gdpictureImaging.DrawRotatedTextBackColor(imageID, 90, "GdPicture.NET", 100, 50, 40, FontStyle.Regular, textColor, gdpictureImaging.ARGB(100, 0, 0, 0), "Arial", true); gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Declare a `Color` object for the text font. Dim textColor As Color = Color.White ' Add the text to the image rotated at 90 degrees clockwise with a semitransparent black background. gdpictureImaging.DrawRotatedTextBackColor(imageID, 90, "GdPicture.NET", 100, 50, 40, FontStyle.Regular, textColor, gdpictureImaging.ARGB(100, 0, 0, 0), "Arial", True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Used Methods
Related Topics
Adding Text Boxes to Images
To add a text box to an image, use the DrawTextBox
method. It requires the parameters outlined below.
-
ImageID
— The GdPicture image ID of the loaded image. -
Text
— The body of the text box. -
Left
— The horizontal distance between the coordinate origin and the upper-left corner of the text box (in pixels). -
Top
— The vertical distance between the coordinate origin and the upper-left corner of the text box (in pixels). -
Width
— The width of the text box (in pixels). -
Height
— The height of the text box (in pixels). -
FontSize
— The font size in units specified by theFontSetUnit
method. The possible values are the following:-
UnitPoint
-
UnitInch
-
UnitPixel
-
UnitDocument
(1/300 inch)
-
-
Alignment
— The horizontal text alignment specified by theTextAlignment
enumeration. The possible values are the following:-
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.
-
-
FontStyle
— The font size set by theFontStyle
enumeration. The possible values are the following:-
Regular
-
Bold
-
Italic
-
Underline
-
Strikeout
-
-
TextColor
— The text color specified by theARGB
method or theColor
object. -
FontName
— The name of the font used. -
DrawBox
— The Boolean value for drawing the borders of the text box. -
AntiAlias
— The Boolean value for applying the antialiasing algorithm.
To add a text box to an image, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint); // Add the text to the image rotated at 90 degrees clockwise. gdpictureImaging.DrawTextBox(imageID, "GdPicture.NET", 100, 50, 300, 200, 40, TextAlignment.TextAlignmentCenter, FontStyle.Regular, gdImage.ARGB(100, 128, 128, 128), "Arial", true, true); gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the font size unit to points. gdpictureImaging.FontSetUnit(UnitMode.UnitPoint) ' Add the text to the image rotated at 90 degrees clockwise. gdpictureImaging.DrawTextBox(imageID, "GdPicture.NET", 100, 50, 300, 200, 40, TextAlignment.TextAlignmentCenter, FontStyle.Regular, gdImage.ARGB(100, 128, 128, 128), "Arial", True, True) gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using