PDF Free hand drawing

Hello,


I was wondering if aspose pdf is capable of allowing users to free hand draw on pdf documents? If so - can this be done using the aspose.pdf.java? Are there examples that I can see or use to demonstrate this?

Kindly let me know,

Thanks!

Hi Troy,


Thanks for your interest in our API’s.

We have an API named Aspose.Pdf for Java which supports the feature to create as well as manipulate PDF documents. It also provides the feature to add Annotations to PDF file. So as per your requirement, you may consider using PdfContentEditor class of com.aspose.pdf.facades package. You may consider using following methods to accomplish your requirement.

  • createPolygon(…)
  • createLine(…)
  • createPolyLine(lineInfo, page, annotRect, annotContents)

Thanks Nayyer!


That was informative. Is there an example already to demonstrate this? Specifically using the PdfContentEditor class?

Thanks,

Nayyer,


I apologize - but I would also like to know if drawing circles/rectangles in addition to free hand drawing on the pdf document is available.

Thanks,

tekstream:
I apologize - but I would also like to know if drawing circles/rectangles in addition to free hand drawing on the pdf document is available.
Hi Troy,

Thanks for sharing the details.

In order to add Circle/Squares to PDF file, please try using following code snippet. A single method createSquareCircle(…) can be used to accomplish both requirements.

[Java]

//
instantiate pdfContentEditor instance
<o:p></o:p>

com.aspose.pdf.facades.PdfContentEditor editor = new com.aspose.pdf.facades.PdfContentEditor();

// bind source PDF file

editor.bindPdf("c:/pdftest/font)_output.pdf");

// pass 4th argument as flase to draw circle. When using true, square will be added

editor.createSquareCircle(new java.awt.Rectangle(10, 10, 50, 30), "contents", java.awt.Color.BLUE, false, 1, 2);

// save resultant file

editor.save(“c:/pdftest/CircleAdded.pdf”);


Ps, you do not need to be sorry. We are always delighted to assist our customers with their queries.

Thanks Nayyer! Appreciate your help and prompt response. I’ll try that code you provided.


I’ll keep this post active, incase there are new requirements or help that I may need from you!



Hello Nayyer,


I have a little more detailed requirement for you. I am hoping you can help me with this.

Use case scenario:

1. User 1 opens a test.pdf file. He or she adds “sticky notes”, circles or rectangles to text on the pdf as he/she wishes to on any part of the pdf.
2. User 2 opens the same test.pdf, however is not able to see changes made by User 1 - and this is due to security reasons. Therefore User 2, makes changes on top on User 1 changes (layers). This user may add “squiggly lines”, “sticky notes” etc.

Essentially - a pdf document that can be fully editable, utilizing all annotations and/or interactive tools provided by Aspose PDF API. However, we should keep in mind the pdf document should be secured per user. ONLY admin can view all the changes made to a pdf document.

Is this possible? Please find an attached image as a simple example. Your help and/or any code would help us get started.

Thanks!


Hi Troy,


Thanks for sharing the details.

As shared earlier, Aspose.Pdf for .NET provides the feature to add annotation to PDF file and it also supports the feature to set access privileges over PDF file where you can prevent the users from adding / updating annotations in PDF document. But I am afraid it does not support the feature to control annotation appearance/access based on user viewing the document.

Besides this, we have Annotation app from our sister company named GroupDocs which provides the feature to share document with multiple users and allowing the users to enter/add their annotations. You may consider using this app and see if it can fulfill your requirements.

In the event of any further query, please feel free to contact.

Nayyer,


Thanks for your response. The Annotation app does look like what we want - however not looking to have additional costs at this point of time.

You mentioned "the feature to add annotation to PDF file and it also supports the feature to set access privileges over PDF file where you can prevent the users from adding / updating annotations in PDF document."

Could you kindly share a code snippet to demonstrate this functionality?

Thanks for your help again.

FYI - We are using the Java API and not .NET

Hi Troy,

Please try using following code snippet to set access privileges over PDF file. When using following code snippet, the users viewing the PDF file will not be able to add/modify annotations but the Admin viewing the file with owner password will be able to make any modifications.

For further details, please visit Set Privileges on an Existing PDF File

[Java]

//
load existing PDF file

Document document = new Document("c:/pdftest/font_output.pdf");

try /*JAVA: was using*/

{

// allow all the feature / modifications over PDF file

com.aspose.pdf.facades.DocumentPrivilege documentPrivilege = com.aspose.pdf.facades.DocumentPrivilege.getAllowAll();

documentPrivilege.setAllowScreenReaders(true);

// revoke annotation modification. Only admin will be able to modify comments/annotations

documentPrivilege.setAllowModifyAnnotations(false);

document.encrypt("user", "owner", documentPrivilege, CryptoAlgorithm.AESx128, false);

// save updated file

document.save("c:/pdftest/AnnotationModify_revoked.pdf");

}

finally { if (document != null) document.dispose(); }

Hello Nayyer,


Please find my code snippet. When the output file is generated, I am still able to highlight, add annotations and copy/paste text from the PDF document. Is there something I am doing wrong here?

