This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/dotnet/pdf-generation/from-scratch.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Create PDF from scratch: Text, images, more | Nutrient .NET SDK

This guide explains how to create a PDF document from scratch.

Download sample

To create a simple PDF form from scratch, follow these steps:

  1. Create a GdPicturePDF object.
  2. Create a new PDF document with the NewPDF method.
  3. Set the measurement unit with the SetMeasurementUnit method. This method takes a member of the PdfMeasurementUnit enumeration as its parameter. For example, to set the unit of measurement to millimeters, call gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter).
  4. Set the origin of the coordinate system with the SetOrigin method. This method takes a member of the PdfOrigin enumeration as its parameter. For example, to set the origin to the top-left corner, call gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft).
  5. Add a new A4-sized page to the document with the NewPage method. This method takes a member of the PdfPageSizes enumeration as its parameter.
  6. 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 the PdfStandardFont enumeration as its parameter.
  7. Add the title in a text box. The SetTextSize method defines the font size in points. The DrawTextBox 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.
  8. 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 rectangle: 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.
  9. 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.
  10. 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.
  11. Optional: Define the maximum number of characters allowed in a form field with the SetFormFieldMaxLen method. This method takes the integer form-field ID and the number of characters as its parameters.
  12. Save the output in a PDF document with the SaveToFile method.

The following example creates a simple PDF form with a title surrounded by a red rectangle and two form fields:

Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.

using GdPicture14;
LicenseManager license = new LicenseManager();
license.RegisterKEY("");
using GdPicturePDF gdpicturePDF = new GdPicturePDF();
// Create a new PDF document.
gdpicturePDF.NewPDF();
// Set the measurement unit and the origin.
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
// 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(@"output.pdf");