/** Sets the field value and the display string. The display string * is used to build the appearance in the cases where the value * is modified by Acrobat with JavaScript and the algorithm is * known. * @param name the fully qualified field name or the partial name in the case of XFA forms * @param value the field value * @param display the string that is used for the appearance. If <CODE>null</CODE> * the <CODE>value</CODE> parameter will be used * @return <CODE>true</CODE> if the field was found and changed, * <CODE>false</CODE> otherwise * @throws IOException on error * @throws DocumentException on error */ public bool SetField(String name, String value, String display) { if (writer == null) throw new DocumentException("This AcroFields instance is read-only."); if (xfa.XfaPresent) { name = xfa.FindFieldName(name, this); if (name == null) return false; String shortName = XfaForm.Xml2Som.GetShortName(name); XmlNode xn = xfa.FindDatasetsNode(shortName); if (xn == null) { xn = xfa.DatasetsSom.InsertNode(xfa.DatasetsNode, shortName); } xfa.SetNodeText(xn, value); } Item item = (Item)fields[name]; if (item == null) return false; PdfDictionary merged = item.GetMerged( 0 ); PdfName type = merged.GetAsName(PdfName.FT); if (PdfName.TX.Equals(type)) { PdfNumber maxLen = merged.GetAsNumber(PdfName.MAXLEN); int len = 0; if (maxLen != null) len = maxLen.IntValue; if (len > 0) value = value.Substring(0, Math.Min(len, value.Length)); } if (display == null) display = value; if (PdfName.TX.Equals(type) || PdfName.CH.Equals(type)) { PdfString v = new PdfString(value, PdfObject.TEXT_UNICODE); for (int idx = 0; idx < item.Size; ++idx) { PdfDictionary valueDic = item.GetValue(idx); valueDic.Put(PdfName.V, v); valueDic.Remove(PdfName.I); MarkUsed(valueDic); merged = item.GetMerged(idx); merged.Remove(PdfName.I); merged.Put(PdfName.V, v); PdfDictionary widget = item.GetWidget(idx); if (generateAppearances) { PdfAppearance app = GetAppearance(merged, display, name); if (PdfName.CH.Equals(type)) { PdfNumber n = new PdfNumber(topFirst); widget.Put(PdfName.TI, n); merged.Put(PdfName.TI, n); } PdfDictionary appDic = widget.GetAsDict(PdfName.AP); if (appDic == null) { appDic = new PdfDictionary(); widget.Put(PdfName.AP, appDic); merged.Put(PdfName.AP, appDic); } appDic.Put(PdfName.N, app.IndirectReference); writer.ReleaseTemplate(app); } else { widget.Remove(PdfName.AP); merged.Remove(PdfName.AP); } MarkUsed(widget); } return true; } else if (PdfName.BTN.Equals(type)) { PdfNumber ff = item.GetMerged(0).GetAsNumber(PdfName.FF); int flags = 0; if (ff != null) flags = ff.IntValue; if ((flags & PdfFormField.FF_PUSHBUTTON) != 0) { //we'll assume that the value is an image in base64 Image img; try { img = Image.GetInstance(Convert.FromBase64String(value)); } catch { return false; } PushbuttonField pb = GetNewPushbuttonFromField(name); pb.Image = img; ReplacePushbuttonField(name, pb.Field); return true; } PdfName v = new PdfName(value); ArrayList lopt = new ArrayList(); PdfArray opts = item.GetValue(0).GetAsArray(PdfName.OPT); if (opts != null) { for (int k = 0; k < opts.Size; ++k) { PdfString valStr = opts.GetAsString(k); if (valStr != null) lopt.Add(valStr.ToUnicodeString()); else lopt.Add(null); } } int vidx = lopt.IndexOf(value); PdfName valt = null; PdfName vt; if (vidx >= 0) { vt = valt = new PdfName(vidx.ToString()); } else vt = v; for (int idx = 0; idx < item.Size; ++idx) { merged = item.GetMerged(idx); PdfDictionary widget = item.GetWidget(idx); PdfDictionary valDict = item.GetValue(idx); MarkUsed(item.GetValue(idx)); if (valt != null) { PdfString ps = new PdfString(value, PdfObject.TEXT_UNICODE); valDict.Put(PdfName.V, ps); merged.Put(PdfName.V, ps); } else { valDict.Put(PdfName.V, v); merged.Put(PdfName.V, v); } MarkUsed(widget); if (IsInAP(widget, vt)) { merged.Put(PdfName.AS, vt); widget.Put(PdfName.AS, vt); } else { merged.Put(PdfName.AS, PdfName.Off_); widget.Put(PdfName.AS, PdfName.Off_); } } return true; } return false; }
/** * 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; }
/** * Constructs a new <CODE>PdfAnnotation</CODE> of subtype text. */ public PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ury, PdfString title, PdfString content) { this.writer = writer; Put(PdfName.SUBTYPE, PdfName.TEXT); Put(PdfName.T, title); Put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury)); Put(PdfName.CONTENTS, content); }
/** * Renames a field. Only the last part of the name can be renamed. For example, * if the original field is "ab.cd.ef" only the "ef" part can be renamed. * @param oldName the old field name * @param newName the new field name * @return <CODE>true</CODE> if the renaming was successful, <CODE>false</CODE> * otherwise */ public bool RenameField(String oldName, String newName) { int idx1 = oldName.LastIndexOf('.') + 1; int idx2 = newName.LastIndexOf('.') + 1; if (idx1 != idx2) return false; if (!oldName.Substring(0, idx1).Equals(newName.Substring(0, idx2))) return false; if (fields.ContainsKey(newName)) return false; Item item = (Item)fields[oldName]; if (item == null) return false; newName = newName.Substring(idx2); PdfString ss = new PdfString(newName, PdfObject.TEXT_UNICODE); item.WriteToAll( PdfName.T, ss, Item.WRITE_VALUE | Item.WRITE_MERGED); item.MarkUsed( this, Item.WRITE_VALUE ); fields.Remove(oldName); fields[newName] = item; return true; }
/** * Closes the document. No more content can be written after the * document is closed. * <p> * If closing a signed document with an external signature the closing must be done * in the <CODE>PdfSignatureAppearance</CODE> instance. * @throws DocumentException on error * @throws IOException on error */ public void Close() { if (!hasSignature) { stamper.Close(moreInfo); return; } sigApp.PreClose(); PdfSigGenericPKCS sig = sigApp.SigStandard; PdfLiteral lit = (PdfLiteral)sig.Get(PdfName.CONTENTS); int totalBuf = (lit.PosLength - 2) / 2; byte[] buf = new byte[8192]; int n; Stream inp = sigApp.RangeStream; while ((n = inp.Read(buf, 0, buf.Length)) > 0) { sig.Signer.Update(buf, 0, n); } buf = new byte[totalBuf]; byte[] bsig = sig.SignerContents; Array.Copy(bsig, 0, buf, 0, bsig.Length); PdfString str = new PdfString(buf); str.SetHexWriting(true); PdfDictionary dic = new PdfDictionary(); dic.Put(PdfName.CONTENTS, str); sigApp.Close(dic); stamper.reader.Close(); }
/** * Reads a pdf object. * @return the pdf object * @throws IOException on error */ public PdfObject ReadPRObject() { if (!NextValidToken()) return null; int type = tokeniser.TokenType; switch (type) { case PRTokeniser.TK_START_DIC: { PdfDictionary dic = ReadDictionary(); return dic; } case PRTokeniser.TK_START_ARRAY: return ReadArray(); case PRTokeniser.TK_STRING: PdfString str = new PdfString(tokeniser.StringValue, null).SetHexWriting(tokeniser.IsHexString()); return str; case PRTokeniser.TK_NAME: return new PdfName(tokeniser.StringValue, false); case PRTokeniser.TK_NUMBER: return new PdfNumber(tokeniser.StringValue); case PRTokeniser.TK_OTHER: return new PdfLiteral(COMMAND_TYPE, tokeniser.StringValue); default: return new PdfLiteral(-type, tokeniser.StringValue); } }
private String DecodeString(PdfString ps) { if (ps.IsHexWriting()) return PdfEncodings.ConvertToString(ps.GetBytes(), "UnicodeBigUnmarked"); else return ps.ToUnicodeString(); }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. * * @param parent the parent of this outline item * @param destination the destination for this outline item * @param title the title of this outline item * @param open <CODE>true</CODE> if the children are visible */ public PdfOutline(PdfOutline parent, PdfDestination destination, PdfString title, bool open) : this(parent, destination, title.ToString(), true) { }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. The open mode is * <CODE>true</CODE>. * * @param parent the parent of this outline item * @param destination the destination for this outline item * @param title the title of this outline item */ public PdfOutline(PdfOutline parent, PdfDestination destination, PdfString title) : this(parent, destination, title, true) { }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. * * @param parent the parent of this outline item * @param action the <CODE>PdfAction</CODE> for this outline item * @param title the title of this outline item * @param open <CODE>true</CODE> if the children are visible */ public PdfOutline(PdfOutline parent, PdfAction action, PdfString title, bool open) : this(parent, action, title.ToString(), open) { }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. The open mode is * <CODE>true</CODE>. * * @param parent the parent of this outline item * @param action the <CODE>PdfAction</CODE> for this outline item * @param title the title of this outline item */ public PdfOutline(PdfOutline parent, PdfAction action, PdfString title) : this(parent, action, title, true) { }