Duplicate Fields using itextsharp

611 Views Asked by At

I have this code to create 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();
    }

As you can see i have some code commented as an alternative but the two different ways are producing the same result.

What i am trying to achieve is create two textfields with the same name to when editing one it edits the others two. This two codes do that (in the browsers and pdfescape site) excepting in the adobe acrobat reader. In the acrobat reader i get just the first field visible and the others hidden i dont know why...

2

There are 2 best solutions below

6
On

If you want to add two text field visualizations which represent the same content, you have to add them as two widgets of the same field and not two distinct fields, e.g. like this:

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();
}

(As I don't have that RCRectangleRecord, I use the iTextSharp Rectangle class as argument.)

This gives you two field visualizations in Adobe Acrobat Reader; after editing one of them and switching focus (e.g. clicking somewhere outside that visualization), the other visualization duplicates the content.

1
On

Now i have this and i can create two fields when the list has more than one Rectangle but for some reason i dont know how the two fields appears without the name!!

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();