This sample shows how to create PDF annotations.
PDFsharp supports the creation of the following annotations:
- Text annotations
- Link annotations
- Rubber stamp annotations
PDF Output File
See the PDF file created by this sample:
output.pdf (3 kB)
Note: the red rubber stamp shows localized resources of Adobe Reader or placeholder if the resource cannot be found
Screen Shots
Here is a sample screen shot taken on a German Windows version:
Note: the red rubber stamp shows the German text "Geheimsache" instead of "Top Secret" because localized resources of Adobe Reader are shown here
Source Code
The first text annotation:
01.
02.
PdfTextAnnotation textAnnot =
new
PdfTextAnnotation();
03.
textAnnot.Title =
"This is the title"
;
04.
textAnnot.Subject =
"This is the subject"
;
05.
textAnnot.Contents =
"This is the contents of the annotation.\rThis is the 2nd line."
;
06.
textAnnot.Icon = PdfTextAnnotationIcon.Note;
07.
08.
gfx.DrawString(
"The first text annotation"
, font, XBrushes.Black, 30, 50, XStringFormats.Default);
09.
10.
11.
12.
XRect rect = gfx.Transformer.WorldToDefaultPage(
new
XRect(
new
XPoint(30, 60),
new
XSize(30, 30)));
13.
textAnnot.Rectangle =
new
PdfRectangle(rect);
14.
15.
16.
page.Annotations.Add(textAnnot);
The second text annotation (opened):
01.
02.
textAnnot =
new
PdfTextAnnotation();
03.
textAnnot.Title =
"Annotation 2 (title)"
;
04.
textAnnot.Subject =
"Annotation 2 (subject)"
;
05.
textAnnot.Contents =
"This is the contents of the 2nd annotation."
;
06.
textAnnot.Icon = PdfTextAnnotationIcon.Help;
07.
textAnnot.Color = XColors.LimeGreen;
08.
textAnnot.Opacity = 0.5;
09.
textAnnot.Open =
true
;
10.
11.
gfx.DrawString(
"The second text annotation (opened)"
, font, XBrushes.Black, 30, 140, XStringFormats.Default);
12.
13.
rect = gfx.Transformer.WorldToDefaultPage(
new
XRect(
new
XPoint(30, 150),
new
XSize(30, 30)));
14.
textAnnot.Rectangle =
new
PdfRectangle(rect);
15.
16.
17.
page.Annotations.Add(textAnnot);
Finally add a rubber stamp annotation:
01.
02.
03.
PdfRubberStampAnnotation rsAnnot =
new
PdfRubberStampAnnotation();
04.
rsAnnot.Icon = PdfRubberStampAnnotationIcon.TopSecret;
05.
rsAnnot.Flags = PdfAnnotationFlags.ReadOnly;
06.
07.
rect = gfx.Transformer.WorldToDefaultPage(
new
XRect(
new
XPoint(100, 400),
new
XSize(350, 150)));
08.
rsAnnot.Rectangle =
new
PdfRectangle(rect);
09.
10.
11.
page.Annotations.Add(rsAnnot);
PDF supports some more pretty types of annotations like PdfLineAnnotation, PdfSquareAnnotation, PdfCircleAnnotation, PdfMarkupAnnotation (with the subtypes PdfHighlightAnnotation, PdfUnderlineAnnotation, PdfStrikeOutAnnotation, and PdfSquigglyAnnotation), PdfSoundAnnotation, or PdfMovieAnnotation.
If you need one of them, feel encouraged to implement it. It is quite easy.
Note: The samples on this site usually show and discuss code snippets only. The complete source code of the samples with solutions for Visual Studio is available from the
download area on CodePlex.