PDF sticky Annotations using C#
The following code snippet also work with Aspose.PDF.Drawing library.
Add Watermark Annotation
A watermark annotation shall be used to represent graphics that shall be printed at a fixed size and position on a page, regardless of the dimensions of the printed page.
You can add Watermark Text using WatermarkAnnotation at a specific position of the PDF page. The opacity of Watermark can also be controlled by using opacity property.
Please check the following code snippet to add WatermarkAnnotation.
//Load a Document
Aspose.PDF.Document doc = new Aspose.PDF.Document("source.pdf");
//Load Page object to add Annotation
Page page = doc.Pages[1];
//Create Annotation
WatermarkAnnotation wa = new WatermarkAnnotation(page, new Aspose.PDF.Rectangle(100, 500, 400, 600));
//Add annotaiton into Annotation collection of Page
page.Annotations.Add(wa);
//Create TextState for Font settings
Aspose.PDF.Text.TextState ts = new Aspose.PDF.Text.TextState();
ts.ForegroundColor = Aspose.PDF.Color.Blue;
ts.Font = FontRepository.FindFont("Times New Roman");
ts.FontSize = 32;
//Set opacity level of Annotaiton Text
wa.Opacity = 0.5;
//Add Text in Annotation
wa.SetTextAndState(new string[] { "HELLO", "Line 1", "Line 2" }, ts);
//Save the Document
doc.Save("Output.pdf");
Add Reference of a single Image multiple times in a PDF Document
Sometimes we have a requirement to use same image multiple times in a PDF document. Adding a new instance increases the resultant PDF document. We have added a new method XImageCollection.Add(XImage) in Aspose.PDF for .NET 17.1.0. This method allows to add reference to the same PDF object as original image that optimize the PDF Document size.
Aspose.PDF.Rectangle imageRectangle = new Aspose.PDF.Rectangle(0, 0, 30, 15);
using (Aspose.PDF.Document document = new Aspose.PDF.Document("input.pdf"))
{
using (var imageStream = File.Open("icon.png", FileMode.Open))
{
XImage image = null;
foreach (Page page in document.Pages)
{
WatermarkAnnotation annotation = new WatermarkAnnotation(page, page.Rect);
XForm form = annotation.Appearance["N"];
form.BBox = page.Rect;
string name;
if (image == null)
{
name = form.Resources.Images.Add(imageStream);
image = form.Resources.Images[name];
}
else
{
name = form.Resources.Images.Add(image);
}
form.Contents.Add(new Operator.GSave());
form.Contents.Add(new Operator.ConcatenateMatrix(new Aspose.PDF.Matrix(imageRectangle.Width, 0, 0, imageRectangle.Height, 0, 0)));
form.Contents.Add(new Operator.Do(name));
form.Contents.Add(new Operator.GRestore());
page.Annotations.Add(annotation, false);
imageRectangle = new Aspose.PDF.Rectangle(0, 0, imageRectangle.Width * 1.01, imageRectangle.Height * 1.01);
}
}
document.Save("output.pdf");
}