Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This Gist contains code snippets regarding the addition of Text, Polyline, Squiggly, and Attachment Annotations to PDF document
This Gist contains code snippets regarding the addition of Text, Polyline, Squiggly, and Attachment Annotations to PDF document
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/
String clientId = "a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4";
String clientSecret = "d87269aade6a46cdc295b711e26809af";
// createPdfApi instance
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// input PDF document
String name = "PdfWithTable.pdf";
// Load the file from local system
File file = new File("/Users/nayyershahbaz/Downloads/" + name);
// upload the file to cloud storage
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null);
// page number for input file where annotation will be added
int pageNumber = 1;
// rectangular region for the annotation
Rectangle rect = new Rectangle()
.LLX(100.)
.LLY(100.)
.URX(200.)
.URY(200.);
List<AnnotationFlags> flags = new ArrayList<>();
flags.add(AnnotationFlags.DEFAULT);
// create FileAttachmentAnnotation object
FileAttachmentAnnotation annotation = new FileAttachmentAnnotation();
annotation.setName("Name");
annotation.setRect(rect);
annotation.setFlags(flags);
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
annotation.contents("Rich Text in the PDF File...");
annotation.setSubject("Subj");
annotation.setZindex(1);
annotation.setTitle("Title");
annotation.setModified("28/05/2021 00:00:00.000 AM");
// path of attachment file
annotation.setFilePath("images.jpeg");
// attachment file name
annotation.setFileName("images.jpeg");
// create List instance of FileAttachment
List<FileAttachmentAnnotation> annotations = new ArrayList<>();
annotations.add(annotation);
// call the method to add FileAttachment Annotation to first page of document
AsposeResponse response = pdfApi.postPageFileAttachmentAnnotations(name, pageNumber, annotations, null, null);
assertEquals(200, (int)response.getCode());
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/
String clientId = "a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4";
String clientSecret = "d87269aade6a46cdc295b711e26809af";
// createPdfApi instance
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// input PDF document
String name = "PdfWithTable.pdf";
// Load the file from local system
File file = new File("/Users/nayyershahbaz/Downloads/" + name);
// upload the file to cloud storage
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null);
// page number for input file where annnotation will be added
int pageNumber = 1;
// rectangular region for the annotation
Rectangle rect = new Rectangle()
.LLX(100.)
.LLY(600.)
.URX(200.)
.URY(650.);
// specify the vertices for the annnotation
List<Point> vertices = new ArrayList();
vertices.add(new Point().X(10.).Y(10.));
vertices.add(new Point().X(20.).Y(10.));
vertices.add(new Point().X(10.).Y(20.));
vertices.add(new Point().X(10.).Y(10.));
List<AnnotationFlags> flags = new ArrayList<>();
flags.add(AnnotationFlags.DEFAULT);
// create PolyLineAnnotation object
PolyLineAnnotation annotation = new PolyLineAnnotation();
annotation.setName("Name");
annotation.setRect(rect);
annotation.setFlags(flags);
// set the Horizontal alignment of Annotation
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
annotation.contents("Rich Text in the PDF File...");
annotation.setSubject("Subj");
annotation.setZindex(1);
annotation.setTitle("Title");
// create color object
Color color = new Color();
color.setA(255);
color.setR(120);
color.setG(140);
color.setB(130);
// set the internal color for annotation instance
annotation.setInteriorColor(color);
annotation.setVertices(vertices);
// specify the starting style for annotation
annotation.startingStyle(LineEnding.OPENARROW);
// set the ending style for annotation
annotation.endingStyle(LineEnding.SQUARE);
List<PolyLineAnnotation> annotations = new ArrayList<>();
annotations.add(annotation);
// call the method to add Polyline Annotation to first page of document
AsposeResponse response = pdfApi.postPagePolyLineAnnotations(name, pageNumber, annotations, null, null);
assertEquals(200, (int)response.getCode());
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/
String clientId = "a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4";
String clientSecret = "d87269aade6a46cdc295b711e26809af";
// createPdfApi instance
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// input PDF document
String name = "PdfWithTable.pdf";
// Load the file from local system
File file = new File("/Users/nayyershahbaz/Downloads/" + name);
// upload the file to cloud storage
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null);
// page number for input file where annotation will be added
int pageNumber = 1;
// rectangular region for the annotation
Rectangle rect = new Rectangle()
.LLX(100.)
.LLY(600.)
.URX(200.)
.URY(650.);
List<AnnotationFlags> flags = new ArrayList<>();
flags.add(AnnotationFlags.DEFAULT);
// create Square Annotation object
SquareAnnotation annotation = new SquareAnnotation();
annotation.setName("Name");
annotation.setRect(rect);
annotation.setFlags(flags);
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
annotation.contents("Rich Text in the PDF File...");
annotation.setSubject("Subj");
annotation.setZindex(1);
annotation.setTitle("Title");
annotation.setModified("28/05/2021 00:00:00.000 AM");
// create color object
Color color = new Color();
color.setA(255);
color.setR(120);
color.setG(140);
color.setB(130);
// set the internal color for annotation instance
annotation.setColor(color);
// create List of Square Annotations to hold one or more Annotation objects
List<SquareAnnotation> annotations = new ArrayList<>();
annotations.add(annotation);
// call the method to add Square Annotation to first page of document
AsposeResponse response = pdfApi.postPageSquareAnnotations(name, pageNumber, annotations, null, null);
assertEquals(200, (int)response.getCode());
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/
String clientId = "a41d01ef-dfd5-4e02-ad29-bd85fe41e3e4";
String clientSecret = "d87269aade6a46cdc295b711e26809af";
// createPdfApi instance
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// input PDF document
String name = "PdfWithTable.pdf";
// Load the file from local system
File file = new File("/Users/nayyershahbaz/Downloads/" + name);
// upload the file to cloud storage
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null);
// page number for input file where annotation will be added
int pageNumber = 1;
// rectangular region for the annotation
Rectangle rect = new Rectangle()
.LLX(100.)
.LLY(600.)
.URX(200.)
.URY(650.);
// specify the vertices for the annotation
List<Point> vertices = new ArrayList();
vertices.add(new Point().X(10.).Y(10.));
vertices.add(new Point().X(20.).Y(10.));
vertices.add(new Point().X(10.).Y(20.));
vertices.add(new Point().X(10.).Y(10.));
List<AnnotationFlags> flags = new ArrayList<>();
flags.add(AnnotationFlags.DEFAULT);
// create SquigglyAnnotation object
SquigglyAnnotation annotation = new SquigglyAnnotation();
annotation.setName("Name");
annotation.setRect(rect);
annotation.setFlags(flags);
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
annotation.contents("Rich Text in the PDF File...");
annotation.setSubject("Subj");
annotation.setZindex(1);
annotation.setTitle("Title");
annotation.setModified("28/05/2021 00:00:00.000 AM");
// create color object
Color color = new Color();
color.setA(155);
color.setR(120);
color.setG(140);
color.setB(130);
// set the internal color for annotation instance
annotation.color(color);
// set annotation points
annotation.setQuadPoints(vertices);
List<SquigglyAnnotation> annotations = new ArrayList<>();
annotations.add(annotation);
// call the method to add Squiggly Annotation to first page of document
AsposeResponse response = pdfApi.postPageSquigglyAnnotations(name, pageNumber, annotations, null, null);
assertEquals(200, (int)response.getCode());
// Get ClientID and ClientSecret from https://dashboard.aspose.cloud/
String clientId = "7042694c-5251-4aba-83c9-e81e7518724f";
String clientSecret = "db246d4742e8cd22e7266c9391992689";
// createPdfApi instance
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// input PDF document
String name = "PdfWithTable.pdf";
// Load the file from local system
File file = new File("/Users/nayyershahbaz/Downloads/" + name);
// upload the file to cloud storage
FilesUploadResult uploadResponse = pdfApi.uploadFile(name, file, null);
// page number where Annotation needs to be added
int pageNumber = 1;
// create Rectangle object where Annotation is added
Rectangle rect = new Rectangle()
.LLX(100.)
.LLY(600.)
.URX(200.)
.URY(650.);
// Create ListArray of AnnotationFlags
List<AnnotationFlags> flags = new ArrayList<>();
flags.add(AnnotationFlags.DEFAULT);
// Create TextAnnotation object
TextAnnotation textAnnotation = new TextAnnotation();
textAnnotation.setName("Annotation Name");
textAnnotation.setRect(rect);
textAnnotation.setFlags(flags);
textAnnotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
// set the content to be displayed inside Annotation
textAnnotation.contents("Hello World...");
// Set the icon for annotation
textAnnotation.icon(TextIcon.KEY);
textAnnotation.setSubject("Text Box Subj");
textAnnotation.setZindex(1);
// the default state of Annotation Object
textAnnotation.setState(AnnotationState.COMPLETED);
// create ListArray of TextAnnotation
List<TextAnnotation> annotations = new ArrayList<>();
// add above created TextAnnotation to List instance
annotations.add(textAnnotation);
// call the method to add annotation to PDF file
AsposeResponse response = pdfApi.postPageTextAnnotations(name, pageNumber, annotations, null, null);
assertEquals(200, (int)response.getCode());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment