iTextSharp the library. me something strange happens with the fields are empty, to fill in some fields in the XML file. Newlines / carriage return generated (in empty fields)

emilio600

14-06-2015

     Hi all, with the library iTextSharp. me something strange happens. the fields are empty, to fill in some fields in the XML file. Newlines / carriage return is generated. I do not know why it happens

  --------------------------CODE EN C# --------------------------------------

  public static int PDF_Actualizar_URL_ENVIO_IDSESSION_PDF(int idOperador, string URL_ENVIO, string ID_SESSION, string rutaFormulario, string rutaFormularioDestino)

        {

            try

            {

                if (Path.GetExtension(rutaFormularioDestino).ToLower() == ".pdf")

                {

                    using (PdfReader pdfReader1 = new PdfReader(rutaFormulario, new System.Text.ASCIIEncoding().GetBytes(global_constants.passwordPDF())))

                    {

                        //si es un formulario con nodos XML y no esta firmado

                        if ((pdfReader1.AcroFields.Xfa.DatasetsNode != null) && (pdfReader1.AcroFields.GetSignatureNames().Count == 0))

                        {

                            //modifico los elmentos referencia e idinforme  del unico nodo dentro del nodo SUBFORMULARIOENCABEZADO

                            XmlDocument xml = new XmlDocument();

                            xml.LoadXml(pdfReader1.AcroFields.Xfa.DatasetsNode.FirstChild.OuterXml);

                            #region rellenar

                            //busco los campos y los modifico, en este caso solamente habrá 1

                            XmlNodeList ElementosFormularios = xml.GetElementsByTagName("SUB_ENCABEZADO");

                            if (ElementosFormularios.Count > 0)

                            {

                                //modifico los dos elementos del nodo

                                foreach (XmlElement nodo in ElementosFormularios)

                                {

                                    if (nodo["URL_ENVIO"].ChildNodes.Item(0) != null)

                                    {

                                        nodo["URL_ENVIO"].ChildNodes.Item(0).InnerText = URL_ENVIO;

                                    }

                                    if (nodo["IDSESSION"].ChildNodes.Item(0) != null)

                                    {

                                        nodo["IDSESSION"].ChildNodes.Item(0).InnerText = ID_SESSION;

                                    }

                                }

                            }

                            #endregion rellenar

                            //creo el fichero y cargo los datos del XML en el nuevo formulario

                            using (PdfStamper pdfStamper1 = new PdfStamper(pdfReader1, new FileStream(rutaFormularioDestino, FileMode.Create)))

                            {

                                //guardo el formulario en un Stream (en memoria)

                                using (MemoryStream xmlStream = new MemoryStream())

                                {

                                    //guardo el fichero XML en el Stream

                                    xml.Save(xmlStream);

                                    //pongo el puntero en el inicio del fichero

                                    xmlStream.Position = 0;

                                    //relleno el formulario con los datos del fichero xmlStream

                                    pdfStamper1.AcroFields.Xfa.FillXfaForm(xmlStream);

                                    pdfStamper1.FormFlattening = false;

                                    xmlStream.Close();

                                }

                                pdfStamper1.Close();

                            }

                        }

                        else //DOCUMENTO FIRMADO

                        {

                            //copio el fichero (firmado) en la ruta destino, este fichero no se puede modificar/rellenar para no invalidar la firma

                            File.Copy(rutaFormulario, rutaFormularioDestino, true);

                        }

                        pdfReader1.Close();

                        //todo OK

                        return 0;

                    }

                }

                else

                {

                    return -2;  //No se trata de un fichero pdf

                }

            }

            catch (Exception ex)

            {

                OperadorCecop.IN_ins_incidencia(idOperador, "Error en el método: PDF_Actualizar_URL_ENVIO_IDSESSION_PDF de la clase  'GestionFichero'", ex.Message);

                return -1;  //Error inexperado

            }

        }

Accepted Solutions (0)

Answers (0)