如何创建和应用删节? [英] How to create and apply redactions?

查看:305
本文介绍了如何创建和应用删节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法利用iText来实现PDF新版本?通过Acrobat SDK API我发现删节也只是似乎与子类型纂的注释工作。 ?所以我在想,如果有可能创造那些iTextSharp的以及



通过Acrobat的SDK代码想简单地是这样的:

  AcroPDAnnot ANNOT = page.AddNewAnnot(-1涂黑,RECT)为AcroPDAnnot; 



(我还没有能够运用它们虽然为 annot.Perform (avDoc)似乎并没有工作。想法?)



在iTextSharp的我可以创建简单的文字注释是这样

  PdfAnnotation注释= PdfAnnotation.CreateText(stamper.Writer,矩形,标题,内容,假,空); 



我发现的唯一的其他选择,所以很是创造黑色矩形作为解释的这里,但这并不删除文本(它仍然可以选择)。我想创建新版本的注释和应用最终节录



//更新:



当我终于可以创建一个工作的例子,我想在这里分享。它并不适用删节到底,但它创造这些Acrobat中正常显示,然后可以手动应用有效的删节。

 使用(流流=新的FileStream(文件名,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
PdfReader pdfReader =新PdfReader(流);
//使用(PdfStamper压模=新PdfStamper(pdfReader,新的F​​ileStream(newFileName,FileMode.OpenOrCreate)))
{
印模
//添加注释
INT页面= 1;
iTextSharp.text.Rectangle矩形=新iTextSharp.text.Rectangle(500,50,200,300);

PdfAnnotation注释=新PdfAnnotation(stamper.Writer,RECT);
annotation.Put(PdfName.SUBTYPE,新PdfName(涂黑));

annotation.Title =我的作者; //标题作者=
annotation.Put(新PdfName(SUBJ),新PdfName(涂黑)); //编修主题。当在Acrobat创建的,它始终设置为涂黑

浮法[]填充颜色= {0,0,0}; //黑
annotation.Put(新PdfName(IC),新PdfArray(填充颜色)); //内部彩色

浮子[] fillColorRed = {1,0,0}; //红
annotation.Put(新PdfName(OC),新PdfArray(fillColorRed)); //轮廓颜色

stamper.AddAnnotation(注释,页);
}

}


解决方案

答1:创建新版本注释



iText的是一个工具箱,让你创造任何你想要的对象的权力。您正在使用的方便的方法的创建一个文本注释。这是表面文章。



您可以使用iText的去创造你想要的任何类型的注释,因为 PdfAnnotation 类,扩展了 PdfDictionary



这是在我的书的的iText在行动 - 第二版 GenericAnnotations 是说明这个功能的例子。



如果我们端口这个例子C#中,我们有:

  PdfAnnotation注释=新PdfAnnotation(作家,RECT) ; 
annotation.Title =文本注释;
annotation.Put(PdfName.SUBTYPE,PdfName.TEXT);
annotation.Put(PdfName.OPEN,PdfBoolean.PDFFALSE);
annotation.Put(PdfName.CONTENTS,
新PdfString(的String.Format(图标:{0},文本))
);
annotation.Put(PdfName.NAME,新PdfName(文本));
writer.AddAnnotation(注释);

这是一个的手动的方法来创建一个文本注释。你想要一个注释,所以你需要的东西是这样的:

  PdfAnnotation注释=新PdfAnnotation(作家,RECT); 
annotation.Put(PdfName.SUBTYPE,新PdfName(涂黑));
writer.AddAnnotation(注释);

您可以使用把()方法要添加所有你需要注解的其他键



答2:如何到应用一个新版本的注释



第二个问题,需要把itext-xtra.jar(一个额外的罐子随iText的),你至少需要iText的5.5.4。添加不透明矩形的方法并不适用于新版本:在节录的内容仅仅是盖的,不会被删除。您仍然可以选择文本和复制/粘贴。如果你不小心,你可能有所谓的 PDF停电愚蠢的。见比如NSA / AT& T公司丑闻



假设你有在其中添加一些新版本注释的文件:的 page229_redacted.pdf





我们现在可以使用此代码,以消除新版本标示内容注释:

 公共无效manipulatePdf(字符串SRC,字符串DEST)抛出IOException异常,DocumentException {
PdfReader读卡器=新PdfReader (SRC);
PdfStamper模子=新PdfStamper(阅读器,新的FileOutputStream(DEST));
PdfCleanUpProcessor清洁=新PdfCleanUpProcessor(印模);
cleaner.cleanUp();
stamper.close();
reader.close();
}

这将导致以下PDF:的 page229_apply_redacted.pdf





正如你可以看到,红色矩形边框被替换填充黑色矩形。如果您尽量选择原文,你会发现,是不再存在。


Is there any way to implement PDF redaction using iText? Working with the Acrobat SDK API I found that redactions also just seem to be annotations with the subtype "Redact". So I was wondering if it's possible to create those in iTextSharp as well?

With the Acrobat SDK the code would like simply like this:

AcroPDAnnot annot = page.AddNewAnnot(-1, "Redact", rect) as AcroPDAnnot;

(I haven't be able to apply them though as annot.Perform(avDoc) does not seem to work. Ideas?)

In iTextSharp I can create simple text annotations like this

PdfAnnotation annotation = PdfAnnotation.CreateText(stamper.Writer, rect, "Title", "Content", false, null);

The only other option I found so was was to create black rectangles as explained here, but that doesn't remove the text (it can still be selected). I want to create redaction annotations and eventually apply redaction.

// Update:

As I finally got around to create a working example I wanted to share it here. It does not apply the redactions in the end but it creates valid redactions which are properly shown within Acrobat and can then be applied manually.

            using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            PdfReader pdfReader = new PdfReader(stream);
            // Create a stamper
            using (PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(newFileName, FileMode.OpenOrCreate)))
                {
                    // Add the annotations
                    int page = 1;
                    iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(500, 50, 200, 300);

                    PdfAnnotation annotation = new PdfAnnotation(stamper.Writer, rect);
                    annotation.Put(PdfName.SUBTYPE, new PdfName("Redact"));

                    annotation.Title = "My Author"; // Title = author
                    annotation.Put(new PdfName("Subj"), new PdfName("Redact")); // Redaction "Subject". When created in Acrobat, this is always set to "Redact"

                    float[] fillColor = { 0, 0, 0 }; // Black
                    annotation.Put(new PdfName("IC"), new PdfArray(fillColor)); // Interior color

                    float[] fillColorRed = { 1, 0, 0 }; // Red
                    annotation.Put(new PdfName("OC"), new PdfArray(fillColorRed)); // Outline color

                    stamper.AddAnnotation(annotation, page);
                }

        }

