0

I am creating a duplicate of an existing TextField on another page of my pdf and i want the duplicate field to be readonly, so that the user can only change the value from the original field. Here is the section where i create the duplicate:

PdfPCell totalPriceCell = new PdfPCell()
{
    BorderWidthLeft = 0,
    BorderWidthRight = 0,
    BorderWidthTop = 0,
    BorderWidthBottom = 0f
};
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false);
TextField tFieldTotalPrice = new TextField(writer, new iTextSharp.text.Rectangle(0, 0), dupTotalPriceName)
{
    FieldName = dupTotalPriceName,
    Alignment = 1,
    FontSize = 12,
    Font = bf
};
tFieldTotalPrice.SetRotationFromPage(doc.PageSize);
FieldPositioningEvents eventsTotalPrice = new FieldPositioningEvents(writer, tFieldTotalPrice.GetTextField());
PdfFormField fieldTotalPrice = tFieldTotalPrice.GetTextField();
fieldTotalPrice.SetFieldFlags(PdfFormField.FF_READ_ONLY);
fieldTotalPrice.SetFieldFlags(PdfFormField.FLAGS_READONLY);
//Divide row number by 2 and keep remainder. If remainder is 0, even. Else, odd.
totalPriceCell.BackgroundColor = r % 2 == 0 ? BaseColor.WHITE : new BaseColor(245, 245, 245);
totalPriceCell.CellEvent = eventsTotalPrice;

Setting the flags this way doesn't work, and it's the only solution i can find on similar questions thus far.

Thanks for your time!

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
knutter539
  • 74
  • 8
  • 2
    You are correct: this isn't supposed to work. Take a look at ISO 32000. One field (the abstract key/value pair) can correspond with two different widget annotations (what you see in the document), but the read-only status cannot be defined at the level of the annotation, only at the level of the field. A field is either read-only or not read-only. If a field is read-only, so are all the widget annotations. If you don't understand this explanation, it can be summarized as *you're trying to do something that is impossible according to the PDF ISO Standard.* – Bruno Lowagie Apr 06 '18 at 14:20
  • 1
    As a work around, you could create two fields with a different name. Make one field editable and make the other field read-only. Then define an action that, when the editable field changes, the read-only field changes too. This can be done using JavaScript. – Bruno Lowagie Apr 06 '18 at 14:22
  • I understand, thank you very much for the explanation. I will attempt your work around – knutter539 Apr 06 '18 at 14:33

1 Answers1

0

As Bruno has explained wonderfully, this cannot be done according to the PDF ISO Standard.

knutter539
  • 74
  • 8