Пример #1
0
        public Chap0702()
        {
            Console.WriteLine("Chapter 7 example 2: parsing the result of example 1");

            // step 1: creation of a document-object
            Document document = new Document();

            try
            {
                // step 2:
                // we create a writer that listens to the document
                // and directs a XML-stream to a file
                PdfWriter.GetInstance(document, new FileStream("Chap0702.pdf", FileMode.Create));
                HtmlWriter.GetInstance(document, new FileStream("Chap0702.htm", FileMode.Create));
                RtfWriter.GetInstance(document, new FileStream("Chap0702.rtf", FileMode.Create));
                // step 3: we create a parser

                ITextHandler h = new ITextHandler(document);

                // step 4: we parse the document
                h.Parse("Chap0701.xml");
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.StackTrace);
                Console.Error.WriteLine(e.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// Returns a PDF action result. This method renders the view to a string then
        /// use that string to generate a PDF file. The generated PDF file is then
        /// returned to the browser as binary content. The view associated with this
        /// action should render an XML compatible with iTextSharp xml format.
        /// </summary>
        /// <param name="model">The model to send to the view.</param>
        /// <returns>The resulted BinaryContentResult.</returns>
        protected ActionResult ViewPdf(object model)
        {
            // Create the iTextSharp document.
            Document doc = new Document();
            // Set the document to write to memory.
            MemoryStream memStream = new MemoryStream();
            PdfWriter    writer    = PdfWriter.GetInstance(doc, memStream);

            writer.CloseStream = false;
            doc.Open();

            // Render the view xml to a string, then parse that string into an XML dom.
            string      xmltext = this.RenderActionResultToString(this.View(model));
            XmlDocument xmldoc  = new XmlDocument();

            xmldoc.InnerXml = xmltext.Trim();

            // Parse the XML into the iTextSharp document.
            ITextHandler textHandler = new ITextHandler(doc);

            textHandler.Parse(xmldoc);

            // Close and get the resulted binary data.
            doc.Close();
            byte[] buf = new byte[memStream.Position];
            memStream.Position = 0;
            memStream.Read(buf, 0, buf.Length);

            // Send the binary data to the browser.
            return(new BinaryContentResult(buf, "application/pdf"));
        }
Пример #3
0
        public void XmlToRtf(string xmlDoc, string strFilename)
        {
            Document     document = new Document();
            MemoryStream ms       = new MemoryStream();
            Phrase       headerPhrase;
            Phrase       footerPhrase;

            // iTextSharp
            RtfWriter2 writer = RtfWriter2.GetInstance(document, ms);

            footerPhrase = new Phrase("", new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 8));
            RtfHeaderFooter footer = new RtfHeaderFooter(footerPhrase);

            footer.SetAlignment("center");
            writer.Footer = footer;

            AssemblyName an = this.GetType().Assembly.GetName();

            headerPhrase = new Phrase(
                "Use Case Maker " + an.Version.ToString(3),
                new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 8));
            RtfHeaderFooter header = new RtfHeaderFooter(headerPhrase);

            header.SetAlignment("right");
            writer.Header = header;

            StringReader  sr         = new StringReader(xmlDoc);
            XmlTextReader reader     = new XmlTextReader(sr);
            ITextHandler  xmlHandler = new ITextHandler(document);

            try
            {
                xmlHandler.Parse(reader);
            }
            catch (Exception e)
            {
                ms.Close();
                throw e;
            }
            finally
            {
                reader.Close();
                sr.Close();
            }

            //Write output file
            FileStream   fs = new FileStream(strFilename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write(ms.ToArray());
            bw.Close();
            fs.Close();
            ms.Close();
        }
Пример #4
0
        /// <summary>
        /// Genera el archivo PDF
        /// </summary>
        private void GenerarArchivoPDF(XDocument xml)
        {
            const string version      = "1.0";
            const string codificacion = "ISO-8859-1";
            var          xmlString    = String.Format("{0}\r{1}", "<?xml version='" + version + "' encoding='" + codificacion + "' ?>", xml);

            var output   = new MemoryStream();
            var document = new Document();

            //document.SetPageSize(PageSize.A4.Rotate());
            //document.Open();


            PdfWriter.GetInstance(document, output);
            var xmlHandler = new ITextHandler(document);

            var documentoXML = new XmlDocument();

            documentoXML.LoadXml(xmlString);

            xmlHandler.Parse(documentoXML);

            if (string.IsNullOrWhiteSpace(NombreArchivo))
            {
                NombreArchivo = "Reporte";
            }

            var file = new SaveFileDialog {
                FileName = NombreArchivo, Filter = @"Archivos PDF|*.pdf", Title = @"Guardar Archivo PDF"
            };
            var result = file.ShowDialog();

            if (result == DialogResult.OK)
            {
                if (file.FileName != "")
                {
                    if (File.Exists(file.FileName))
                    {
                        try
                        {
                            var archivoPDF = new FileStream(file.FileName, FileMode.Open);
                            archivoPDF.Close();
                        }
                        catch (IOException)
                        {
                            throw new ExcepcionServicio(Properties.Resources.ExportarExcel_ArchivoAbierto);
                        }
                    }
                    File.WriteAllBytes(file.FileName, output.ToArray());
                    RutaFinal = file.FileName;
                }
            }
        }
Пример #5
0
        //
        public void WriteXMLToPDF(string FileName, string TextToWrite, Rectangle pageSize, bool landscape)
        {
            string filePath = HttpContext.Current.Server.MapPath("PDF");

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath += "/" + FileName + ".pdf";
            Document document = new Document();

            if (landscape)
            {
                document.SetPageSize(pageSize.Rotate());
            }
            else
            {
                document.SetPageSize(pageSize);
            }
            PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
            document.Open();
            //EtrekReports.text.html.simpleparser.HTMLWorker hw = new EtrekReports.text.html.simpleparser.HTMLWorker(document);
            //hw.Parse(new StringReader(TextToWrite));
            //ITextHandler xmlHandler = new ITextHandler(document);
            //XmlDocument doc = new XmlDocument();
            //doc.LoadXml(TextToWrite); // error thrown here
            //XDocument doc;
            // using (StringReader s = new StringReader(TextToWrite))
            //{
            //    doc = XDocument.Load(s);
            //}
            ITextHandler xmlHandler = new ITextHandler(document);

            // step 4: we parse the document
            xmlHandler.Parse(TextToWrite);



            document.Close();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filePath);
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.WriteFile(filePath);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Clear();
        }