解决方案

Answer 1: Creating redaction annotations

iText is a toolbox that gives you the power to create any object you want. You are using a convenience method to create a Text annotation. That's scratching the surface.

You can use iText to create any type of annotation you want, because the PdfAnnotation class extends the PdfDictionary class.

This is explained in chapter 7 of my book "iText in Action - Second edition". GenericAnnotations is the example that illustrates this functionality.

If we port this example to C#, we have:

PdfAnnotation annotation = new PdfAnnotation(writer, rect);
annotation.Title = "Text annotation";
annotation.Put(PdfName.SUBTYPE, PdfName.TEXT);
annotation.Put(PdfName.OPEN, PdfBoolean.PDFFALSE);
annotation.Put(PdfName.CONTENTS,
  new PdfString(string.Format("Icon: {0}", text))
);
annotation.Put(PdfName.NAME, new PdfName(text));
writer.AddAnnotation(annotation);

This is a manual way to create a text annotation. You want a Redact annotations, so you'll need something like this:

PdfAnnotation annotation = new PdfAnnotation(writer, rect);
annotation.Put(PdfName.SUBTYPE, new PdfName("Redact"));
writer.AddAnnotation(annotation);

You can use the Put() method to add all the other keys you need for the annotation.

Answer 2: How to "apply" a redaction annotation

The second question requires the itext-xtra.jar (an extra jar shipped with iText) and you need at least iText 5.5.4. The approach to add opaque rectangles doesn't apply redaction: the redacted content is merely covered, not removed. You can still select the text and copy/paste it. If you're not careful, you risk ending up with a so-called PDF Blackout Folly. See for instance the NSA / AT&T scandal.

Suppose that you have a file to which you added some redaction annotations: page229_redacted.pdf

We can now use this code to remove the content marked by the redaction annotations:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(stamper);
    cleaner.cleanUp();
    stamper.close();
    reader.close();
}

This results in the following PDF: page229_apply_redacted.pdf

As you can see, the red rectangle borders are replaced by filled black rectangles. If you try to select the original text, you'll notice that is is no longer present.

这篇关于如何创建和应用删节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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