예제 #1
0
        /// <summary>Sets the XFA key from a byte array.</summary>
        /// <remarks>Sets the XFA key from a byte array. The old XFA is erased.</remarks>
        /// <param name="form">the data</param>
        /// <param name="acroForm">an AcroForm instance</param>
        /// <exception cref="System.IO.IOException">on IO error</exception>
        public static void SetXfaForm(iText.Forms.Xfa.XfaForm form, PdfAcroForm acroForm)
        {
            if (form == null || acroForm == null || acroForm.GetPdfDocument() == null)
            {
                throw new ArgumentException("XfaForm, PdfAcroForm and PdfAcroForm's document shall not be null");
            }
            PdfDocument document = acroForm.GetPdfDocument();
            PdfObject   xfa      = GetXfaObject(acroForm);

            if (xfa != null && 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(SerializeDocument(form.templateNode));
                    tStream.SetCompressionLevel(document.GetWriter().GetCompressionLevel());
                    ar.Set(t, tStream);
                    PdfStream dStream = new PdfStream(SerializeDocument(form.datasetsNode));
                    dStream.SetCompressionLevel(document.GetWriter().GetCompressionLevel());
                    ar.Set(d, dStream);
                    ar.SetModified();
                    ar.Flush();
                    acroForm.Put(PdfName.XFA, new PdfArray(ar));
                    acroForm.SetModified();
                    if (!acroForm.GetPdfObject().IsIndirect())
                    {
                        document.GetCatalog().SetModified();
                    }
                    return;
                }
            }
            //reader.killXref(af.get(PdfName.XFA));
            PdfStream stream = new PdfStream(SerializeDocument(form.domDocument));

            stream.SetCompressionLevel(document.GetWriter().GetCompressionLevel());
            stream.Flush();
            acroForm.Put(PdfName.XFA, stream);
            acroForm.SetModified();
            if (!acroForm.GetPdfObject().IsIndirect())
            {
                document.GetCatalog().SetModified();
            }
        }