PDF Document API Library (New): Add Content to Pages
- 10 minutes to read
This help topic describes how to add content to PDF pages and explains the coordinate system used to position content.
Basic API: Fragment Collection and Add Methods
To add content to a Page, first create a content fragment: text, image, shape, or form. Add the object to the FragmentCollection available via the Page.Fragments property.
The Page class also ships with a set of methods that allows you to add a specific content type to a PDF page:
- AddTextFragment
- Adds a text fragment to a PDF page.
- AddImageFragment
- Adds an image fragment to a PDF page.
- AddPathFragment
- Adds a path fragment (custom shapes and lines) to a PDF page.
- AddFormFragment
- Adds a form fragment (reusable template) to a PDF page.
Coordinate System and Units
When adding content to a PDF page, specify location and size. The table below describes the coordinate system and units used by the DevExpress PDF Document API library:
| Parameter | Value |
|---|---|
| Coordinate System Type | Page |
| Coordinate System Origin | Bottom-left corner of the page |
| Units | Points (1 inch = 72 points) |
| Rotation Angle | Degrees (positive values indicate clockwise rotation) |
Add Text to a PDF Page
Add a Text Block
Use the Page.AddTextFragment method to add a text line to a PDF page. You can pass a simple string to this method to add unformatted text, or you can use the TextFragment object to specify formatting options such as font, size, and color. You can pass both right-to-left and left-to-right text to this method. The text direction is determined automatically.
The following code snippet adds a single line of text to a PDF document page:

using DevExpress.Docs.Pdf;
using DevExpress.Drawing.Printing;
using System.Drawing;
using DevExpress.Drawing;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument()) {
var page = pdfDocument.Pages.Add(DXPaperKind.A4);
// Create a text fragment with formatting.
var text = new TextFragment()
{
Text = "The PDF Document API",
Location = new PointF(10, 740),
Font = new DXFont("Courier New", 11, DXFontStyle.Regular),
};
page.AddTextFragment(text);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Add Multiline Text With Wrapping
To add a multiline text with specific alignment, you can use the ParagraphFragment object. This object allows you to manage line breaks.
The following code snippet creates a paragraph and adds it to a PDF page:

using DevExpress.Docs.Pdf;
using DevExpress.Drawing;
using DevExpress.Drawing.Printing;
using System.Drawing;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument()) {
var page = pdfDocument.Pages.Add(DXPaperKind.A4);
// Create a paragraph fragment.
var multiLine = new ParagraphFragment(){
Text = "The PDF Document API is a non-visual .NET library that allows you to " +
"generate, convert, merge, split, edit, password-protect, and digitally " +
"sign PDF files.",
Location = new PointF(10, 740),
Font = new DXFont("Courier New", 11, DXFontStyle.Regular),
Width = 500,
Height = 500,
StringFormat = new DXStringFormat() {
Alignment = DXStringAlignment.Center,
LineAlignment = DXStringAlignment.Near,
},
};
// Add the paragraph to the PDF page.
page.Fragments.Add(multiLine);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Add Images to a PDF Page
Use the ImageFragment object to add images to a PDF page. This object allows you to specify the location and size of the image on the page.
The following image formats are supported:
- BMP
- JPEG
- PNG
- EMF
- EMF+
- TIFF
- GIF
- SVG
The following code snippet adds an image to a page:

using DevExpress.Docs.Pdf;
using DevExpress.Drawing;
using DevExpress.Drawing.Printing;
using System.Drawing;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument())
{
var page = pdfDocument.Pages.Add(DXPaperKind.A4);
// Create an image fragment.
var image = new ImageFragment
{
Image = DXImage.FromStream(new FileStream("DevExpress.png", FileMode.Open, FileAccess.Read)),
Location = new PointF(100, 100),
SkewX = 20,
};
// Add the image to the page.
page.Fragments.Add(image);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Add Shapes to a PDF Page
The PathFragment class allows you to define straight lines and curves.
The following code snippet adds a triangle and Bezier curve to the page:

using DevExpress.Docs.Pdf;
using DevExpress.Drawing;
using DevExpress.Drawing.Printing;
using System.Drawing;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument())
{
var page = pdfDocument.Pages.Add(DXPaperKind.A4);
// Create a triangle path.
GraphicsPath triangle = new GraphicsPath(new PointF(410, 490));
triangle.AppendLineSegment(new PointF(480, 460));
triangle.AppendLineSegment(new PointF(550, 490));
triangle.Closed = true;
PathFragment triangleFragment = new PathFragment() {
Paths = { triangle },
Pen = new DXPen(Color.Black, 1),
Brush = new DXSolidBrush(Color.LightGray),
Action = PathShapeAction.FillAndStroke
};
page.Fragments.Add(triangleFragment);
// Create a Bezier curve path.
GraphicsPath bezier = new GraphicsPath(new PointF(210, 375));
bezier.AppendBezierSegment(new PointF(210, 320),
new PointF(350, 320), new PointF(350, 375));
bezier.AppendBezierSegment(new PointF(350, 430),
new PointF(210, 430), new PointF(210, 375));
PathFragment bezierFragment = new PathFragment()
{
Paths = { bezier },
Pen = new DXPen(Color.Black, 1),
Brush = new DXSolidBrush(Color.Red),
Action = PathShapeAction.FillAndStroke
};
page.Fragments.Add(bezierFragment);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Call the PathFragment.Rectangle method to create a rectangle path fragment. You can specify the rectangle’s position, size, fill, and outline.
The following code snippet creates a rectangle and adds it to a PDF page:

using DevExpress.Docs.Pdf;
using DevExpress.Drawing;
using DevExpress.Drawing.Printing;
using System.Drawing;
using System.IO;
using (PdfDocument pdfDocument = new PdfDocument()) {
var page = pdfDocument.Pages.Add(DXPaperKind.A4);
// Create a rectangle path fragment.
var rectangle = PathFragment.CreateRectanglePathFragment(new RectangleF(100, 100, 200, 200));
rectangle.Brush = new DXSolidBrush(Color.Red);
rectangle.Pen = new DXPen(Color.Blue, 5);
page.Fragments.Add(rectangle);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
}
Add a Reusable Template (PDF Form XObject) to a Page
The PDF Form XObject is a reusable layout where the same structure and content are repeated each time it is used. You can create a form and add it to different pages of a PDF document.
Create a new FormTemplate object and define its bounds. The template can contain text, images, and graphics. Make sure that the content of the form template fits within the defined bounds, as the form is clipped to these dimensions when added to a PDF page.
The following code snippet creates a form template, adds text and a line to it:
using DevExpress.Docs.Pdf;
using DevExpress.Drawing;
using DevExpress.Drawing.Printing;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using PdfDocument pdfDocument = new PdfDocument();
var page = pdfDocument.Pages.Add(DXPaperKind.A4);
// Create a form template.
var headerTemplate = new FormTemplate();
headerTemplate.Bounds = new RectangleF(0, 0, ((float)page.Width), 150);
// Add text to the form template.
headerTemplate.AddTextFragment("Company Name: DevExpress", 50, 160);
headerTemplate.AddTextFragment("Invoice Date: " + DateTime.Now.ToShortDateString(), 50, 120);
headerTemplate.AddTextFragment("Invoice Number: 1001", 50, 140);
// Create a line.
GraphicsPath headerLine = new GraphicsPath(new PointF(0, 110));
headerLine.AppendLineSegment(new PointF((float)headerTemplate.Bounds.Width, 110));
var linePath = new GraphicsPath[] { headerLine, };
var lineFragment = new PathFragment();
lineFragment.Brush = new DXSolidBrush(Color.LightGray);
lineFragment.Paths = linePath;
// Add the line to the form template.
headerTemplate.Fragments.Add(lineFragment);
Create a FormFragment object based on the generated FormTemplate object. You can specify multiple position options for the form fragment, such as the location of the form on the page, its size, rotation angle, scaling, and skewing.
Call the Page.FragmentCollection.Add() method to add the form to a PDF page.
The following code snippet adds the form created earlier to a PDF page:

// Continue from the previous example.
// Create a form fragment based on the form template.
var templateForm = new FormFragment(headerTemplate);
templateForm.Location = new PointF(0, (float)page.Height - 160);
page.Fragments.Add(templateForm);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
You can create a form template once and reuse it on multiple pages. Create multiple FormFragment objects based on the same FormTemplate object, and add them to different pages of a PDF document. You can also specify different position options for each form fragment.
The following code snippet adds the same form to two different pages at different locations:
// Continue from the previous example.
var secondPage = pdfDocument.Pages.Add(DXPaperKind.A4);
// Create the first form fragment and add it to the first page.
var formFragment = new FormFragment(headerTemplate);
formFragment.Location = new PointF(50, (float)page.Height - 160);
page.AddFormFragment(formFragment);
// Create the second form fragment and add it to the second page.
var formFragment2 = new FormFragment(headerTemplate);
formFragment2.Location = new PointF(100, (float)secondPage.Height - 200);
secondPage.AddFormFragment(formFragment2);
pdfDocument.Save(new FileStream("Result.pdf", FileMode.Create, FileAccess.Write));
Next Steps
Refer to the following help topics for more information on how to use the PDF Document API library: