如何使用iTextSharp C#标记/取消标记,写为接受/拒绝/取消文本注释 [英] How do I mark/unmark, write as Accepted/Rejected/Cancelled a text annotation using iTextSharp C#

查看:158
本文介绍了如何使用iTextSharp C#标记/取消标记,写为接受/拒绝/取消文本注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio上使用iTextSharp。我创建了一个像这样的Pdfannotation:

  PdfAnnotation annotation = PdfAnnotation.CreateText(stamper.Writer,rect,Author,I写我的文字,真实,新段落); 
stamper.AddAnnotation(annotation,1);

所以我的注释内容为我写我的文字。
当我使用Adobe Acrobat Reader打开我的pdf时,当我点击评论按钮查看我在pdf上写的所有评论时,我看到了我的评论并且接近我的评论我写了我的文字,我看到一个小方块,我可以检查或取消选中。
这是我创建Pdf注释时自动创建的一个小复选框。我没有自己创建它。



我想使用iTextSharp检查或取消选中这个小复选框。



<我考虑过这样做,但它不起作用:

  RadioCheckField checkbox = new RadioCheckField(stamper.Writer,rect ,bonjour,on); 
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
checkbox.Checked = true;
PdfFormField field = checkbox.CheckField;

annotation.Put(PdfName.A,field);

有谁知道怎么做?



非常感谢你!



祝你有个美好的一天! :)

解决方案

这个问题有点混乱,因为在文本的上下文中使用术语检查/取消选中(或粘贴)注)注释。正确的术语应该是:如何标记/取消标记文本注释?



立即检查/取消选中会让我们考虑复选框,但是下面的屏幕截图显示了当我们谈论标记文本注释时的含义:





标记文本注释不是检查复选框的问题。通过添加隐藏的在回复中(IRT)注释来标记文本注释。请参阅



此表引用标题为注释状态的表:





我们使用了组合 StateModel = Marked; State = Marked ,表示注释已由用户标记。我没有在规范中找到任何参考,这个标记只能在标记文档的用户的机器上看到。



发现这个后,我创建了



如您所见,评论面板中会出现绿色复选标记。它在登录用户不是Bruno的计算机上显示Bruno Accepted。您可以在此处自行查看: hello_accepted.pdf


I'm working with iTextSharp on Visual Studio. I created a Pdfannotation like that:

PdfAnnotation annotation = PdfAnnotation.CreateText(stamper.Writer, rect, "Author", "I write my text", true, "New Paragraph");
stamper.AddAnnotation(annotation, 1);

So my annotation contents a text "I write my text". When I go on Adobe Acrobat Reader to open my pdf, and when I click on the button "Comment" to see all the comments I wrote on my pdf, I see my comment and near to my comment "I write my text", I see a little square that I can check or uncheck. This is a little Checkbox which was automatically created when I created my Pdf annotation. I didn't create it by myself.

I would like to check or uncheck this little checkbox using iTextSharp.

I thought about doing that, but it doesn't work :

  RadioCheckField checkbox = new RadioCheckField(stamper.Writer, rect, "bonjour", "on");
        checkbox.CheckType = RadioCheckField.TYPE_CHECK;
        checkbox.Checked = true;
        PdfFormField field = checkbox.CheckField;

        annotation.Put(PdfName.A, field);

Does anyone know how to do it ?

Thank you a lot!

Have a good day! :)

解决方案

The question was somewhat confusion because of the terminology "check/uncheck" in the context of a text (or Sticky Note) annotation. The correct terminology would have been: How to I mark/unmark a text annotation?

Checking/unchecking immediately makes us think about check boxes, but the following screen shot shows what is meant when we talk about marking a text annotation:

Marking a text annotation isn't a matter of checking a check box. Marking a text annotation is done by adding a hidden "In Reply To" (IRT) annotation. See How to add an "In Reply To" annotation? on the official site for more information about "In Reply To" annotations.

I've adapted the AddInReplyTo example with the AddMarked as result:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfDictionary page = reader.getPageN(1);
    PdfArray annots = page.getAsArray(PdfName.ANNOTS);
    PdfDictionary sticky = annots.getAsDict(0);
    PdfArray stickyRect = sticky.getAsArray(PdfName.RECT);
    PdfDictionary popup = annots.getAsDict(1);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfWriter writer = stamper.getWriter();
    Rectangle stickyRectangle = new Rectangle(
        stickyRect.getAsNumber(0).floatValue(), stickyRect.getAsNumber(1).floatValue(),
        stickyRect.getAsNumber(2).floatValue(), stickyRect.getAsNumber(3).floatValue()
    );
    PdfAnnotation replySticky = PdfAnnotation.createText(
            writer, stickyRectangle, "Bruno", "Marked set by Bruno", false, "Comment");
    replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));
    replySticky.put(PdfName.STATE, new PdfString("Marked"));
    PdfNumber n = sticky.getAsNumber(PdfName.F);
    replySticky.put(PdfName.F, new PdfNumber(n.intValue() | PdfAnnotation.FLAGS_HIDDEN));
    replySticky.put(new PdfName("StateModel"), new PdfString("Marked"));
    stamper.addAnnotation(replySticky, 1);
    stamper.close();
}

The example is in Java, but it should be fairly easy to adapt it to C#. It's important to know that marking the original annotation sticky is done by adding an extra annotation replySticky. The difference with an ordinary IRT annotation, is that we are going to hide the annotation by adding FLAGS_HIDDEN to the flags of the annotation. We also set the /State to Marked and the /StateModel to Marked.

This code turns hello_sticky_note.pdf into hello_marked.pdf as was requested, but there's a catch! The check box will only be visible if you are logged in as user "Bruno". This check box is for personal use only.

If you want others to see a review status, you shouldn't use the "Marked" feature. Instead you should use "Review". This is poorly documented in ISO-32000. See the table with title "Additional entries specific to a text annotation":

This table refers to the table with title "Annotation States":

We have used the combination StateModel = Marked; State = Marked, which means that the annotation has been marked by the user. I didn't find any reference in the spec that this mark is only visible on the machine of the user who marked the document.

After discovering this, I've created the AddAccepted example:

PdfAnnotation replySticky = PdfAnnotation.createText(
        writer, stickyRectangle, "Bruno", "Accepted by Bruno", false, "Comment");
replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));
replySticky.put(PdfName.STATE, new PdfString("Accepted"));
PdfNumber n = sticky.getAsNumber(PdfName.F);
replySticky.put(PdfName.F, new PdfNumber(n.intValue() | PdfAnnotation.FLAGS_HIDDEN));
replySticky.put(new PdfName("StateModel"), new PdfString("Review"));
stamper.addAnnotation(replySticky, 1);
stamper.close();

This example is identical to what we had before, except that we now use the combination: StateModel = Review; State = Accepted. As you can tell from the "Annotation states" table, other possible options for the State are "Rejected", "Cancelled", "Completed" and "None" (which is the default value).

The result looks like this:

As you can see, a green check mark appears in the comments panel. It shows "Bruno Accepted" on a computer where the logged in user isn't Bruno. You can check this for yourself here: hello_accepted.pdf

这篇关于如何使用iTextSharp C#标记/取消标记,写为接受/拒绝/取消文本注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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