This module uses the New DevExpress PDF Document API (PdfDocument) 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 Clear Annotations and Save to remove all annotations.
using DevExpress.Docs.Pdf;
Stream CreateLink(Stream documentStream, string searchText, System.Drawing.Color color) {
using var doc = new PdfDocument(documentStream);
foreach(var result in doc.FindText(searchText)) {
foreach(var match in result.Matches) {
var bounds = match.MatchFragments.Select(f => f.Rectangle.BoundingBox)
.Aggregate(RectangleF.Union);
var link = new LinkAnnotation(bounds) {
Action = new UriAction { Uri = "https://www.devexpress.com" },
Color = PdfColor.FromRgb(color.R, color.G, color.B),
BorderStyle = new AnnotationBorderStyle { Width = 2, Style = BorderStyle.Dashed }
};
doc.Pages[result.PageIndex].Annotations.Add(link);
}
}
var outputStream = new MemoryStream();
doc.Save(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream CreateFreeTextAnnotation(Stream documentStream, string text, System.Drawing.Color color) {
using var doc = new PdfDocument(documentStream);
var page = doc.Pages[0];
page.Annotations.Add(new FreeTextAnnotation(new RectangleF(450, 20, 130, 100)) {
Content = text,
Title = "DX Demo",
Color = PdfColor.FromRgb(color.R, color.G, color.B),
CreationDate = DateTimeOffset.UtcNow
});
var outputStream = new MemoryStream();
doc.Save(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream CreateRubberStamp(Stream documentStream, string iconName) {
using var doc = new PdfDocument(documentStream);
var page = doc.Pages[0];
page.Annotations.Add(new RubberStampAnnotation(
new RectangleF(350, 80, 230, 55),
RubberStampAnnotationIconName.FromName(iconName)) {
Title = "DX Demo",
CreationDate = DateTimeOffset.UtcNow
});
var outputStream = new MemoryStream();
doc.Save(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream CreateStickyNote(Stream documentStream, string iconName, System.Drawing.Color color) {
using var doc = new PdfDocument(documentStream);
var page = doc.Pages[0];
page.Annotations.Add(new TextAnnotation(new RectangleF(450, 150, 20, 20)) {
IconName = Enum.Parse<TextAnnotationIconName>(iconName),
Color = PdfColor.FromRgb(color.R, color.G, color.B),
Content = "This is a Sticky Note",
Title = "DX Demo",
CreationDate = DateTimeOffset.UtcNow
});
var outputStream = new MemoryStream();
doc.Save(outputStream);
outputStream.Position = 0;
return outputStream;
}
Stream ClearAllAnnotations(Stream documentStream) {
using var doc = new PdfDocument(documentStream);
foreach(var page in doc.Pages)
page.Annotations.Clear();
var outputStream = new MemoryStream();
doc.Save(outputStream);
outputStream.Position = 0;
return outputStream;
}