/** * Sets the XFA key from a byte array. The old XFA is erased. * @param form the data * @param reader the reader * @param writer the writer * @throws java.io.IOException on error */ public static void SetXfa(XfaForm form, PdfReader reader, PdfWriter writer) { PdfDictionary af = (PdfDictionary)PdfReader.GetPdfObjectRelease(reader.Catalog.Get(PdfName.ACROFORM)); if (af == null) { return; } PdfObject xfa = GetXfaObject(reader); if (xfa.IsArray()) { PdfArray ar = (PdfArray)xfa; int t = -1; int d = -1; for (int k = 0; k < ar.Size; k += 2) { PdfString s = ar.GetAsString(k); if ("template".Equals(s.ToString())) { t = k + 1; } if ("datasets".Equals(s.ToString())) { d = k + 1; } } if (t > -1 && d > -1) { reader.KillXref(ar.GetAsIndirectObject(t)); reader.KillXref(ar.GetAsIndirectObject(d)); PdfStream tStream = new PdfStream(SerializeDoc(form.templateNode)); tStream.FlateCompress(writer.CompressionLevel); ar[t] = writer.AddToBody(tStream).IndirectReference; PdfStream dStream = new PdfStream(SerializeDoc(form.datasetsNode)); dStream.FlateCompress(writer.CompressionLevel); ar[d] = writer.AddToBody(dStream).IndirectReference; af.Put(PdfName.XFA, new PdfArray(ar)); return; } } reader.KillXref(af.Get(PdfName.XFA)); PdfStream str = new PdfStream(SerializeDoc(form.domDocument)); str.FlateCompress(writer.CompressionLevel); PdfIndirectReference refe = writer.AddToBody(str).IndirectReference; af.Put(PdfName.XFA, refe); }
private void FillMetrics(byte[] touni, IntHashtable widths, int dw) { PdfContentParser ps = new PdfContentParser(new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(touni)))); PdfObject ob = null; bool notFound = true; int nestLevel = 0; int maxExc = 50; while ((notFound || nestLevel > 0)) { try { ob = ps.ReadPRObject(); } catch { if (--maxExc < 0) { break; } continue; } if (ob == null) { break; } if (ob.Type == PdfContentParser.COMMAND_TYPE) { if (ob.ToString().Equals("begin")) { notFound = false; nestLevel++; } else if (ob.ToString().Equals("end")) { nestLevel--; } else if (ob.ToString().Equals("beginbfchar")) { while (true) { PdfObject nx = ps.ReadPRObject(); if (nx.ToString().Equals("endbfchar")) { break; } String cid = DecodeString((PdfString)nx); String uni = DecodeString((PdfString)ps.ReadPRObject()); if (uni.Length == 1) { int cidc = (int)cid[0]; int unic = (int)uni[uni.Length - 1]; int w = dw; if (widths.ContainsKey(cidc)) { w = widths[cidc]; } metrics[unic] = new int[] { cidc, w }; } } } else if (ob.ToString().Equals("beginbfrange")) { while (true) { PdfObject nx = ps.ReadPRObject(); if (nx.ToString().Equals("endbfrange")) { break; } String cid1 = DecodeString((PdfString)nx); String cid2 = DecodeString((PdfString)ps.ReadPRObject()); int cid1c = (int)cid1[0]; int cid2c = (int)cid2[0]; PdfObject ob2 = ps.ReadPRObject(); if (ob2.IsString()) { String uni = DecodeString((PdfString)ob2); if (uni.Length == 1) { int unic = (int)uni[uni.Length - 1]; for (; cid1c <= cid2c; cid1c++, unic++) { int w = dw; if (widths.ContainsKey(cid1c)) { w = widths[cid1c]; } metrics[unic] = new int[] { cid1c, w }; } } } else { PdfArray a = (PdfArray)ob2; for (int j = 0; j < a.Size; ++j, ++cid1c) { String uni = DecodeString(a.GetAsString(j)); if (uni.Length == 1) { int unic = (int)uni[uni.Length - 1]; int w = dw; if (widths.ContainsKey(cid1c)) { w = widths[cid1c]; } metrics[unic] = new int[] { cid1c, w }; } } } } } } } }
private void FillMetrics(byte[] touni, IntHashtable widths, int dw) { PdfContentParser ps = new PdfContentParser(new PRTokeniser(touni)); PdfObject ob = null; PdfObject last = null; while ((ob = ps.ReadPRObject()) != null) { if (ob.Type == PdfContentParser.COMMAND_TYPE) { if (ob.ToString().Equals("beginbfchar")) { int n = ((PdfNumber)last).IntValue; for (int k = 0; k < n; ++k) { String cid = DecodeString((PdfString)ps.ReadPRObject()); String uni = DecodeString((PdfString)ps.ReadPRObject()); if (uni.Length == 1) { int cidc = (int)cid[0]; int unic = (int)uni[uni.Length - 1]; int w = dw; if (widths.ContainsKey(cidc)) { w = widths[cidc]; } metrics[unic] = new int[] { cidc, w }; } } } else if (ob.ToString().Equals("beginbfrange")) { int n = ((PdfNumber)last).IntValue; for (int k = 0; k < n; ++k) { String cid1 = DecodeString((PdfString)ps.ReadPRObject()); String cid2 = DecodeString((PdfString)ps.ReadPRObject()); int cid1c = (int)cid1[0]; int cid2c = (int)cid2[0]; PdfObject ob2 = ps.ReadPRObject(); if (ob2.IsString()) { String uni = DecodeString((PdfString)ob2); if (uni.Length == 1) { int unic = (int)uni[uni.Length - 1]; for (; cid1c <= cid2c; cid1c++, unic++) { int w = dw; if (widths.ContainsKey(cid1c)) { w = widths[cid1c]; } metrics[unic] = new int[] { cid1c, w }; } } } else { PdfArray a = (PdfArray)ob2; for (int j = 0; j < a.Size; ++j, ++cid1c) { String uni = DecodeString(a.GetAsString(j)); if (uni.Length == 1) { int unic = (int)uni[uni.Length - 1]; int w = dw; if (widths.ContainsKey(cid1c)) { w = widths[cid1c]; } metrics[unic] = new int[] { cid1c, w }; } } } } } } else { last = ob; } } }