如何检查PDF启用Reader或不使用C#? [英] How to Check PDF is Reader enabled or not using C#?

查看:245
本文介绍了如何检查PDF启用Reader或不使用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我唯一的要求是找一个文件夹中选定的PDF启用与否阅读器,更具体地说,如果使用权的方式,使人们能够添加注释(例如注释)定义的。



我在Windows应用程序这样做。如果我点击一个按钮,就会触发一个事件PDF文件搜索的文件夹。此事件需要检查文件夹中的PDF文件是否是征求意见启用读卡器。如果是这样,我需要删除注释使用权,或恢复PDF回原来的版本。



我的代码只能找到该文件夹​​中的PDF文件。我不`吨知道如何检查,如果选定的PDF或不启用评论。请温柔,并提出解决方案。



下面是我的代码:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
{
的String []文件路径= Directory.GetFiles(D:\\myfolder\\pdffolder);
名单,LT; ListViewItem的>文件=新的List< ListViewItem的>();
的foreach(在文件路径字符串文件路径)
{
---需要检查启用与否评论---
}
}
}


解决方案

您想知道,如果一个PDF是阅读器启用。读者可以通过添加所谓的使用权(UR)数字签名签名确立启用。如果你有 PdfReader 的一个实例,你可以检查PDF是否使用 hasUsageRights()方法:

  PdfReader读卡器=新PdfReader(文件路径); 
布尔isReaderEnabled = reader.hasUsageRights();



使用权可以包含很多不同的东西,比如让人们发表意见,让人们保存填充不在状态,让人们签署一份文件,...



要找出哪些权利被启用,您有权检查无论是 UR UR3 词典(注意, UR 已过时,但仍可能PDF文件出来有有一个 UR 字典):

  PdfDictionary目录=读者。 getCatalog(); 
PdfDictionary烫发= catalog.getAsDict(PdfName.PERMS);
PdfDictionary UR = NULL;
如果(烫发!= NULL){
PdfDictionary UR = perms.getAsDict(PdfName.UR);
如果(UR == NULL)
UR = perms.getAsDict(PdfName.UR3);
}
}

如果 UR 遗体,没有使用权。如果你只是想检查是否启用了注释,你必须检查 UR 词典的词条。届时将有 / Annots 项与作为价值与价值观,如创建,删除,修改,复制,进口,出口,在线和SummaryView阵列。对于可能的项的完整概述,请参阅表255项中的UR变换参数字典的ISO-32000-1。



您可以删除全部< /像这样STRONG>使用权:

  PdfReader读卡器=新PdfReader(文件路径); 
如果(reader.hasUsageRights()){
reader.removeUsageRights();
PdfStamper模子=新PdfStamper(阅读器,新的FileOutputStream(path_to_new_file));
stamper.close();
}



这是不可能以删除只有使用权为评论,同时保留其它使用权​​限(如果存在)。刚刚从本 / UR / Annots 输入 / UR3 字典将打破,使使用权的数字签名。此数字签名与被允许使用该密钥由Adobe和任何第三方工具(不是Adobe产品等)所拥有的私钥生成的。



决赛注意:



所有的代码片段都用Java编写的,但iTextSharp的在C#中相应的方法或属性。它不应该是口段为C#问题



在很多情况下,它足以小写变成大写:




  • Java的:object.add(东西);

  • C#:object.Add(东西);



或者你必须删除设置/获取:




  • Java的:object.setSomething(东西);

  • C#:object.Something =东西;


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---
        }
    }
}

解决方案

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;

这篇关于如何检查PDF启用Reader或不使用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