Migrate to New PDF Document API
- 6 minutes to read
PDF Document API* introduces new and improved API in v26.1. Both versions are available side by side in the following namespaces:
- DevExpress.Docs.Pdf — new API
- DevExpress.Pdf — backward-compatibility API
This topic helps you migrate existing PDF Document API code from previous types to the new API. It is useful in the following cases
- You have an existing application and expect long-term support/development.
- You are starting a new project and want to reuse code from an existing codebase.
To migrate from the DevExpress.Pdf namespace to DevExpress.Docs.Pdf, follow the steps below:
Reference the
DevExpress.Docs.PdfNuGet package. You can run the following command:dotnet add package DevExpress.Docs.PdfIf you do not use any Office File API libraries other than PDF Document API, remove the
DevExpress.Document.ProcessorNuGet package reference.Change the namespace reference from
DevExpress.Pdftousing DevExpress.Docs.Pdf:Replace types with their new versions (see the table below).
- Test the PDF Document API output (open/save documents, edit document content, etc).
Basic Migration Example
Previous API
using DevExpress.Pdf;
using System.Linq;
using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
// Load a document
processor.LoadDocument("..\\..\\Result.pdf");
// Access the first page
PdfPageFacade pageFacade = processor.DocumentFacade.Pages[0];
// Obtain all free-text annotations
var freeTextAnnotations = pageFacade.Annotations.Where
(annotation => annotation.Type == PdfAnnotationType.FreeText);
foreach (PdfFreeTextAnnotationFacade freeText in freeTextAnnotations) {
// Change border parameters and text justification
freeText.InteriorColor = new PdfRGBColor(0.36, 0.54, 0.66);
freeText.BorderWidth = 0.5;
freeText.TextJustification = PdfTextJustification.Centered;
}
// Generate a watermark
string fontName = "Arial Black";
int fontSize = 12;
PdfStringFormat stringFormat = PdfStringFormat.GenericTypographic;
stringFormat.Alignment = PdfStringAlignment.Center;
stringFormat.LineAlignment = PdfStringAlignment.Center;
using (DXSolidBrush brush = new DXSolidBrush(Color.FromArgb(63,Color.Black))) {
DXFont font = new DXFont(fontName,fontSize);
foreach (var page in processor.Document.Pages) {
var watermarkSize = page.CropBox.Width * 0.75;
using (PdfGraphics graphics = processor.CreateGraphicsPageSystem()) {
SizeF stringSize = graphics.MeasureString(text,font);
float scale = (float)(watermarkSize / (double)stringSize.Width);
graphics.TranslateTransform((float)(page.CropBox.Width * 0.5),(float)(page.CropBox.Height * 0.5));
graphics.RotateTransform((float)45.0);
graphics.TranslateTransform((float)(-stringSize.Width * scale * 0.5),(float)(-stringSize.Height * scale * 0.5));
DXFont actualFont = new DXFont(fontName,fontSize * scale);
RectangleF rect = new RectangleF(0,0,stringSize.Width * scale,stringSize.Height * scale);
graphics.DrawString(text,actualFont,brush,rect,stringFormat);
graphics.AddToPageForeground(page);
}
}
}
// Save the result
processor.SaveDocument("..\\..\\Result_1.pdf");
}
New API
using DevExpress.Docs.Pdf;
using System.Drawing;
using (PdfDocument document = new PdfDocument(File.OpenRead("Demo.pdf"), new LoadOptions())) {
// Access the first page
Page page = document.Pages[0];
// Obtain all free-text annotations
var freeTextAnnotations = page.Annotations.Where
(annotation => annotation is FreeTextAnnotation);
foreach (FreeTextAnnotation freeText in freeTextAnnotations) {
// Change border parameters and text justification
freeText.Color = PdfColor.DarkSlateGray;
freeText.Border = new AnnotationBorder{ Width = 0.5 };
freeText.TextJustification = TextJustification.Centered;
}
// Generate a watermark
TextFragment textFragment = new() {
Text = "WATERMARK",
Location = new PointF(150, 80),
Font = new("Arial"),
FontSize = 100,
RotationAngle = 45,
ForegroundFill = new SolidFill(PdfColor.Red,0.2)
};
for (int i = 0; i < document.Pages.Count; i++) {
document.Pages[i].Fragments.Add(textFragment);
}
// Save the result
document.Save(new FileStream("Demo1.pdf", FileMode.Create, FileAccess.Write));
}
PDF Type Equivalents
The table below lists members/types available in the DevExpress.Pdf namespace and their DevExpress.Docs.Pdf counterparts.