Create PDFs from Scratch in C#
This guide explains how to create a PDF document from scratch.
To create a simple PDF form from scratch, follow these steps:
-
Create a
GdPicturePDF
object. -
Set the measurement unit with the
SetMeasurementUnit
method. This method takes a member of thePdfMeasurementUnit
enumeration as its parameter. For example, to set the unit of measurement to millimeters, callgdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
. -
Set the origin of the coordinate system with the
SetOrigin
method. This method takes a member of thePdfOrigin
enumeration as its parameter. For example, to set the origin to the top-left corner, callgdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
. -
Create a new PDF document with the
NewPDF
method. -
Add a new A4-sized page to the document with the
NewPage
method. This method takes a member of thePdfPageSizes
enumeration as its parameter. -
Set the font types to be used in the PDF document with the
AddStandardFont
method and store them in variables. This method takes a member of thePdfStandardFont
enumeration as its parameter. -
Add the title in a text box. The
SetTextSize
method defines the font size in points. TheDrawTextBox
method adds the text box and takes the following parameters:-
The variable for the font type.
-
The coordinates of the text box. These coordinates are relative to the origin and measurement unit defined above.
-
Two parameters, each of which is a member of the
TextAlignment
enumeration, that define the horizontal and vertical alignment of the text within the text box. -
The text to be added. You can define single-line or multiline text.
-
-
Draw a red rectangle around the title with the following methods:
-
The
SetLineColor
method defines the color of the line using the RGB standard. -
The
SetLineWidth
method defines the width of the line using the measurement unit defined above. -
The
DrawRectangle
method defines the parameters of the text box: the coordinates of the closest point to the origin, the width and the height, and Boolean values defining whether to draw the rectangle with fill and stroke.
-
-
Add the form field descriptions with the
DrawText
method. This method takes the following parameters:-
The variable for the font type.
-
The coordinates where to add the text.
-
The text to be added. You can define single-line or multiline text.
-
-
Add the form fields with the
AddTextFormField
method. This method takes the following parameters:-
The coordinates of the form field’s closest point to the origin. These coordinates are relative to the origin and measurement unit defined above.
-
The width and the height of the form field using the measurement unit defined above.
-
The field name. This is used for identifying the field programmatically, and it isn’t displayed in the document.
-
The default text in the form field.
-
A Boolean value defining whether the form field is multiline.
-
The variable for the font type.
-
The font size in points.
-
The color of the text using the RGB standard.
-
-
Optional: Define the maximum number of characters allowed in a form field with the
SetFormFieldMaxLen
method. This method takes the field name and the number of characters as its parameters. -
Save the output in a PDF document with the
SaveToFile
method.
The example below creates a simple PDF form with a title surrounded by a red rectangle and two form fields:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); // Set the measurement unit and the origin. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter); gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft); // Create a new PDF document. gdpicturePDF.NewPDF(); // Add a new A4-sized page to the document. gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4); // Set the fonts to be used in the PDF document. string fontHelvetica = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica); string fontHelveticaBold = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold); // Add the title. gdpicturePDF.SetTextSize(30); gdpicturePDF.DrawTextBox(fontHelveticaBold, 10, 5, 200, 25, TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, "Dinner Signup Form"); // Draw a red rectangle around the title. gdpicturePDF.SetLineColor(255, 0, 0); gdpicturePDF.SetLineWidth(2); gdpicturePDF.DrawRectangle(10, 5, 190, 20, false, true); // Add the form field descriptions. gdpicturePDF.SetTextSize(10); gdpicturePDF.DrawText(fontHelvetica, 15, 40, "Name"); gdpicturePDF.DrawText(fontHelvetica, 15, 50, "Dietary requirements"); // Add the form fields. int fieldNameId = gdpicturePDF.AddTextFormField(60, 35, 90, 8, "Name", "Enter your name", false, fontHelvetica, 8, 0, 0, 0); gdpicturePDF.SetFormFieldMaxLen(fieldNameId, 50); int fieldDietaryRequirementsId = gdpicturePDF.AddTextFormField(60, 45, 90, 30, "Dietary requirements", "Enter your dietary requirements", true, fontHelvetica, 8, 0, 0, 0); gdpicturePDF.SetFormFieldMaxLen(fieldDietaryRequirementsId, 500); // Save the PDF document. gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() ' Set the measurement unit and the origin. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter) gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Create a new PDF document. gdpicturePDF.NewPDF() ' Add a new A4-sized page to the document. gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) ' Set the fonts to be used in the PDF document. Dim fontHelvetica As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica) Dim fontHelveticaBold As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold) ' Add the title. gdpicturePDF.SetTextSize(30) gdpicturePDF.DrawTextBox(fontHelveticaBold, 10, 5, 200, 25, TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, "Dinner Signup Form") ' Draw a red rectangle around the title. gdpicturePDF.SetLineColor(255, 0, 0) gdpicturePDF.SetLineWidth(2) gdpicturePDF.DrawRectangle(10, 5, 190, 20, False, True) ' Add the form field descriptions. gdpicturePDF.SetTextSize(10) gdpicturePDF.DrawText(fontHelvetica, 15, 40, "Name") gdpicturePDF.DrawText(fontHelvetica, 15, 50, "Dietary requirements") ' Add the form fields. Dim fieldNameId As Integer = gdpicturePDF.AddTextFormField(60, 35, 90, 8, "Name", "Enter your name", False, fontHelvetica, 8, 0, 0, 0) gdpicturePDF.SetFormFieldMaxLen(fieldNameId, 50) Dim fieldDietaryRequirementsId As Integer = gdpicturePDF.AddTextFormField(60, 45, 90, 30, "Dietary requirements", "Enter your dietary requirements", True, fontHelvetica, 8, 0, 0, 0) gdpicturePDF.SetFormFieldMaxLen(fieldDietaryRequirementsId, 500) ' Save the PDF document. gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using