FormSource
With FormSource you can add content to a Form object, which will be inserted in the PDF document. This article explains the following topics:
- Creating a FormSource
- FormSource Properties
- Adding Content to a FormSource Object
- Inserting a FormSource into a Document
Creating a FormSource
The FormSource class exposes a default constructor which you can use to create an empty instance.
Example 1: Create FormSource
The properties exposed by the FormSource class are as follows: |Property|Description| |----|----| |Size|Allows getting or setting the size of the form.| |Content|Gets the contents of the form.|
Adding Content to a FormSource Object
The FormSource class inherits from the IContentRootElement interface. This inheritance allows you to use the FixedContentEditor class for filling the content of the form.
Example 2 shows you how you can insert a content into a FormSource object using FixedContentEditor.
Example 2: Add content to a FormSource
Inserting a FormSource into a Document
After generating the FormSource object and filling it with content, you should insert it in the document. The API provides you with convenient approaches that might be useful to easily insert the form in different scenarios.
You can reuse a single FormSource object among the document by setting it to different Form instances.
-
In addition to the ability to fill a form source, the FixedContentEditor allows you to add this form to a container.
Example 3: Add a FormSource to a document using FixedContentEditor
There are several overloads of the DrawForm() method that enables you to specify the size of the form. A FormSource object can be inserted using the methods of RadFixedDocumentEditor. The InsertFormInline() method is described in the RadFixedDocumentEditor topic.
When editing a Block, you can insert a FormSource object directly into it. For more information, check this topic.
Adding SVG into a Document
As of Q3 2024 RadPdfProcessing provides support for SVG (vector graphics image format). The static FormSource.FromSvg method allows the possibility to insert a vector image in the PDF document. The following overloads are publicly available:
Method | Description |
---|---|
FormSource.FromSvg(string xml) | Creates a FormSource object from an SVG file provided as a xml. |
FormSource.FromSvg(byte[] svgData) | Creates a FormSource object from an SVG file provided as a byte[]. |
FormSource.FromSvg(Stream stream) | Creates a FormSource object from an SVG file provided as a stream. |
The following example shows how to insert an SVG image into a FormSource object using FixedContentEditor:
string svgFilePath = "image.svg";
RadFixedDocument fixedDocument = new RadFixedDocument();
FixedContentEditor documentPageEditor = new FixedContentEditor(fixedDocument.Pages.AddPage());
int offset = 10;
documentPageEditor.Position.Translate(offset, offset);
FormSource svgHostForm = FormSource.FromSvg(File.ReadAllBytes(svgFilePath));
documentPageEditor.DrawForm(svgHostForm);
//draw the SVG with its original size
double aspectRatio = svgHostForm.Size.Width / svgHostForm.Size.Height;
//get the aspect ratio from the original SVG size
double desiredSVGWidth = 100;
double calculatedSVGHeight = desiredSVGWidth / aspectRatio;
documentPageEditor.Position.Translate(offset, svgHostForm.Size.Height + offset);
documentPageEditor.DrawForm(svgHostForm, new Size(desiredSVGWidth, calculatedSVGHeight));
//draw the SVG with desired width preserving the aspect ratio
Adding Barcode into a Document
As of Q1 2025 RadPdfProcessing provides support for adding Barcodes (1D and 2D) into a PDF document. This is possible through the static FormSource.FromBarcode and FormSource.From2DBarcode methods. They utilize the Symbology1DType and Symbology2DType enums that represent the different types of 1D and 2D barcode symbologies supported by the barcode model. These are the publicly available overloads:
Method | Description |
---|---|
FormSource.FromBarcode(Symbology1DType symbology, string value) | Creates a FormSource object from a one-dimensional (1D) barcode parameters, with a default Width and Height of 100. |
FormSource.FromBarcode(Symbology1DType symbology, string value, int width, int height) | Creates a FormSource object from a one-dimensional (1D) barcode parameters with custom Width and Height. |
FormSource.FromBarcode(Symbology1DType symbology, string value, int width, int height, bool showText) | Creates a FormSource object from a one-dimensional (1D) barcode parameters with custom Width and Height while specifying whether the text should be shown or not (showText is false by default). |
FormSource.FromBarcode(Symbology1DType symbology, string value, bool showText) | Creates a FormSource object from a one-dimensional (1D) barcode parameters while specifying whether the text should be shown or not (showText is false by default). Width and Height are 100 by default. |
FormSource.From2DBarcode(Symbology2DType symbology, string value) | Creates a FormSource object from a two-dimensional (2D) barcode parameters, with a default Width and Height of 100. |
FormSource.From2DBarcode(Symbology2DType symbology, string value, int width, int height) | Creates a FormSource object from a two-dimensional (2D) barcode parameters, with custom Width and Height. |
The following example shows how to create a Barcode as a FormSource object and insert it in a page using the FixedContentEditor:
RadFixedDocument doc = new RadFixedDocument();
FixedContentEditor documentPageEditor = new FixedContentEditor(doc.Pages.AddPage());
documentPageEditor.Position.Translate(0, 0);
FormSource barcode = FormSource.FromBarcode(Symbology1DType.Code128, "1234567890", width: 100, height: 100);
documentPageEditor.DrawForm(barcode, 100, 100);
documentPageEditor.Position.Translate(120, 0);
FormSource barcodeWithCenterCenterText = FormSource.FromBarcode(Symbology1DType.Code128, "1234567890", showText: true);
documentPageEditor.DrawForm(barcodeWithCenterCenterText, 100, 100);
documentPageEditor.Position.Translate(240, 0);
FormSource swissQr = FormSource.From2DBarcode(Symbology2DType.SwissQR, "1234567890");
documentPageEditor.DrawForm(swissQr, 100, 100);
documentPageEditor.Position.Translate(360, 0);
FormSource qr = FormSource.From2DBarcode(Symbology2DType.QR, "1234567890");
documentPageEditor.DrawForm(qr, 100, 100);