Пример #6
0
        /// <summary>
        /// Genera el archivo PDF
        /// </summary>
        private MemoryStream GenerarArchivoPDF(XDocument xml)
        {
            const string version      = "1.0";
            const string codificacion = "ISO-8859-1";
            var          xmlString    = String.Format("{0}\r{1}",
                                                      "<?xml version='" + version + "' encoding='" + codificacion + "' ?>", xml);

            var output   = new MemoryStream();
            var document = new Document();

            PdfWriter.GetInstance(document, output);
            var xmlHandler = new ITextHandler(document);

            var documentoXML = new XmlDocument();

            documentoXML.LoadXml(xmlString);

            xmlHandler.Parse(documentoXML);

            return(output);
        }
Пример #7
0
        public void XmlToPdf(string xmlDoc, string strFilename)
        {
            Document     document = new Document();
            MemoryStream ms       = new MemoryStream();

            // iTextSharp
            PdfWriter    writer     = PdfWriter.GetInstance(document, ms);
            MyPageEvents pageEvents = new MyPageEvents();

            writer.PageEvent = pageEvents;

            StringReader  sr         = new StringReader(xmlDoc);
            XmlTextReader reader     = new XmlTextReader(sr);
            ITextHandler  xmlHandler = new ITextHandler(document);

            try
            {
                xmlHandler.Parse(reader);
            }
            catch (Exception e)
            {
                ms.Close();
                throw e;
            }
            finally
            {
                reader.Close();
                sr.Close();
            }

            //Write output file
            FileStream   fs = new FileStream(strFilename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write(ms.ToArray());
            bw.Close();
            fs.Close();
            ms.Close();
        }
Пример #8
0
        /// <summary>
        /// Genera el XML que se guarda en la tabla
        /// </summary>
        /// <param name="checkListCorral">contenedor donde se encuentra la información del Check List</param>
        /// <param name="xml">Layout del XML</param>
        /// <returns></returns>
        internal static void ArmarXML(CheckListCorralInfo checkListCorral, XDocument xml)
        {
            ArmarNodoTablaCheckList(xml, checkListCorral);
            ArmarNodoTablaGanado(xml, checkListCorral.ListaConceptos);
            ArmarNodoTablaCorral(xml, checkListCorral.ListaAcciones);

            var version      = "1.0";
            var codificacion = "ISO-8859-1";
            var xmlString    = String.Format("{0}\r{1}", "<?xml version='" + version + "' encoding='" + codificacion + "' ?>", xml);

            var output   = new MemoryStream();
            var document = new Document();

            PdfWriter.GetInstance(document, output);
            var xmlHandler = new ITextHandler(document);

            var documentoXML = new XmlDocument();

            documentoXML.LoadXml(xmlString);

            xmlHandler.Parse(documentoXML);
            checkListCorral.PDF = output.ToArray();
        }
Пример #9
0
 /// <summary>
 /// Parses a given XmlTextReader.
 /// </summary>
 /// <param name="document"></param>
 /// <param name="reader"></param>
 /// <param name="tagmap"></param>
 public virtual void Go(IDocListener document, XmlTextReader reader, Hashtable tagmap)
 {
     parser = new ITextmyHandler(document, tagmap);
     parser.Parse(reader);
 }
Пример #10
0
 /// <summary>
 /// Parses a given file.
 /// </summary>
 /// <param name="document"></param>
 /// <param name="file"></param>
 /// <param name="tagmap"></param>
 public virtual void Go(IDocListener document, String file, Hashtable tagmap)
 {
     parser = new ITextmyHandler(document, tagmap);
     parser.Parse(file);
 }
Пример #11
0
 /// <summary>
 /// Parses a given file.
 /// </summary>
 /// <param name="document"></param>
 /// <param name="file"></param>
 /// <param name="tagmap"></param>
 public virtual void Go(IDocListener document, String file, String tagmap)
 {
     parser = new ITextmyHandler(document, new TagMap(tagmap));
     parser.Parse(file);
 }
Пример #12
0
 /// <summary>
 /// Parses a given XmlTextReader.
 /// </summary>
 /// <param name="document"></param>
 /// <param name="reader"></param>
 /// <param name="tagmap"></param>
 public virtual void Go(IDocListener document, XmlTextReader reader, String tagmap)
 {
     parser = new ITextmyHandler(document, new TagMap(tagmap));
     parser.Parse(reader);
 }
Пример #13
0
 /// <summary>
 /// Parses a given file.
 /// </summary>
 /// <param name="document"></param>
 /// <param name="file"></param>
 /// <param name="tagmap"></param>
 public virtual void Go(IDocListener document, XmlDocument xDoc, XmlDocument xTagmap)
 {
     parser = new ITextmyHandler(document, new TagMap(xTagmap));
     parser.Parse(xDoc);
 }
Пример #14
0
 /// <summary>
 /// Parses a given XmlTextReader.
 /// </summary>
 /// <param name="document"></param>
 /// <param name="reader"></param>
 public virtual void Go(IDocListener document, XmlTextReader reader)
 {
     parser = new ITextHandler(document);
     parser.Parse(reader);
 }
Пример #15
0
 /// <summary>
 /// Parses a given file.
 /// </summary>
 /// <param name="document"></param>
 /// <param name="file"></param>
 public virtual void Go(IDocListener document, String file)
 {
     parser = new ITextHandler(document);
     parser.Parse(file);
 }
Пример #16
0
 /// <summary>
 /// Parses a given file.
 /// </summary>
 /// <param name="document"></param>
 /// <param name="file"></param>
 public virtual void Go(IDocListener document, XmlDocument xDoc)
 {
     parser = new ITextHandler(document);
     parser.Parse(xDoc);
 }