This module uses the DevExpress PDF Document API (PdfDocumentProcessor) to create and manage PDF annotations. You can process the predefined sample file or supply your own document. To do the latter, select Upload a File in the file selection drop-down menu.
Use the dropdown button to select the annotation type: Free Text generates text comments, Link adds clickable links to annotated text, Stamp adds predefined stamp icons, and Sticky Note adds sticky notes. Configure annotation parameters and set the annotation color if available.
Click the button to create the annotation and download the result. Use Flatten Annotations and Save to merge annotations with the document content or Clear Annotations and Save to remove all annotations.
using DevExpress.Pdf;
IReadOnlyList<PdfRectangle> FindText(PdfDocumentProcessor processor, string searchText) {
var search = processor.FindText(
searchText,
new PdfTextSearchParameters() {
CaseSensitive = false,
WholeWords = false
});
if(search.Status == PdfTextSearchStatus.Found)
return search.Rectangles.Select(x => x.BoundingRectangle).ToList();
else
throw new InvalidOperationException();
}
Stream CreateLink(Stream documentStream, string searchText, System.Drawing.Color annotationColor) {
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(documentStream);
var page = processor.DocumentFacade.Pages[0];
var textRectangles = FindText(processor, searchText);
foreach (var rectangle in textRectangles) {
var linkAnnotation = page.AddLinkAnnotation(rectangle, "https://www.devexpress.com");
linkAnnotation.Color = new PdfRGBColor(annotationColor.R / 255.0, annotationColor.G / 255.0, annotationColor.B / 255.0);
linkAnnotation.BorderWidth = 2;
linkAnnotation.BorderStyle = PdfBorderStyle.Dash;
}
var outputStream = new MemoryStream();
processor.SaveDocument(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream CreateFreeTextAnnotation(MemoryStream documentStream, string text, System.Drawing.Color annotationColor) {
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(documentStream);
var page = processor.DocumentFacade.Pages[0];
var freeTextAnnotation = page.AddFreeTextAnnotation(new PdfRectangle(600, 10, 700, 110), text);
freeTextAnnotation.Author = "DX Demo";
freeTextAnnotation.Color = new PdfRGBColor(annotationColor.R / 255.0, annotationColor.G / 255.0, annotationColor.B / 255.0);
freeTextAnnotation.CreationDate = DateTime.UtcNow;
var outputStream = new MemoryStream();
processor.SaveDocument(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream CreateRubberStamp(MemoryStream documentStream, string iconName) {
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(documentStream);
var page = processor.DocumentFacade.Pages[0];
var stamp = page.AddRubberStampAnnotation(new PdfPoint(600, 100), iconName);
stamp.Author = "DX Demo";
stamp.CreationDate = DateTime.UtcNow;
var outputStream = new MemoryStream();
processor.SaveDocument(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream CreateStickyNote(MemoryStream documentStream, string iconName, System.Drawing.Color annotationColor) {
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(documentStream);
var page = processor.DocumentFacade.Pages[0];
var stickyNote = page.AddTextAnnotation(new PdfPoint(600, 100), iconName);
stickyNote.Author = "DX Demo";
stickyNote.Color = new PdfRGBColor(annotationColor.R / 255.0, annotationColor.G / 255.0, annotationColor.B / 255.0);
stickyNote.CreationDate = DateTime.UtcNow;
var outputStream = new MemoryStream();
processor.SaveDocument(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream FlattenAnnotations(MemoryStream documentStream) {
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(documentStream);
processor.DocumentFacade.FlattenAnnotations();
var outputStream = new MemoryStream();
processor.SaveDocument(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream ClearAnnotations(MemoryStream documentStream) {
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(documentStream);
var annotations = processor.DocumentFacade.Pages.SelectMany(p => p.Annotations).ToList();
foreach(var annotation in annotations)
annotation.Remove();
var outputStream = new MemoryStream();
processor.SaveDocument(outputStream);
outputStream.Position = 0;
return outputStream;
}