/**
 * Sets a field property. Valid property names are:
 * <p>
 * <ul>
 * <li>textfont - sets the text font. The value for this entry is a <CODE>BaseFont</CODE>.<br>
 * <li>textcolor - sets the text color. The value for this entry is a <CODE>java.awt.Color</CODE>.<br>
 * <li>textsize - sets the text size. The value for this entry is a <CODE>Float</CODE>.
 * <li>bgcolor - sets the background color. The value for this entry is a <CODE>java.awt.Color</CODE>.
 *     If <code>null</code> removes the background.<br>
 * <li>bordercolor - sets the border color. The value for this entry is a <CODE>java.awt.Color</CODE>.
 *     If <code>null</code> removes the border.<br>
 * </ul>
 * @param field the field name
 * @param name the property name
 * @param value the property value
 * @param inst an array of <CODE>int</CODE> indexing into <CODE>AcroField.Item.merged</CODE> elements to process.
 * Set to <CODE>null</CODE> to process all
 * @return <CODE>true</CODE> if the property exists, <CODE>false</CODE> otherwise
 */
 public bool SetFieldProperty(String field, String name, Object value, int[] inst)
 {
     if (writer == null)
         throw new Exception("This AcroFields instance is read-only.");
     Item item = (Item)fields[field];
     if (item == null)
         return false;
     InstHit hit = new InstHit(inst);
     PdfDictionary merged;
     PdfString da;
     if (Util.EqualsIgnoreCase(name, "textfont")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 merged = item.GetMerged( k );
                 da = merged.GetAsString(PdfName.DA);
                 PdfDictionary dr = merged.GetAsDict(PdfName.DR);
                 if (da != null && dr != null) {
                     Object[] dao = SplitDAelements(da.ToUnicodeString());
                     PdfAppearance cb = new PdfAppearance();
                     if (dao[DA_FONT] != null) {
                         BaseFont bf = (BaseFont)value;
                         PdfName psn = (PdfName)PdfAppearance.stdFieldFontNames[bf.PostscriptFontName];
                         if (psn == null) {
                             psn = new PdfName(bf.PostscriptFontName);
                         }
                         PdfDictionary fonts = dr.GetAsDict(PdfName.FONT);
                         if (fonts == null) {
                             fonts = new PdfDictionary();
                             dr.Put(PdfName.FONT, fonts);
                         }
                         PdfIndirectReference fref = (PdfIndirectReference)fonts.Get(psn);
                         PdfDictionary top = reader.Catalog.GetAsDict(PdfName.ACROFORM);
                         MarkUsed(top);
                         dr = top.GetAsDict(PdfName.DR);
                         if (dr == null) {
                             dr = new PdfDictionary();
                             top.Put(PdfName.DR, dr);
                         }
                         MarkUsed(dr);
                         PdfDictionary fontsTop = dr.GetAsDict(PdfName.FONT);
                         if (fontsTop == null) {
                             fontsTop = new PdfDictionary();
                             dr.Put(PdfName.FONT, fontsTop);
                         }
                         MarkUsed(fontsTop);
                         PdfIndirectReference frefTop = (PdfIndirectReference)fontsTop.Get(psn);
                         if (frefTop != null) {
                             if (fref == null)
                                 fonts.Put(psn, frefTop);
                         }
                         else if (fref == null) {
                             FontDetails fd;
                             if (bf.FontType == BaseFont.FONT_TYPE_DOCUMENT) {
                                 fd = new FontDetails(null, ((DocumentFont)bf).IndirectReference, bf);
                             }
                             else {
                                 bf.Subset = false;
                                 fd = writer.AddSimple(bf);
                                 localFonts[psn.ToString().Substring(1)] = bf;
                             }
                             fontsTop.Put(psn, fd.IndirectReference);
                             fonts.Put(psn, fd.IndirectReference);
                         }
                         ByteBuffer buf = cb.InternalBuffer;
                         buf.Append(psn.GetBytes()).Append(' ').Append((float)dao[DA_SIZE]).Append(" Tf ");
                         if (dao[DA_COLOR] != null)
                             cb.SetColorFill((Color)dao[DA_COLOR]);
                         PdfString s = new PdfString(cb.ToString());
                         item.GetMerged(k).Put(PdfName.DA, s);
                         item.GetWidget(k).Put(PdfName.DA, s);
                         MarkUsed(item.GetWidget(k));
                     }
                 }
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "textcolor")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 merged = item.GetMerged( k );
                 da = merged.GetAsString(PdfName.DA);
                 if (da != null) {
                     Object[] dao = SplitDAelements(da.ToUnicodeString());
                     PdfAppearance cb = new PdfAppearance();
                     if (dao[DA_FONT] != null) {
                         ByteBuffer buf = cb.InternalBuffer;
                         buf.Append(new PdfName((String)dao[DA_FONT]).GetBytes()).Append(' ').Append((float)dao[DA_SIZE]).Append(" Tf ");
                         cb.SetColorFill((Color)value);
                         PdfString s = new PdfString(cb.ToString());
                         item.GetMerged(k).Put(PdfName.DA, s);
                         item.GetWidget(k).Put(PdfName.DA, s);
                         MarkUsed(item.GetWidget(k));
                     }
                 }
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "textsize")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 merged = item.GetMerged( k );
                 da = merged.GetAsString(PdfName.DA);
                 if (da != null) {
                     Object[] dao = SplitDAelements(da.ToUnicodeString());
                     PdfAppearance cb = new PdfAppearance();
                     if (dao[DA_FONT] != null) {
                         ByteBuffer buf = cb.InternalBuffer;
                         buf.Append(new PdfName((String)dao[DA_FONT]).GetBytes()).Append(' ').Append((float)value).Append(" Tf ");
                         if (dao[DA_COLOR] != null)
                             cb.SetColorFill((Color)dao[DA_COLOR]);
                         PdfString s = new PdfString(cb.ToString());
                         item.GetMerged(k).Put(PdfName.DA, s);
                         item.GetWidget(k).Put(PdfName.DA, s);
                         MarkUsed(item.GetWidget(k));
                     }
                 }
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "bgcolor") || Util.EqualsIgnoreCase(name, "bordercolor")) {
         PdfName dname = (Util.EqualsIgnoreCase(name, "bgcolor") ? PdfName.BG : PdfName.BC);
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 merged = item.GetMerged( k );
                 PdfDictionary mk = merged.GetAsDict(PdfName.MK);
                 if (mk == null) {
                     if (value == null)
                         return true;
                     mk = new PdfDictionary();
                     item.GetMerged(k).Put(PdfName.MK, mk);
                     item.GetWidget(k).Put(PdfName.MK, mk);
                     MarkUsed(item.GetWidget(k));
                 } else {
                     MarkUsed( mk );
                 }
                 if (value == null)
                     mk.Remove(dname);
                 else
                     mk.Put(dname, PdfFormField.GetMKColor((Color)value));
             }
         }
     }
     else
         return false;
     return true;
 }
 /**
 * Sets a field property. Valid property names are:
 * <p>
 * <ul>
 * <li>flags - a set of flags specifying various characteristics of the field�s widget annotation.
 * The value of this entry replaces that of the F entry in the form�s corresponding annotation dictionary.<br>
 * <li>setflags - a set of flags to be set (turned on) in the F entry of the form�s corresponding
 * widget annotation dictionary. Bits equal to 1 cause the corresponding bits in F to be set to 1.<br>
 * <li>clrflags - a set of flags to be cleared (turned off) in the F entry of the form�s corresponding
 * widget annotation dictionary. Bits equal to 1 cause the corresponding
 * bits in F to be set to 0.<br>
 * <li>fflags - a set of flags specifying various characteristics of the field. The value
 * of this entry replaces that of the Ff entry in the form�s corresponding field dictionary.<br>
 * <li>setfflags - a set of flags to be set (turned on) in the Ff entry of the form�s corresponding
 * field dictionary. Bits equal to 1 cause the corresponding bits in Ff to be set to 1.<br>
 * <li>clrfflags - a set of flags to be cleared (turned off) in the Ff entry of the form�s corresponding
 * field dictionary. Bits equal to 1 cause the corresponding bits in Ff
 * to be set to 0.<br>
 * </ul>
 * @param field the field name
 * @param name the property name
 * @param value the property value
 * @param inst an array of <CODE>int</CODE> indexing into <CODE>AcroField.Item.merged</CODE> elements to process.
 * Set to <CODE>null</CODE> to process all
 * @return <CODE>true</CODE> if the property exists, <CODE>false</CODE> otherwise
 */
 public bool SetFieldProperty(String field, String name, int value, int[] inst)
 {
     if (writer == null)
         throw new Exception("This AcroFields instance is read-only.");
     Item item = (Item)fields[field];
     if (item == null)
         return false;
     InstHit hit = new InstHit(inst);
     if (Util.EqualsIgnoreCase(name, "flags")) {
         PdfNumber num = new PdfNumber(value);
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 item.GetMerged(k).Put(PdfName.F, num);
                 item.GetWidget(k).Put(PdfName.F, num);
                 MarkUsed(item.GetWidget(k));
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "setflags")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = item.GetWidget(k).GetAsNumber(PdfName.F);
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val | value);
                 item.GetMerged(k).Put(PdfName.F, num);
                 item.GetWidget(k).Put(PdfName.F, num);
                 MarkUsed(item.GetWidget(k));
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "clrflags")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 PdfDictionary widget = item.GetWidget( k );
                 PdfNumber num = widget.GetAsNumber(PdfName.F);
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val & (~value));
                 item.GetMerged(k).Put(PdfName.F, num);
                 widget.Put(PdfName.F, num);
                 MarkUsed(widget);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "fflags")) {
         PdfNumber num = new PdfNumber(value);
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 item.GetMerged(k).Put(PdfName.FF, num);
                 item.GetValue(k).Put(PdfName.FF, num);
                 MarkUsed(item.GetValue(k));
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "setfflags")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 PdfDictionary valDict = item.GetValue( k );
                 PdfNumber num = valDict.GetAsNumber( PdfName.FF );
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val | value);
                 item.GetMerged(k).Put(PdfName.FF, num);
                 valDict.Put(PdfName.FF, num);
                 MarkUsed(valDict);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "clrfflags")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 PdfDictionary valDict = item.GetValue( k );
                 PdfNumber num = valDict.GetAsNumber(PdfName.FF);
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val & (~value));
                 item.GetMerged(k).Put(PdfName.FF, num);
                 valDict.Put(PdfName.FF, num);
                 MarkUsed(valDict);
             }
         }
     }
     else
         return false;
     return true;
 }
