-2

I am trying to make a program that multiple users can share one PDF document , and every one can put his comments on the PDF using add sticky note without changing/modifying other notes. For example , The program will transfer that PDF file to another person for review and checking , the reviewer will put his comments on the PDF and send it to Approver person . The approver can't edit reviewer comments and change it at all , he can add new sticky note to it for his comments.

if I use the security on the PDF ( ~ PdfWriter.AllowModifyAnnotations ) , I will disable entering new sticky note.

is there any solution for that ?

please help me and thanks in advance

Ali Alawi
  • 33
  • 1
  • 8
  • Which programs do your users user? I ask because given the appropriate program, a read-only annotation can be edited nonetheless. – mkl May 17 '16 at 21:02
  • i am using JSP ( java server page) , a user will upload PDF with his comments and the system will forword the file to the assgined person ( say person B ) . person B can't edit all prevoius comments by previous users. he can only add his comment to the file and upload it to the system again. i want to know how can I make the comments read only if other people open the PDF file.they can read the content and add notes only. – Ali Alawi May 17 '16 at 21:15
  • *person B can't edit all prevoius comments by previous users. he can only add his comment to the file and upload it to the system again.* - using which program shall person B be unable to edit previous comments but be able to add new ones? – mkl May 18 '16 at 04:09
  • "using which program shall person B be unable to edit previous comments but be able to add new ones?" , it is normal PDF program. Person B will receive the PDF in email , he will read , check and put his comments on the PDF he received. he will open the PDF file as normal , using acrobat reader or PDF professional or whatever tools opening the PDF file. when he is opening the PDF using acrobat reader , he will not be able to adjust other people notes . – Ali Alawi May 18 '16 at 04:54
  • *using acrobat reader or PDF professional or whatever tools opening the PDF file* - some such programs allow to ignore the read/only property of annotations. That's why I asked. *when he is opening the PDF using acrobat reader , he will not be able to adjust other people notes* - ah, ok, so it suffices if those notes cannot be changed in Adobe Acrobat Reader (not pro). – mkl May 18 '16 at 10:25
  • now how can I make my java program to disable editing the comments , comments will be read only. it is the same like how we secure full PDF by allowing some permission only ( allow print only for example ) , user can't edit the file at all. I need the same to do with annotation , enable adding and editing new one and disable editing the exist previous one. – Ali Alawi May 18 '16 at 14:19
  • Annotations have flags. One of these flags is a read-only flag. My approach would be to iterate over the annotations of all pages and set their read-only flag. I have not yet had the time to test this, though. – mkl May 18 '16 at 15:10
  • please Sir , if you could test this and show me how it can be done I would appreciate this very much from you because I searched in many websites but I could not find anything. I am waiting your respond Sir .. thanks a lot – Ali Alawi May 19 '16 at 04:37

1 Answers1

-1

PDF annotation objects can have flags; one of these flags is a read-only flag. Thus, you only have to iterate over all annotations on all pages and set their respective read-only flag.


In iText 5.5.x this can be done like this:

PdfReader reader = new PdfReader(resourceStream);
PdfStamper stamper = new PdfStamper(reader, outputStream);

for (int page = 1; page <= reader.getNumberOfPages(); page++)
{
    PdfDictionary pageDictionary = reader.getPageN(page);
    PdfArray annotationArray = pageDictionary.getAsArray(PdfName.ANNOTS);
    if (annotationArray == null)
        continue;
    for (PdfObject object : annotationArray)
    {
        PdfObject directObject = PdfReader.getPdfObject(object);
        if (directObject instanceof PdfDictionary)
        {
            PdfDictionary annotationDictionary = (PdfDictionary) directObject;
            PdfNumber flagsNumber = annotationDictionary.getAsNumber(PdfName.F);
            int flags = flagsNumber != null ? flagsNumber.intValue() : 0;
            flags |= PdfAnnotation.FLAGS_READONLY;
            annotationDictionary.put(PdfName.F, new PdfNumber(flags));
        }
    }
}

stamper.close();

(iText 5 MarkAnnotationReadOnly.java)


In iText 7.0.x it can be done like this

try (   PdfReader pdfReader = new PdfReader(resourceStream);
        PdfWriter pdfWriter = new PdfWriter(outputStream);
        PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter) )
{
    for (int page = 1; page <= pdfDocument.getNumberOfPages(); page++)
    {
        PdfPage pdfPage = pdfDocument.getPage(page);
        for (PdfAnnotation pdfAnnotation : pdfPage.getAnnotations())
        {
            pdfAnnotation.setFlag(PdfAnnotation.READ_ONLY);
        }
    }
}

(iText 7 MarkAnnotationReadOnly.java)

Only the kernel iText 7 artifact is required.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • If this works for you, you might want to accept the answer (click on the tick at its upper left). – mkl May 21 '16 at 06:51