Пример #1
0
        public void GenerarArchivoDocxDesdeTextosHtml(string[] textosHtml, System.IO.Stream archivo)
        {
            using (DocumentFormat.OpenXml.Packaging.WordprocessingDocument objPaquete = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Create(archivo, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
            {
                DocumentFormat.OpenXml.Packaging.MainDocumentPart objDocumentoPrincipal = objPaquete.MainDocumentPart;
                if (objDocumentoPrincipal == null)
                {
                    objDocumentoPrincipal = objPaquete.AddMainDocumentPart();
                    new DocumentFormat.OpenXml.Wordprocessing.Document(new DocumentFormat.OpenXml.Wordprocessing.Body()).Save(objDocumentoPrincipal);
                }

                NotesFor.HtmlToOpenXml.HtmlConverter       objConversorHtml = new NotesFor.HtmlToOpenXml.HtmlConverter(objDocumentoPrincipal);
                DocumentFormat.OpenXml.Wordprocessing.Body objCuerpo        = objDocumentoPrincipal.Document.Body;
                objConversorHtml.ImageProcessing = NotesFor.HtmlToOpenXml.ImageProcessing.ManualProvisioning;
                objConversorHtml.ProvisionImage += eventoHtmlDoc_ProveerImagenes;

                for (int intIndiceHtml = 0; intIndiceHtml < textosHtml.Length; intIndiceHtml++)
                {
                    if (intIndiceHtml > 0)
                    {
                        Paragraph PageBreakParagraph = new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Break()
                        {
                            Type = BreakValues.Page
                        }));
                        objCuerpo.Append(PageBreakParagraph);
                    }

                    var arrParrafos = objConversorHtml.Parse(textosHtml[intIndiceHtml]);
                    foreach (var objParrafo in arrParrafos)
                    {
                        objCuerpo.Append(objParrafo);
                    }
                }

                objDocumentoPrincipal.Document.Save();
            }
        }
Пример #2
0
        internal List<customSection> getHTML(string text, List<customSection> sections = null)
        {
            if (sections == null) sections = new List<customSection>();
            NotesFor.HtmlToOpenXml.HtmlConverter htmlconv = new NotesFor.HtmlToOpenXml.HtmlConverter(mainDoc);
            int start = text.ToString().IndexOf("{IMG");

            if (start > 0)
            {
                //There's an image.  Start by adding the text before the image

                sections.Add(new customSection(htmlconv.Parse(text.Substring(0, start))));
                //Add the image
                string part = text.Substring(start);
                int end = part.IndexOf('}');
                imageObj image = new imageObj();
                string imagestring = part.Substring(5, end - 5); //gives "#:title" inside the {}
                image.Url = "http://mbastorage.blob.core.windows.net/sfwart/" + new ARTService().ImageGet(m_ReportId, Convert.ToInt32(imagestring.Split(':')[0])).Path.Replace(" ", "%20");
                image.Link = image.Url;
                image.Filename = image.Url;
                sections.Add(new customSection(image));

                //Now recursively pass the remaining xml to this function again (in case there are additional images to process)
                sections = getHTML(text.Substring(start + end+ 1), sections);
            }
            else
            {
                sections.Add(new customSection(htmlconv.Parse(text)));
            }
            return sections;
        }