JPedal’s PDF Viewer currently supports adding of annotations then saving a copy via
File -> Save As.
Can we do this programmatically? Do you think we can use
SaveFile.saveAsPdf(File file, final GUIFactory guiFactory, Values commonValues) function and just send an arbitrary value with annotations included on the
I haven’t tried this yet, so I would like to ask your opinion on this if it might not be possible due to some dependencies etc.. If there’s already an existing function for this, let me know.
I would not suggest using code from SaveFile.saveAsPdf as it contains additional steps you would not need if you are just adding annotations to a document. Instead I recommend the following.
Adding annotations can be done programmatically using the AnnotationWriter.writeAnnotations(File, File, WritableAnnotation).
This static method accepts two File objects, the first is the input file and the second is the output.
The final variable is an array of WritableAnnotation which represent the annotations you wish to add to the document.
WritableAnnotation is a abstract lass that is extended by the classes in org.jpedal.io.annotation such as CircleAnnotation, SquareAnnotation and the other classes ending in Annotation.
Each of these classes has two constructors depending on your needs.
The first constructor allows you to pass in some basic details for the annotation and covers the basics for the annotation.
The second constructor also accepts a FormObject as an input, the FormObject can contain much more detailed information about the annotation but requires much more in depth knowledge of the values used by annotations in the pdf spec.
For instance, the following would add two annotations, a square to page one at coordinates 10, 10, 100, 100 and a circle to page two at coordinates 10, 10, 100, 100.
WritableAnnotation annotations = new WritableAnnotation; annotations = new SquareAnnotation(1, 10, 10, 100, 100); annotations = new CircleAnnotation(2, 10, 10, 100, 100); AnnotationWriter.writeAnnotations(inputFile, outputFile, annotation);
The JavaDoc for AnnotationWriter can also be found here with an example that creates one of each of the support annotation types.
Thank you all for your response.
I’ll update once I encounter any problem with the suggested approach.
How about encrypted PDFs? It might be better if
AnnotationWriter accepts another param which contains PDF passwords rather than just
You above example doesn’t work. Null pointer in
saveAnnotationObjects when calling
Looks like we really need to study PDF specs and use the 2nd param which accepts a
FormObject for this to work.
Unfortunately we currently do not save annotations to encrypted files.
The null pointer you mentioned has been fixed and will be in the monthly release later today.
I tried the monthly release build today but I still get
nullpointer in other parts.
Got the error from line 191
final AnnotOREF page = info.pageOffsets[pageNum],
info can’t be null so I think the
pageOffsets array is the null one.
I have not seen this issue when running the example code.
Could you submit a bug report with the code you are using, an example file and any additional instructions / properties required to replicate the issue.
You must be logged in to reply to this topic.