使用itextsharp重复字段

时间:2016-12-29 17:42:09

标签: itext adobe acrobat

我有这个代码来创建TextFields

public void MssCreateTextField(byte[] ssPdf, RCRectangleRecord ssRectangle, string ssName, int ssFontSize, string ssValue, int ssPage, out byte[] ssPdfOut, bool ssIsMultiline) {

        PdfReader reader = new PdfReader(ssPdf);
        ssPdfOut = null;

        var output = new MemoryStream();
        var stamper = new PdfStamper(reader, output);

        /*TextField tField = new TextField(stamper.Writer, new iTextSharp.text.Rectangle((float)ssRectangle.ssSTRectangle.ssllx, (float)ssRectangle.ssSTRectangle.sslly, (float)ssRectangle.ssSTRectangle.ssurx, (float)ssRectangle.ssSTRectangle.ssury), ssName);




        if (ssValue!="")
            tField.Text = ssValue;

        if (ssIsMultiline)
            tField.Options = TextField.MULTILINE;


         tField.FontSize = ssFontSize;*/

        PdfFormField tField = PdfFormField.CreateTextField(stamper.Writer, ssIsMultiline, false, 50);

        tField.FieldName = ssName;
        tField.SetWidget(new iTextSharp.text.Rectangle((float)ssRectangle.ssSTRectangle.ssllx, (float)ssRectangle.ssSTRectangle.sslly, (float)ssRectangle.ssSTRectangle.ssurx, (float)ssRectangle.ssSTRectangle.ssury), PdfAnnotation.HIGHLIGHT_TOGGLE);



         stamper.FormFlattening = false;
        stamper.AddAnnotation(tField, ssPage);


        stamper.Close();
        reader.Close();

        ssPdfOut = output.ToArray();
    }

正如您所看到的,我将一些代码作为替代方案进行了评论,但两种不同的方式产生了相同的结果。

我想要实现的是创建两个具有相同名称的文本字段,当编辑一个时,它编辑其他两个文本字段。除了adobe acrobat reader之外,这两个代码(在浏览器和pdfescape网站中)都是这样做的。在acrobat阅读器中,我只看到第一个可见的区域,其他隐藏的区域,我不知道为什么......

2 个答案:

答案 0 :(得分:0)

如果要添加两个代表相同内容的文本字段可视化,则必须将它们添加为同一字段的两个小部件不是两个不同的字段,例如像这样:

public void CreateDoubleTextField(byte[] ssPdf, Rectangle ssRectangle1, Rectangle ssRectangle2, string ssName, int ssFontSize, string ssValue, int ssPage, out byte[] ssPdfOut, bool ssIsMultiline)
{
    PdfReader reader = new PdfReader(ssPdf);
    ssPdfOut = null;

    var output = new MemoryStream();
    var stamper = new PdfStamper(reader, output);

    PdfFormField tField = PdfFormField.CreateTextField(stamper.Writer, ssIsMultiline, false, 50);
    tField.FieldName = ssName;

    PdfFormField widget1 = PdfFormField.CreateEmpty(stamper.Writer);
    widget1.SetWidget(ssRectangle1, PdfAnnotation.HIGHLIGHT_TOGGLE);
    PdfFormField widget2 = PdfFormField.CreateEmpty(stamper.Writer);
    widget2.SetWidget(ssRectangle2, PdfAnnotation.HIGHLIGHT_TOGGLE);

    tField.AddKid(widget1);
    tField.AddKid(widget2);

    stamper.FormFlattening = false;
    stamper.AddAnnotation(tField, ssPage);

    stamper.Close();
    reader.Close();

    ssPdfOut = output.ToArray();
}

(由于我没有RCRectangleRecord,我使用iTextSharp Rectangle类作为参数。)

这为您提供了Adobe Acrobat Reader中的两个现场可视化;在编辑其中一个并切换焦点(例如,单击该可视化之外的某个位置)之后,另一个可视化将复制内容。

答案 1 :(得分:0)

现在我有了这个,当列表有多个矩形时我可以创建两个字段但由于某种原因我不知道这两个字段是如何出现而没有名字!!

PdfReader reader = new PdfReader(ssPdf);
        ssPdfOut = null;

        var output = new MemoryStream();
        var stamper = new PdfStamper(reader, output);

        TextField tField;


        if (ssRectangle.Count==1){

            tField= new TextField(stamper.Writer, new iTextSharp.text.Rectangle((float)ssRectangle[0].ssSTRectangle.ssllx, (float)ssRectangle[0].ssSTRectangle.sslly, (float)ssRectangle[0].ssSTRectangle.ssurx, (float)ssRectangle[0].ssSTRectangle.ssury), ssName);
            if (ssValue!="")
            tField.Text = ssValue;

            if (ssIsMultiline)
                tField.Options = TextField.MULTILINE;


             tField.FontSize = ssFontSize;

             tField.FieldName = ssName;

             stamper.AddAnnotation(tField.GetTextField(), ssPage);
        }


        else
        {

            PdfFormField PtField = PdfFormField.CreateTextField(stamper.Writer, ssIsMultiline, false, 250);
            PtField.Name=ssName;
            foreach (RCRectangleRecord item in ssRectangle)
                {
                /*
                    tField=new TextField(stamper.Writer, new iTextSharp.text.Rectangle((float)ssRectangle[0].ssSTRectangle.ssllx, (float)ssRectangle[0].ssSTRectangle.sslly, (float)ssRectangle[0].ssSTRectangle.ssurx, (float)ssRectangle[0].ssSTRectangle.ssury), ssName);
                    tField.FieldName = ssName;
                    PtField.AddKid(tField.GetTextField());*/

                    PdfFormField widget = PdfFormField.CreateEmpty(stamper.Writer);
                    widget.SetWidget(new Rectangle((float)item.ssSTRectangle.ssllx, (float)item.ssSTRectangle.sslly, (float)item.ssSTRectangle.ssurx, (float)item.ssSTRectangle.ssury), PdfAnnotation.HIGHLIGHT_TOGGLE);
                    widget.Name = ssName;
                    PtField.AddKid(widget);
                }

            stamper.AddAnnotation(PtField, ssPage);

        }



        stamper.FormFlattening = false;


        stamper.Close();
        reader.Close();

        ssPdfOut = output.ToArray();