Exemplo n.º 1
0
        /// <summary>
        /// Appends the documents to primary document.
        /// </summary>
        /// <param name="primaryDocument">The primary document.</param>
        /// <param name="documentstoAppend">The documents to append.</param>
        /// <returns></returns>
        public byte[] AppendDocumentsToPrimaryDocument(byte[] primaryDocument, List <byte[]> documentstoAppend)
        {
            if (documentstoAppend == null)
            {
                throw new ArgumentNullException("documentstoAppend");
            }

            if (primaryDocument == null)
            {
                throw new ArgumentNullException("primaryDocument");
            }

            byte[] output = null;

            using (MemoryStream finalDocumentStream = new MemoryStream())
            {
                finalDocumentStream.Write(primaryDocument, 0, primaryDocument.Length);

                using (WordprocessingDocument finalDocument = WordprocessingDocument.Open(finalDocumentStream, true))
                {
                    SectionProperties finalDocSectionProperties = null;
                    this.UnprotectDocument(finalDocument);

                    SectionProperties tempSectionProperties = finalDocument.MainDocumentPart.Document.Descendants <SectionProperties>().LastOrDefault();

                    if (tempSectionProperties != null)
                    {
                        finalDocSectionProperties = tempSectionProperties.CloneNode(true) as SectionProperties;
                    }

                    this.RemoveContentControlsAndKeepContents(finalDocument.MainDocumentPart.Document);

                    foreach (byte[] documentToAppend in documentstoAppend)
                    {
                        AlternativeFormatImportPart subReportPart = finalDocument.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);
                        SectionProperties           secProperties = null;

                        using (MemoryStream docToAppendStream = new MemoryStream())
                        {
                            docToAppendStream.Write(documentToAppend, 0, documentToAppend.Length);

                            using (WordprocessingDocument docToAppend = WordprocessingDocument.Open(docToAppendStream, true))
                            {
                                this.UnprotectDocument(docToAppend);

                                tempSectionProperties = docToAppend.MainDocumentPart.Document.Descendants <SectionProperties>().LastOrDefault();

                                if (tempSectionProperties != null)
                                {
                                    secProperties = tempSectionProperties.CloneNode(true) as SectionProperties;
                                }

                                this.RemoveContentControlsAndKeepContents(docToAppend.MainDocumentPart.Document);
                                docToAppend.MainDocumentPart.Document.Save();
                            }

                            docToAppendStream.Position = 0;
                            subReportPart.FeedData(docToAppendStream);
                        }

                        if (documentstoAppend.ElementAtOrDefault(0).Equals(documentToAppend))
                        {
                            AssignSectionProperties(finalDocument.MainDocumentPart.Document, finalDocSectionProperties);
                        }

                        AltChunk altChunk = new AltChunk();
                        altChunk.Id = finalDocument.MainDocumentPart.GetIdOfPart(subReportPart);
                        finalDocument.MainDocumentPart.Document.AppendChild(altChunk);

                        if (!documentstoAppend.ElementAtOrDefault(documentstoAppend.Count - 1).Equals(documentToAppend))
                        {
                            AssignSectionProperties(finalDocument.MainDocumentPart.Document, secProperties);
                        }

                        finalDocument.MainDocumentPart.Document.Save();
                    }

                    finalDocument.MainDocumentPart.Document.Save();
                }

                finalDocumentStream.Position = 0;
                output = new byte[finalDocumentStream.Length];
                finalDocumentStream.Read(output, 0, output.Length);
                finalDocumentStream.Close();
            }

            return(output);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Start the parse processing and append the converted paragraphs into the Body of the document.
        /// </summary>
        public void ParseHtml(String html)
        {
            // This method exists because we may ensure the SectionProperties remains the last element of the body.
            // It's mandatory when dealing with page orientation

            var paragraphs = Parse(html);

            Body body = mainPart.Document.Body;
            SectionProperties initialSectionProperties = mainPart.Document.Body.GetLastChild <SectionProperties>();
            var firstOrientationChange  = Orientation.Keys.FirstOrDefault();
            var lastOrienationChange    = Orientation.Keys.LastOrDefault();
            var singleOrientationChange = Orientation.Keys.Count == 1;

            if (Orientation.Keys.Count == 0)
            {
                firstOrientationChange = -1;
                lastOrienationChange   = -1;
            }

            var singleSectionOrientation = initialSectionProperties.CloneNode(true);

            if (singleOrientationChange)
            {
                singleSectionOrientation = Orientation[lastOrienationChange] ? ChangePageOrientation(PageOrientationValues.Landscape)
                                                                                : ChangePageOrientation(PageOrientationValues.Portrait);
            }
            for (int i = 0; i < paragraphs.Count; i++)
            {
                var paragraph = paragraphs[i];
                if (i == firstOrientationChange - 1)
                {
                    paragraph.PrependChild(new ParagraphProperties(new KeepLines(), initialSectionProperties.CloneNode(true)));
                    body.Append(paragraph);
                    continue;
                }
                if (i == lastOrienationChange && !singleOrientationChange)
                {
                    //paragraph.AppendChild(new ParagraphProperties(new KeepLines(), initialSectionProperties.CloneNode(true)));
                    body.Append(paragraph);
                    continue;
                }

                if (i >= lastOrienationChange && singleOrientationChange)
                {
                    paragraph.PrependChild(new ParagraphProperties(new KeepLines(), singleSectionOrientation.CloneNode(true)));
                    body.Append(paragraph);
                    continue;
                }

                if (!Orientation.TryGetValue(i, out var landscape))
                {
                    body.Append(paragraph);
                    continue;
                }
                var pProps = paragraph.GetFirstChild <ParagraphProperties>();
                if (pProps is null)
                {
                    var orientation = landscape ? PageOrientationValues.Landscape : PageOrientationValues.Portrait;
                    paragraph.PrependChild(new ParagraphProperties(new KeepLines(), ChangePageOrientation(orientation)));
                    body.Append(paragraph);
                    continue;
                }
            }
            //Push the sectionProperties as the last element of the Body
            //(required by OpenXml schema to avoid the bad formatting of the document)
            //if (singleSectionOrientation != null) {
            //    var lastParagraph=body.GetLastChild<Paragraph>();
            //    lastParagraph.Remove();
            //    lastParagraph.PrependChild(new ParagraphProperties(singleSectionOrientation.CloneNode(true)));
            //    body.Append(lastParagraph);
            //}
        }