Skip to content

Commit f502e56

Browse files
committed
952337 Added proper input and code example.
1 parent 671f5be commit f502e56

4 files changed

Lines changed: 28 additions & 54 deletions

File tree

Binary file not shown.

Annotation/Get-value-from-PDF-annotation/.NET/Get-value-from-PDF-annotation/Program.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,27 @@
55
// Load the existing PDF document using FileStream
66
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read))
77
{
8+
// Load the PDF document from the input stream
89
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream))
910
{
10-
// Get the first page
11-
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
11+
// Access the first page of the document
12+
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
13+
// Get the collection of annotations from the page
14+
PdfLoadedAnnotationCollection annotations = page.Annotations;
1215

13-
// Get all annotations on the page
14-
PdfLoadedAnnotationCollection annotations = loadedPage.Annotations;
15-
16-
// Make sure the 65th annotation exists and is a circle annotation
17-
if (annotations.Count > 64 && annotations[64] is PdfLoadedCircleAnnotation circleAnnotation)
16+
// Check if at least one annotation exists and it's a popup annotation
17+
if (annotations.Count > 0 && annotations[0] is PdfLoadedPopupAnnotation annotation)
1818
{
19-
// Get the review history from the circle annotation
20-
PdfLoadedPopupAnnotationCollection reviewHistory = circleAnnotation.ReviewHistory;
19+
// Get the custom value from the annotation
20+
List<string> customValue = annotation.GetValues("custom");
2121

22-
// Ensure review history has at least two items
23-
if (reviewHistory != null && reviewHistory.Count > 1)
22+
foreach (string value in customValue)
2423
{
25-
// Get the second popup annotation from review history
26-
PdfLoadedPopupAnnotation popupAnnotation = reviewHistory[1] as PdfLoadedPopupAnnotation;
27-
28-
if (popupAnnotation != null)
29-
{
30-
// Get values for the "State" key
31-
List<string> values = popupAnnotation.GetValues("State");
32-
foreach (string value in values)
33-
{
34-
Console.WriteLine(value);
35-
}
36-
}
37-
}
24+
// Print the custom value to the console
25+
Console.WriteLine("Custom value from annotation: " + value);
26+
}
3827
}
28+
// Close the document and release resources
29+
loadedDocument.Close(true);
3930
}
4031
}
Binary file not shown.

Annotation/Set-value-from-PDF-annotation/.NET/Set-value-from-PDF-annotation/Program.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,29 @@
66
// Load the existing PDF document using FileStream
77
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read))
88
{
9+
// Load the PDF document from the input stream
910
using (PdfLoadedDocument ldoc = new PdfLoadedDocument(inputStream))
1011
{
11-
// Get the first page
12-
PdfLoadedPage lpage = ldoc.Pages[0] as PdfLoadedPage;
12+
// Access the first page of the document
13+
PdfLoadedPage page = ldoc.Pages[0] as PdfLoadedPage;
1314

14-
// Get all annotations on the page
15-
PdfLoadedAnnotationCollection annots = lpage.Annotations;
15+
// Get the collection of annotations from the page
16+
PdfLoadedAnnotationCollection annotations = page.Annotations;
1617

17-
// Access the 65th annotation (index starts at 0)
18-
if (annots.Count > 64 && annots[64] is PdfLoadedCircleAnnotation Icircle)
18+
// Check if at least one annotation exists and it's a popup annotation
19+
if (annotations.Count > 0 && annotations[0] is PdfLoadedPopupAnnotation annotation)
1920
{
20-
// Get the author of the circle annotation
21-
string author = Icircle.Author;
22-
23-
// Get the review history and comments
24-
PdfLoadedPopupAnnotationCollection collection = Icircle.ReviewHistory;
25-
PdfLoadedPopupAnnotationCollection collectionComments = Icircle.Comments;
26-
27-
// Check if there's at least a second item in the review history
28-
if (collection != null && collection.Count > 1)
29-
{
30-
PdfLoadedPopupAnnotation annotation = collection[1] as PdfLoadedPopupAnnotation;
31-
32-
if (annotation != null)
33-
{
34-
// Set custom state and state model
35-
annotation.SetValues("State", "Approved");
36-
annotation.SetValues("StateModel", "ReviewWorkflow");
37-
annotation.SetValues("ReviewedBy", "John Doe");
38-
annotation.SetValues("ReviewedOn", DateTime.Now.ToString("yyyy-MM-dd"));
39-
}
40-
}
21+
// Set a custom key-value pair in the annotation's metadata
22+
annotation.SetValues("custom", "This is the custom data for the annotation");
4123
}
4224

43-
// Save the modified document using FileStream
25+
// Save the modified document using a new FileStream
4426
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write))
4527
{
46-
ldoc.Save(outputStream);
28+
// Save changes to a new PDF file
29+
ldoc.Save(outputStream);
4730
}
48-
31+
// Close the document and release resources
4932
ldoc.Close(true);
5033
}
5134
}

0 commit comments

Comments
 (0)