RELLENAR FORMULARIO PDF (C#) CON itextsharp.dll

http://itextpdf.com/


'relCAMPOSxVALOR = nombrecampo&valorcampo|nombrecampo2&valorcampo2|...
public void writePDF(string relCAMPOSxVALOR, string PDForigen, string PDFfin)
        {
            string pdfTemplate = @PDForigen;
            string newFile = @PDFfin;
            string fileREL = @relCAMPOSxVALOR;


            try
            {
                System.IO.StreamReader sw = new System.IO.StreamReader(fileREL, Encoding.Default);
                relCAMPOSxVALOR = sw.ReadToEnd();
                sw.Close();
                System.IO.File.Delete(fileREL);
            }
            catch
            {
            }


            PdfReader pdfReader = new PdfReader(pdfTemplate);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
                            newFile, FileMode.Create));


            AcroFields pdfFormFields = pdfStamper.AcroFields;


            try
            {


                string CAMPO;
                string VALOR;


                relCAMPOSxVALOR = relCAMPOSxVALOR.Replace("\r\n", "");


                CAMPO = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("&", 1));
                relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("&", 1) + 1);
                VALOR = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("|", 1));
                relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("|", 1) + 1);


                pdfFormFields.SetField(CAMPO, VALOR);


                if (relCAMPOSxVALOR.Length != 0)
                {
                    writePDF_sub(relCAMPOSxVALOR, PDForigen, PDFfin, pdfStamper.AcroFields);
                }
            }
            catch (InvalidCastException e)
            {
                throw (e);    // Rethrowing exception e
            }
                     
            // flatten the form to remove editting options, set it to false
            // to leave the form open to subsequent manual edits
            pdfStamper.FormFlattening = false;


            // close the pdf
            pdfStamper.Close();
        }


        private void writePDF_sub(string relCAMPOSxVALOR, string PDForigen, string PDFfin, AcroFields pdfFormFields)
        {
            string pdfTemplate = @PDForigen;
            string newFile = @PDFfin;


            try
            {
                string CAMPO;
                string VALOR;


                if (relCAMPOSxVALOR.IndexOf("&") > 0 && relCAMPOSxVALOR.IndexOf("|") > 0)
                {
                    CAMPO = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("&", 1));
                    relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("&", 1) + 1);
                    VALOR = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("|", 1));
                    relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("|", 1) + 1);


                    pdfFormFields.SetField(CAMPO, VALOR);


                    if (relCAMPOSxVALOR.Length != 0)
                    {
                        writePDF_sub(relCAMPOSxVALOR, PDForigen, PDFfin, pdfFormFields);
                    }
                }
            }
            catch (InvalidCastException e)
            {
                throw (e);    // Rethrowing exception e
            }
            
        }





No hay comentarios:

Publicar un comentario