示例#3
0
 /**
 * Sets a field property. Valid property names are:
 * <p>
 * <ul>
 * <li>flags - a set of flags specifying various characteristics of the field’s widget annotation.
 * The value of this entry replaces that of the F entry in the form’s corresponding annotation dictionary.<br>
 * <li>setflags - a set of flags to be set (turned on) in the F entry of the form’s corresponding
 * widget annotation dictionary. Bits equal to 1 cause the corresponding bits in F to be set to 1.<br>
 * <li>clrflags - a set of flags to be cleared (turned off) in the F entry of the form’s corresponding
 * widget annotation dictionary. Bits equal to 1 cause the corresponding
 * bits in F to be set to 0.<br>
 * <li>fflags - a set of flags specifying various characteristics of the field. The value
 * of this entry replaces that of the Ff entry in the form’s corresponding field dictionary.<br>
 * <li>setfflags - a set of flags to be set (turned on) in the Ff entry of the form’s corresponding
 * field dictionary. Bits equal to 1 cause the corresponding bits in Ff to be set to 1.<br>
 * <li>clrfflags - a set of flags to be cleared (turned off) in the Ff entry of the form’s corresponding
 * field dictionary. Bits equal to 1 cause the corresponding bits in Ff
 * to be set to 0.<br>
 * </ul>
 * @param field the field name
 * @param name the property name
 * @param value the property value
 * @param inst an array of <CODE>int</CODE> indexing into <CODE>AcroField.Item.merged</CODE> elements to process.
 * Set to <CODE>null</CODE> to process all
 * @return <CODE>true</CODE> if the property exists, <CODE>false</CODE> otherwise
 */
 public bool SetFieldProperty(String field, String name, int value, int[] inst)
 {
     if (writer == null)
         throw new Exception("This AcroFields instance is read-only.");
     Item item = (Item)fields[field];
     if (item == null)
         return false;
     InstHit hit = new InstHit(inst);
     if (Util.EqualsIgnoreCase(name, "flags")) {
         PdfNumber num = new PdfNumber(value);
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 ((PdfDictionary)item.merged[k]).Put(PdfName.F, num);
                 ((PdfDictionary)item.widgets[k]).Put(PdfName.F, num);
                 MarkUsed((PdfDictionary)item.widgets[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "setflags")) {
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.widgets[k]).Get(PdfName.F));
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val | value);
                 ((PdfDictionary)item.merged[k]).Put(PdfName.F, num);
                 ((PdfDictionary)item.widgets[k]).Put(PdfName.F, num);
                 MarkUsed((PdfDictionary)item.widgets[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "clrflags")) {
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.widgets[k]).Get(PdfName.F));
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val & (~value));
                 ((PdfDictionary)item.merged[k]).Put(PdfName.F, num);
                 ((PdfDictionary)item.widgets[k]).Put(PdfName.F, num);
                 MarkUsed((PdfDictionary)item.widgets[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "fflags")) {
         PdfNumber num = new PdfNumber(value);
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 ((PdfDictionary)item.merged[k]).Put(PdfName.FF, num);
                 ((PdfDictionary)item.values[k]).Put(PdfName.FF, num);
                 MarkUsed((PdfDictionary)item.values[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "setfflags")) {
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.values[k]).Get(PdfName.FF));
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val | value);
                 ((PdfDictionary)item.merged[k]).Put(PdfName.FF, num);
                 ((PdfDictionary)item.values[k]).Put(PdfName.FF, num);
                 MarkUsed((PdfDictionary)item.values[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "clrfflags")) {
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.values[k]).Get(PdfName.FF));
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val & (~value));
                 ((PdfDictionary)item.merged[k]).Put(PdfName.FF, num);
                 ((PdfDictionary)item.values[k]).Put(PdfName.FF, num);
                 MarkUsed((PdfDictionary)item.values[k]);
             }
         }
     }
     else
         return false;
     return true;
 }