Also how can I add different users access to the output pdf, however restrict usage such as adding annotations etc? Admin will still continue to have all privileges.

p.s - I have attached the test document.
Thanks for your help.

public class PDFSecurity {

private Document document = new Document(“C:/test_security.pdf”);
private String licenseFile = “C:/Aspose.Pdf.lic”;
public void setSecurity() {

try{
//allow all features/modifications over PDF file
com.aspose.pdf.facades.DocumentPrivilege documentPrivilege = com.aspose.pdf.facades.DocumentPrivilege.getAllowAll();
documentPrivilege.setAllowScreenReaders(true);
//revoke annotation modifications. Only admin will be able to modify comments/annotations
//Restrict annotation modification
documentPrivilege.setAllowModifyAnnotations(false);

//Restrict contents modification
documentPrivilege.setAllowModifyContents(false);

//Restrict copying the data
documentPrivilege.setAllowCopy(false);
document.encrypt(“user”, “owner”, documentPrivilege, CryptoAlgorithm.AESx128, false);
//save updated file
document.save(“C:/AnnotationModify_revoked.pdf”);
}
finally{
if(document != null){
document.dispose();
}
}
}
public void license() {
com.aspose.pdf.License license = new com.aspose.pdf.License();
try {

license.setLicense(licenseFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {

System.out.println(“Setting License file…”);
new PDFSecurity().license();
System.out.println(“Executing security method…”);
new PDFSecurity().setSecurity();
}

}

Hi Troy,


I have tested the scenario using Aspose.Pdf for Java 9.5.0 and as per my observations, the viewer providing user password can only view the document but cannot add Annotations or make any other modifications to the PDF file However the person viewing file with owner password can edit/update/manipulate the PDF file. For your reference, I have also attached the resultant PDF generated over my end.

In case I have not properly understood your problem, please share some further details.

Nayyer,


You are absolutely correct. I misunderstood the constructor for encrypt. I misread it as “userName” and “userPassword” but it is infact “userPassword” and “ownerPassword”. I apologize for that. I kept testing with the “owner” password, and hence none of the restrictions were provoked.

But keeping this in mind - document.encrypt(“user”, “owner”, documentPrivilege, CryptoAlgorithm.AESx128, false);

is a one line argument, that can pass both user and owner passwords. But I would like to have conditional statements for e.g.

if document.encrypt -> “owner”
-> then
//Restrict annotation modification
documentPrivilege.setAllowModifyAnnotations(false);
//Restrict contents modification
documentPrivilege.setAllowModifyContents(false);
//Restrict copying the data
documentPrivilege.setAllowCopy(false);

else if document.encrypt -> “user”
-> then
//Restrict annotation modification
documentPrivilege.setAllowModifyAnnotations(true);
//Restrict contents modification documentPrivilege.setAllowModifyContents(true);
//Restrict copying the data
documentPrivilege.setAllowCopy(true);

The reason I want a conditional is because, we may have multiple users trying to access the same pdf file, but based on the user’s credential’s we allow them to do certain things to the pdf document.

Would you have an idea on how to implement it using if/else conditions?

Thanks for your help.

Hi Troy,


Thanks for sharing the details.

I am afraid the above stated requirement is currently not supported. However for
the sake of correction, I have logged it in our issue tracking system as PDFNEWJAVA-34480. We will
investigate this issue in details and will keep you updated on the status of a
correction.

We apologize for your inconvenience.

Nayyer,


Thanks for your response! Please keep me updated if you find an approach.

Thank you

Hi Troy,


The development team will analyze this requirement and will determine if this feature can be accomplished or not but we will surely update with the status of implementation. Your patience and comprehension is greatly appreciated in this regard.

Hello Nayyer,


Would you have any update on this ticket #?

Thanks

Hi Troy,


Thanks for your inquiry. I am afraid your issue is not resolved as we have noticed it recently. It is pending for investigation in the queue with other priority task. As soon as its investigation is completed then we will share our findings or ETA with you.<o:p></o:p>

We are sorry for the inconvenience caused.<o:p></o:p>

Best Regards,

hi nayer, this looks like a way to create drawings in pdf. is this considered an annotation?


what if we want to add replies to that annotation? how will the code look like?

Hi Chit,


Thanks for your inquiry. Please note drawing objects via Graph are not annotations but graphics.

Furthermore we can add reply to an annotation as following. However we will appreciate it if you please share your sample code along with document here, it will help us to understand your scenario exactly and will guide you accordingly.

Document pdfDocument = new Document(“annotation_out.pdf”);<o:p></o:p>

TextAnnotation replyAnnotation = new TextAnnotation(pdfDocument.getPages().get_Item(1), new com.aspose.pdf.Rectangle(200, 300, 300, 400));

replyAnnotation.setTitle("It needs to revisit");

replyAnnotation.setInReplyTo(pdfDocument.getPages().get_Item(1).getAnnotations().get_Item(1));

pdfDocument.getPages().get_Item(1).getAnnotations().add(replyAnnotation);

pdfDocument.save("Replied.pdf");


Best Regards,