2

My only requirement is to find a selected pdf in a folder is Reader enabled or not, more specifically if usage rights are defined in a way that allows people to add annotations (e.g. comments).

I am doing this in windows application. If I click a button, an event is triggered searching a folder for PDF files. This event needs to check whether or not the PDFs in the folder are Reader enabled for comments. If they are, I need to remove the comment usage rights or revert the PDF back to its original version.

My code can only find PDF files in the folder. I don`t know how to check if the selected PDF is comment enabled or not. Please be gentle and suggest solution.

Here's my code:

private void button1_Click(object sender, EventArgs e)
{
    {
        string[] filePaths = Directory.GetFiles("D:\\myfolder\\pdffolder");
        List<ListViewItem> files = new List<ListViewItem>();
        foreach (string filePath in filePaths)
        {
            ---need to check comment enabled or not---
        }
    }
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Nachiappan R
  • 182
  • 1
  • 1
  • 20
  • 1
    Please clarify what you mean when you say "Comment enabled". Are you referring to "Reader enabling"? Note that *all* PDFs are comment enabled in the sense that Adobe Reader no longer requires documents to be Reader enabled for people to be able to add comments. Another option is that you are referring to permissions set by encryption, but in that case your question makes even less sense because it's not clear what you'd do with an encrypted file to make it no longer comment enabled. This question is unanswerable in its current state. If it's not clarified, it should be closed as "unclear". – Bruno Lowagie May 29 '15 at 14:31
  • 1
    It would help if you'd ask the person who wrote your requirement what she/he means and what she/he want to achieve by removing "Comment enabling." Never obey an order unless you understand what is intended with the order. – Bruno Lowagie May 29 '15 at 14:34
  • Thanks Bruno for your suggestion and clarrification. Actually the generated pdf is not defaultly comment enabled, to comment enable in acrobat File > Save As > Reader Extended PDF > Enable Commenting In Adobe Reader, which help reader to mark comments. So I need to check this programmatically whether that file is comment enabled or not? I hope I have shown my view... – Nachiappan R May 29 '15 at 15:06
  • What I tried to explain is that Adobe Reader now allows end users to add comments to *any* PDF whether it is Reader enabled or not. Removing reader enabling won't help you prevent that users add comments... – Bruno Lowagie May 29 '15 at 15:25
  • I've answered your question in Java. It shouldn't be any problem for you to convert the Java to C#. – Bruno Lowagie May 29 '15 at 15:47
  • Thanks Bruno for your suggestion, Adobe Reader not allows to add comment, but the adobe acrobat pro user can give access to readers to add comment, any way thanks for your coding. I will try this in C# and get back to you. – Nachiappan R May 30 '15 at 05:17
  • If Adobe Reader doesn't allow you to add comments, you are using an old version of Adobe Reader. Recent versions allow you to add comments. – Bruno Lowagie May 30 '15 at 10:21
  • I am checking with it and get back to u bruno – Nachiappan R May 30 '15 at 17:37

2 Answers2

2

You want to know if a PDF is Reader enabled or not. Reader enabling is established by adding a digital signature known as a Usage Rights (UR) signature. If you have an instance of PdfReader, you can check whether or not a PDF is Reader enabled by using the hasUsageRights() method:

PdfReader reader = new PdfReader(path_to_file);
boolean isReaderEnabled = reader.hasUsageRights();

Usage rights can encompass many different things, such as allowing people to comment, allowing people to save a filled out form, allowing people to sign a document,...

To find out which rights are enabled, you have to inspect either the UR or the UR3 dictionary (note that UR is deprecated, but there may still be PDFs out there that have a UR dictionary):

PdfDictionary catalog = reader.getCatalog();
PdfDictionary perms = catalog.getAsDict(PdfName.PERMS);
PdfDictionary ur = null;
if (perms != null) {
    PdfDictionary ur = perms.getAsDict(PdfName.UR);
    if (ur == null)
        ur = perms.getAsDict(PdfName.UR3);
    }
}

If ur remains null, there are no usage rights. If you only want to check if commenting is enabled, you'll have to inspect the entries of the ur dictionary. There will be an /Annots entry with as value an array with values such as Create, Delete, Modify, Copy, Import, Export, Online and SummaryView. FOr the full overview of possible entries, see Table 255 "Entries in the UR transform parameters dictionary" of ISO-32000-1.

You can remove all usage rights like this:

PdfReader reader = new PdfReader(path_to_file);
if (reader.hasUsageRights()) {
    reader.removeUsageRights();
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(path_to_new_file));
    stamper.close();
}

It is impossible to remove only the usage rights for commenting while preserving other usage rights (if present). Just removing the /Annots entry from the /UR or /UR3 dictionary will break the digital signature that enables usage rights. This digital signature is created with a private key owned by Adobe and no third party tool (other than an Adobe product) is allowed to use that key.

Final note:

all code snippet were written in Java, but iTextSharp has corresponding methods or properties in C#. It shouldn't be a problem to port the snippets to C#.

In many cases, it's sufficient to change a lower case into an upper case:

  • Java: object.add(something);
  • C#: object.Add(something);

Or you have to remove the set/get:

  • Java: object.setSomething(something);
  • C#: object.Something = something;
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks Bruno great effort, I am beginner to C#, I need to make a R and D. Please hold I will get back you soon if I need any suggestion. – Nachiappan R May 30 '15 at 05:22
  • Bruno, I just started checking today, I tried in c#, but pdfreader no having hasUsageRights() property. Could you help on this? I just need to check pdf is comment enabled (which is activated thru acrobat professional), if the pdf is enabled I need to delete the specific task. – Nachiappan R Jun 02 '15 at 13:50
  • @@Bruno Lowagie, only need is to check the pdf is enable or not, if enabled reader can see the comments and measuring tool menu in tool menu, if is not enabled the comments and measuring tool is not available in menu. My point is to check the selected pdf is comment enabled or not. – Nachiappan R Jun 03 '15 at 09:38
  • Could you confirm this should check only comment enabled? I am converting java to C# in a process, but facing some errors – Nachiappan R Jun 04 '15 at 03:04
  • [Copy/Pasted reply:] I was in Singapore for the Communicasia conference the whole week. If you have an additional question, hence I can't answer your question today. [Extra comment:] I have no idea what you're talking about. iTextSharp is your only hope, but if you want to make this work, you either have to find the solution yourself. If you want somebody else to solve your problem, please hire somebody to help you. – Bruno Lowagie Jun 06 '15 at 10:20
  • Thanks @Bruno, I will try with itextsharp and at extreme I hire somebody. – Nachiappan R Jun 07 '15 at 04:35
0

Thanks for all who takes effect to my question. I finally found the answer in a similar way by reading the PDF and check for a particular string (particular string presented if Comment enabled on the PDF).

The particular string starts with /Annot ....., First I read the PDF thru System.IO, then store in a string and looking for the particular string, If the searching string available then the PDF is comment enabled else not.

Nachiappan R
  • 182
  • 1
  • 1
  • 20
  • This is entirely wrong. **Annot** is a key used in page dictionaries for an array of annotations on the page. Thus, all your test does is check whether there *currently **is** any annotation in the document* (and even that not properly because there can be both false positives and false negatives). There are numerous tools out there which can add annotations without requiring reader enabled documents, and reader enabled documents need not yet have annotations. – mkl Jun 19 '15 at 08:35
  • This is Right, I am not included full keyword @mkl. I mentioned like Annots.., that is this is the key that comment enable pdf will contains, Annots[/Create/Delete/Modify/Copy/Import/Export – Nachiappan R Jun 19 '15 at 08:56
  • Still this is not a sure thing, maybe the next Acrobat changes the details of the string'ish representation in a way that according to PDF syntax is equivalent. Algorithms treating PDFs like they were simple text files usually break after some time or given enough input from the wild.. – mkl Jun 19 '15 at 09:11
  • @mkl, I am looking for solution for my current issue, I will check and update for future changes. Thanks for your suggestion – Nachiappan R Jun 19 '15 at 09:34