コード例 #1
0
        private static void FillInEmptyFootnotesEndnotes(WordprocessingDocument wDoc)
        {
            XElement emptyFootnote = XElement.Parse(
                @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  <w:pPr>
    <w:pStyle w:val='FootnoteText'/>
  </w:pPr>
  <w:r>
    <w:rPr>
      <w:rStyle w:val='FootnoteReference'/>
    </w:rPr>
    <w:footnoteRef/>
  </w:r>
</w:p>");

            XElement emptyEndnote = XElement.Parse(
                @"<w:p xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>
  <w:pPr>
    <w:pStyle w:val='EndnoteText'/>
  </w:pPr>
  <w:r>
    <w:rPr>
      <w:rStyle w:val='EndnoteReference'/>
    </w:rPr>
    <w:endnoteRef/>
  </w:r>
</w:p>");

            FootnotesPart footnotePart = wDoc.MainDocumentPart.FootnotesPart;

            if (footnotePart != null)
            {
                XElement fnRoot = footnotePart.GetXDocument().Root ?? throw new ArgumentException();
                foreach (XElement fn in fnRoot.Elements(W.footnote))
                {
                    if (!fn.HasElements)
                    {
                        fn.Add(emptyFootnote);
                    }
                }

                footnotePart.PutXDocument();
            }

            EndnotesPart endnotePart = wDoc.MainDocumentPart.EndnotesPart;

            if (endnotePart != null)
            {
                XElement fnRoot = endnotePart.GetXDocument().Root ?? throw new ArgumentException();
                foreach (XElement fn in fnRoot.Elements(W.endnote))
                {
                    if (!fn.HasElements)
                    {
                        fn.Add(emptyEndnote);
                    }
                }

                endnotePart.PutXDocument();
            }
        }
コード例 #2
0
        /// <summary>
        /// Delete endnotes from the document
        /// </summary>
        /// <param name="docName"></param>
        /// <returns></returns>
        public static bool RemoveEndnotes(string docName)
        {
            fWorked = false;

            using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(docName, true))
            {
                if (wdDoc.MainDocumentPart.GetPartsCountOfType <EndnotesPart>() > 0)
                {
                    MainDocumentPart mainPart = wdDoc.MainDocumentPart;

                    var enr = mainPart.Document.Descendants <EndnoteReference>().ToArray();
                    foreach (var e in enr)
                    {
                        e.Remove();
                    }

                    EndnotesPart ep = mainPart.EndnotesPart;
                    ep.Endnotes = WordOpenXml.CreateDefaultEndnotes();
                    mainPart.Document.Save();
                    fWorked = true;
                }
            }

            return(fWorked);
        }
コード例 #3
0
        private static void ChangeFootnoteEndnoteReferencesToUniqueRange(
            WordprocessingDocument wDoc,
            int startingIdForFootnotesEndnotes)
        {
            MainDocumentPart mainDocPart   = wDoc.MainDocumentPart;
            FootnotesPart    footnotesPart = wDoc.MainDocumentPart.FootnotesPart;
            EndnotesPart     endnotesPart  = wDoc.MainDocumentPart.EndnotesPart;

            XElement document =
                mainDocPart.GetXDocument().Root ?? throw new OpenXmlPowerToolsException("Invalid document.");

            XElement footnotes = footnotesPart?.GetXDocument().Root;
            XElement endnotes  = endnotesPart?.GetXDocument().Root;

            IEnumerable <XElement> references = document
                                                .Descendants()
                                                .Where(d => d.Name == W.footnoteReference || d.Name == W.endnoteReference);

            foreach (XElement r in references)
            {
                var    oldId = (string)r.Attribute(W.id);
                string newId = startingIdForFootnotesEndnotes.ToString();
                startingIdForFootnotesEndnotes++;
                r.SetAttributeValue(W.id, newId);
                if (r.Name == W.footnoteReference)
                {
                    XElement fn = footnotes?
                                  .Elements()
                                  .FirstOrDefault(e => (string)e.Attribute(W.id) == oldId);

                    if (fn == null)
                    {
                        throw new OpenXmlPowerToolsException("Invalid document");
                    }

                    fn.SetAttributeValue(W.id, newId);
                }
                else
                {
                    XElement en = endnotes?
                                  .Elements()
                                  .FirstOrDefault(e => (string)e.Attribute(W.id) == oldId);

                    if (en == null)
                    {
                        throw new OpenXmlPowerToolsException("Invalid document");
                    }

                    en.SetAttributeValue(W.id, newId);
                }
            }

            mainDocPart.PutXDocument();
            footnotesPart?.PutXDocument();
            endnotesPart?.PutXDocument();
        }
コード例 #4
0
        private static void RemoveExistingPowerToolsMarkup(WordprocessingDocument wDoc)
        {
            wDoc.MainDocumentPart
            .GetXDocument()
            .Root?
            .Descendants()
            .Attributes()
            .Where(a => a.Name.Namespace == PtOpenXml.pt)
            .Where(a => a.Name != PtOpenXml.Unid)
            .Remove();

            wDoc.MainDocumentPart.PutXDocument();

            FootnotesPart fnPart = wDoc.MainDocumentPart.FootnotesPart;

            if (fnPart != null)
            {
                XDocument fnXDoc = fnPart.GetXDocument();
                fnXDoc
                .Root?
                .Descendants()
                .Attributes()
                .Where(a => a.Name.Namespace == PtOpenXml.pt)
                .Where(a => a.Name != PtOpenXml.Unid)
                .Remove();

                fnPart.PutXDocument();
            }

            EndnotesPart enPart = wDoc.MainDocumentPart.EndnotesPart;

            if (enPart != null)
            {
                XDocument enXDoc = enPart.GetXDocument();
                enXDoc
                .Root?
                .Descendants()
                .Attributes()
                .Where(a => a.Name.Namespace == PtOpenXml.pt)
                .Where(a => a.Name != PtOpenXml.Unid)
                .Remove();

                enPart.PutXDocument();
            }
        }
コード例 #5
0
        public static void GenerateEndnotesPart1Content(EndnotesPart endnotesPart1)
        {
            var endnotes1 = new Endnotes
            {
                MCAttributes = new MarkupCompatibilityAttributes {
                    Ignorable = "w14 w15 w16se w16cid wp14"
                }
            };

            endnotes1.AddNamespaceDeclaration("wpc",
                                              "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
            endnotes1.AddNamespaceDeclaration("cx", "http://schemas.microsoft.com/office/drawing/2014/chartex");
            endnotes1.AddNamespaceDeclaration("cx1", "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex");
            endnotes1.AddNamespaceDeclaration("cx2", "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex");
            endnotes1.AddNamespaceDeclaration("cx3", "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex");
            endnotes1.AddNamespaceDeclaration("cx4", "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex");
            endnotes1.AddNamespaceDeclaration("cx5", "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex");
            endnotes1.AddNamespaceDeclaration("cx6", "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex");
            endnotes1.AddNamespaceDeclaration("cx7", "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex");
            endnotes1.AddNamespaceDeclaration("cx8", "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex");
            endnotes1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            endnotes1.AddNamespaceDeclaration("aink", "http://schemas.microsoft.com/office/drawing/2016/ink");
            endnotes1.AddNamespaceDeclaration("am3d", "http://schemas.microsoft.com/office/drawing/2017/model3d");
            endnotes1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            endnotes1.AddNamespaceDeclaration("r",
                                              "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            endnotes1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            endnotes1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            endnotes1.AddNamespaceDeclaration("wp14",
                                              "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
            endnotes1.AddNamespaceDeclaration("wp",
                                              "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            endnotes1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            endnotes1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            endnotes1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
            endnotes1.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml");
            endnotes1.AddNamespaceDeclaration("w16cid", "http://schemas.microsoft.com/office/word/2016/wordml/cid");
            endnotes1.AddNamespaceDeclaration("w16se", "http://schemas.microsoft.com/office/word/2015/wordml/symex");
            endnotes1.AddNamespaceDeclaration("wpg",
                                              "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
            endnotes1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
            endnotes1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
            endnotes1.AddNamespaceDeclaration("wps",
                                              "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");

            var endnote1 = new Endnote {
                Type = FootnoteEndnoteValues.Separator, Id = -1
            };

            var paragraph271 = new Paragraph
            {
                RsidParagraphAddition  = "003C529E",
                RsidRunAdditionDefault = "003C529E",
                ParagraphId            = "45330DB0",
                TextId = "77777777"
            };

            var paragraphProperties174 = new ParagraphProperties();
            var spacingBetweenLines171 =
                new SpacingBetweenLines {
                After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto
            };

            paragraphProperties174.Append(spacingBetweenLines171);

            var run394         = new Run();
            var separatorMark1 = new SeparatorMark();

            run394.Append(separatorMark1);

            paragraph271.Append(paragraphProperties174);
            paragraph271.Append(run394);

            endnote1.Append(paragraph271);

            var endnote2 = new Endnote {
                Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 0
            };

            var paragraph272 = new Paragraph
            {
                RsidParagraphAddition  = "003C529E",
                RsidRunAdditionDefault = "003C529E",
                ParagraphId            = "02CE0DA3",
                TextId = "77777777"
            };

            var paragraphProperties175 = new ParagraphProperties();
            var spacingBetweenLines172 =
                new SpacingBetweenLines {
                After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto
            };

            paragraphProperties175.Append(spacingBetweenLines172);

            var run395 = new Run();
            var continuationSeparatorMark1 = new ContinuationSeparatorMark();

            run395.Append(continuationSeparatorMark1);

            paragraph272.Append(paragraphProperties175);
            paragraph272.Append(run395);

            endnote2.Append(paragraph272);

            endnotes1.Append(endnote1);
            endnotes1.Append(endnote2);

            endnotesPart1.Endnotes = endnotes1;
        }
コード例 #6
0
        private void CreateParts(WordprocessingDocument document)
        {
            DocxBase.CurrentTitle = DocxServiceProvinceRes.Title;

            ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart <ExtendedFilePropertiesPart>("rId3");

            DocxBase.GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);

            MainDocumentPart mainDocumentPart1 = document.AddMainDocumentPart();

            GenerateMainDocumentPart1Content(mainDocumentPart1);

            FontTablePart fontTablePart1 = mainDocumentPart1.AddNewPart <FontTablePart>("rId13");

            DocxBase.GenerateFontTablePart1Content(fontTablePart1);

            StyleDefinitionsPart styleDefinitionsPart1 = mainDocumentPart1.AddNewPart <StyleDefinitionsPart>("rId3");

            DocxBase.GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1);

            EndnotesPart endnotesPart1 = mainDocumentPart1.AddNewPart <EndnotesPart>("rId7");

            DocxBase.GenerateEndnotesPart1Content(endnotesPart1);

            FooterPart footerPart1 = mainDocumentPart1.AddNewPart <FooterPart>("rId12");

            DocxBase.GenerateFooterPart1Content(footerPart1);

            NumberingDefinitionsPart numberingDefinitionsPart1 = mainDocumentPart1.AddNewPart <NumberingDefinitionsPart>("rId2");

            DocxBase.GenerateNumberingDefinitionsPart1Content(numberingDefinitionsPart1);

            CustomXmlPart customXmlPart1 = mainDocumentPart1.AddNewPart <CustomXmlPart>("application/xml", "rId1");

            DocxBase.GenerateCustomXmlPart1Content(customXmlPart1);

            CustomXmlPropertiesPart customXmlPropertiesPart1 = customXmlPart1.AddNewPart <CustomXmlPropertiesPart>("rId1");

            DocxBase.GenerateCustomXmlPropertiesPart1Content(customXmlPropertiesPart1);

            FootnotesPart footnotesPart1 = mainDocumentPart1.AddNewPart <FootnotesPart>("rId6");

            DocxBase.GenerateFootnotesPart1Content(footnotesPart1);

            HeaderPart headerPart1 = mainDocumentPart1.AddNewPart <HeaderPart>("rId11");

            DocxBase.GenerateHeaderPart1Content(headerPart1);

            WebSettingsPart webSettingsPart1 = mainDocumentPart1.AddNewPart <WebSettingsPart>("rId5");

            DocxBase.GenerateWebSettingsPart1Content(webSettingsPart1);

            //ChartPart chartPart1 = mainDocumentPart1.AddNewPart<ChartPart>("rId10");
            //DocxBase.GenerateChartPart1Content(chartPart1);

            //EmbeddedPackagePart embeddedPackagePart1 = chartPart1.AddNewPart<EmbeddedPackagePart>("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "rId3");
            //DocxBase.GenerateEmbeddedPackagePart1Content(embeddedPackagePart1);

            //ChartColorStylePart chartColorStylePart1 = chartPart1.AddNewPart<ChartColorStylePart>("rId2");
            //DocxBase.GenerateChartColorStylePart1Content(chartColorStylePart1);

            //ChartStylePart chartStylePart1 = chartPart1.AddNewPart<ChartStylePart>("rId1");
            //DocxBase.GenerateChartStylePart1Content(chartStylePart1);

            DocumentSettingsPart documentSettingsPart1 = mainDocumentPart1.AddNewPart <DocumentSettingsPart>("rId4");

            DocxBase.GenerateDocumentSettingsPart1Content(documentSettingsPart1);

            //ChartPart chartPart2 = mainDocumentPart1.AddNewPart<ChartPart>("rId9");
            //DocxBase.GenerateChartPart2Content(chartPart2);

            //EmbeddedPackagePart embeddedPackagePart2 = chartPart2.AddNewPart<EmbeddedPackagePart>("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "rId3");
            //DocxBase.GenerateEmbeddedPackagePart2Content(embeddedPackagePart2);

            //ChartColorStylePart chartColorStylePart2 = chartPart2.AddNewPart<ChartColorStylePart>("rId2");
            //DocxBase.GenerateChartColorStylePart2Content(chartColorStylePart2);

            //ChartStylePart chartStylePart2 = chartPart2.AddNewPart<ChartStylePart>("rId1");
            //DocxBase.GenerateChartStylePart2Content(chartStylePart2);

            ThemePart themePart1 = mainDocumentPart1.AddNewPart <ThemePart>("rId14");

            DocxBase.GenerateThemePart1Content(themePart1);

            foreach (UsedHyperlink usedHyperlink in DocxBase.UsedHyperlinkList)
            {
                mainDocumentPart1.AddHyperlinkRelationship(new System.Uri(usedHyperlink.URL, System.UriKind.Absolute), true, usedHyperlink.Id.ToString());
            }

            DocxBase.SetPackageProperties(document);
        }
コード例 #7
0
        /// <summary>
        /// Add a note to the Endnotes part and ensure it exists.
        /// </summary>
        /// <param name="description">The description of an acronym, abbreviation, some book references, ...</param>
        /// <returns>Returns the id of the endnote reference.</returns>
        protected int AddEndnoteReference(string description)
        {
            EndnotesPart fpart = mainPart.EndnotesPart;

            if (fpart == null)
            {
                fpart = mainPart.AddNewPart <EndnotesPart>();
            }

            if (fpart.Endnotes == null)
            {
                // Insert a new Footnotes reference
                new Endnotes(
                    new Endnote(
                        new Paragraph(
                            new ParagraphProperties {
                    SpacingBetweenLines = new SpacingBetweenLines()
                    {
                        After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto
                    }
                },
                            new Run(
                                new SeparatorMark())
                            )
                        )
                {
                    Type = FootnoteEndnoteValues.ContinuationSeparator, Id = -1
                },
                    new Endnote(
                        new Paragraph(
                            new ParagraphProperties {
                    SpacingBetweenLines = new SpacingBetweenLines()
                    {
                        After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto
                    }
                },
                            new Run(
                                new ContinuationSeparatorMark())
                            )
                        )
                {
                    Id = 0
                }).Save(fpart);
                endnotesRef = 1;
            }
            else
            {
                // The footnotesRef Id is a required field and should be unique. You can assign yourself some hard-coded
                // value but that's absolutely not safe. We will loop through the existing Footnote
                // to retrieve the highest Id.
                foreach (var p in fpart.Endnotes.Elements <Endnote>())
                {
                    if (p.Id.HasValue && p.Id > footnotesRef)
                    {
                        endnotesRef = (int)p.Id.Value;
                    }
                }
                endnotesRef++;
            }

            Run markerRun;

            fpart.Endnotes.Append(
                new Endnote(
                    new Paragraph(
                        new ParagraphProperties {
                ParagraphStyleId = new ParagraphStyleId()
                {
                    Val = htmlStyles.GetStyle("endnote text", StyleValues.Paragraph)
                }
            },
                        markerRun = new Run(
                            new RunProperties {
                RunStyle = new RunStyle()
                {
                    Val = htmlStyles.GetStyle("endnote reference", StyleValues.Character)
                }
            },
                            new FootnoteReferenceMark()),
                        new Run(
                            // Word insert automatically a space before the definition to separate the reference number
                            // with its description
                            new Text(" " + description)
            {
                Space = SpaceProcessingModeValues.Preserve
            })
                        )
                    )
            {
                Id = endnotesRef
            });

            if (!htmlStyles.DoesStyleExists("endnote reference"))
            {
                // Force the superscript style because if the footnote text style does not exists,
                // the rendering will be awful.
                markerRun.InsertInProperties(prop =>
                                             prop.VerticalTextAlignment = new VerticalTextAlignment()
                {
                    Val = VerticalPositionValues.Superscript
                });
            }

            fpart.Endnotes.Save();

            return(endnotesRef);
        }
コード例 #8
0
        private void SetupStyles()
        {
            WordprocessingCommentsPart commentsPart        = AddTemplatePart <WordprocessingCommentsPart>(this.document, CommentsStyleResource);
            EndnotesPart             endNotesPart          = AddTemplatePart <EndnotesPart>(this.document, EndNotesStyleResource);
            FontTablePart            fontTablePart         = AddTemplatePart <FontTablePart>(this.document, FontsTableStyleResource);
            FootnotesPart            footnotesPart         = AddTemplatePart <FootnotesPart>(this.document, FootNotesStyleResource);
            HeaderPart               headerPart            = AddTemplatePart <HeaderPart>(this.document, HeaderStyleResource);
            DocumentSettingsPart     settingsPart          = AddTemplatePart <DocumentSettingsPart>(this.document, SettingsStyleResource);
            StyleDefinitionsPart     styles                = AddTemplatePart <StyleDefinitionsPart>(this.document, StylesStyleResource);
            StylesWithEffectsPart    stylesWithEffectsPart = AddTemplatePart <StylesWithEffectsPart>(this.document, StylesWithEffectsStyleResource);
            WebSettingsPart          webSettingsPart       = AddTemplatePart <WebSettingsPart>(this.document, WebSettingsStyleResource);
            ThemePart                themePart             = AddTemplatePart <ThemePart>(this.document, ThemeStyleResource);
            NumberingDefinitionsPart numberingPart         = AddTemplatePart <NumberingDefinitionsPart>(this.document, NumberingStyleResource);

            // Initialize the comments manager with the comments part
            this.commentManager = new CommentManager(commentsPart.Comments);

            // Initialize the footer
            string     footerTitle      = this.implementationGuide.GetDisplayName();
            DateTime   footerDate       = this.implementationGuide.PublishDate != null ? this.implementationGuide.PublishDate.Value : DateTime.Now;
            string     footerDateString = footerDate.ToString("m");
            FooterPart newFooterPart    = this.document.MainDocumentPart.AddNewPart <FooterPart>();
            Footer     newFooter        = new Footer();
            Paragraph  pFooter          = new Paragraph(
                new ParagraphProperties(
                    new ParagraphStyleId()
            {
                Val = "Footer"
            }));

            pFooter.Append(
                new Run(
                    new Text(footerTitle)),
                new Run(
                    new TabChar(),
                    new Text(footerDateString)
            {
                Space = SpaceProcessingModeValues.Preserve
            }),
                new Run(
                    new TabChar(),
                    new Text("Page ")
            {
                Space = SpaceProcessingModeValues.Preserve
            }),
                new Run(
                    new FieldChar()
            {
                FieldCharType = FieldCharValues.Begin
            }),
                new Run(
                    new FieldCode(" PAGE ")
            {
                Space = SpaceProcessingModeValues.Preserve
            }),
                new Run(
                    new FieldChar()
            {
                FieldCharType = FieldCharValues.Separate
            }),
                new Run(
                    new RunProperties(
                        new NoProof()),
                    new Text("54")),
                new Run(
                    new FieldChar()
            {
                FieldCharType = FieldCharValues.End
            }));
            newFooter.Append(pFooter);
            newFooterPart.Footer = newFooter;

            // Add numbering for templates
            foreach (Template cTemplate in this.templates)
            {
                NumberingInstance ni = new NumberingInstance(
                    new AbstractNumId()
                {
                    Val = 3
                })
                {
                    NumberID = GenerationConstants.BASE_TEMPLATE_INDEX + (int)cTemplate.Id
                };

                for (int i = 0; i < 9; i++)
                {
                    ni.Append(new LevelOverride(
                                  new StartOverrideNumberingValue()
                    {
                        Val = 1
                    })
                    {
                        LevelIndex = i
                    });
                }

                numberingPart.Numbering.Append(ni);
            }
        }
コード例 #9
0
        private static void ProcessWordDocument(
            string outputDocumentFullName,
            string sourceLanguage,
            string targetLanguage,
            bool ignoreHidden = false)
        {
            using (WordprocessingDocument doc = WordprocessingDocument.Open(outputDocumentFullName, true))
            {
                OpenXmlPowerTools.SimplifyMarkupSettings settings = new OpenXmlPowerTools.SimplifyMarkupSettings
                {
                    AcceptRevisions       = true,
                    NormalizeXml          = false, //setting this to false reduces translation quality, but if true some documents have XML format errors when opening
                    RemoveBookmarks       = true,
                    RemoveComments        = true,
                    RemoveContentControls = true,
                    RemoveEndAndFootNotes = false,
                    RemoveFieldCodes      = true,
                    RemoveGoBackBookmark  = true,
                    //RemoveHyperlinks = false,
                    RemoveLastRenderedPageBreak       = true,
                    RemoveMarkupForDocumentComparison = true,
                    RemovePermissions     = false,
                    RemoveProof           = true,
                    RemoveRsidInfo        = true,
                    RemoveSmartTags       = false,   //setting this to false reduces translation quality, but if true some documents have XML format errors when opening
                    RemoveSoftHyphens     = true,
                    RemoveWebHidden       = true,
                    ReplaceTabsWithSpaces = false
                };
                OpenXmlPowerTools.MarkupSimplifier.SimplifyMarkup(doc, settings);
            }

            List <DocumentFormat.OpenXml.Wordprocessing.Text> texts = new List <DocumentFormat.OpenXml.Wordprocessing.Text>();

            using (WordprocessingDocument doc = WordprocessingDocument.Open(outputDocumentFullName, true))
            {
                var body = doc.MainDocumentPart.Document.Body;
                texts.AddRange(body.Descendants <DocumentFormat.OpenXml.Wordprocessing.Text>()
                               .Where(text => !String.IsNullOrEmpty(text.Text) && text.Text.Length > 0));

#if DEBUG
                //--------DEBUG PROCESSING -------------
                if (false)
                {
                    using (StreamWriter debugoutput = new StreamWriter(outputDocumentFullName + ".debug.txt"))
                    {
                        foreach (var text in texts)
                        {
                            debugoutput.WriteLine(text.Text);
                        }
                        debugoutput.Flush();
                        debugoutput.Close();
                    }
                }
                //--------END DEBUG --------------------
#endif

                var headers = doc.MainDocumentPart.HeaderParts.Select(p => p.Header);
                foreach (var header in headers)
                {
                    texts.AddRange(header.Descendants <DocumentFormat.OpenXml.Wordprocessing.Text>().Where(text => !string.IsNullOrEmpty(text.Text) && text.Text.Length > 0));
                }

                var footers = doc.MainDocumentPart.FooterParts.Select(p => p.Footer);
                foreach (var footer in footers)
                {
                    texts.AddRange(footer.Descendants <DocumentFormat.OpenXml.Wordprocessing.Text>().Where(text => !string.IsNullOrEmpty(text.Text) && text.Text.Length > 0));
                }

                FootnotesPart footnotesPart = doc.MainDocumentPart.FootnotesPart;
                if (footnotesPart != null)
                {
                    var footnotes = footnotesPart.Footnotes.Elements <Footnote>();
                    foreach (var footnote in footnotes)
                    {
                        texts.AddRange(footnote.Descendants <DocumentFormat.OpenXml.Wordprocessing.Text>().Where(text => !string.IsNullOrEmpty(text.Text) && text.Text.Length > 0));
                    }
                }

                EndnotesPart endnotesPart = doc.MainDocumentPart.EndnotesPart;
                if (endnotesPart != null)
                {
                    var endnotes = endnotesPart.Endnotes.Elements <DocumentFormat.OpenXml.Wordprocessing.Endnote>();
                    foreach (var endnote in endnotes)
                    {
                        texts.AddRange(endnote.Descendants <DocumentFormat.OpenXml.Wordprocessing.Text>().Where(text => !string.IsNullOrEmpty(text.Text) && text.Text.Length > 0));
                    }
                }



                if (ignoreHidden)
                {
                    texts.RemoveAll(t => t.Parent.Descendants <Vanish>().Any());
                }

                var exceptions = new ConcurrentQueue <Exception>();

                // Extract Text for Translation
                var batch = texts.Select(text => text.Text);

                // Do Translation
                var batches = SplitList(batch, TranslationServiceFacade.Maxelements, TranslationServiceFacade.Maxrequestsize);
                Parallel.For(
                    0,
                    batches.Count(),
                    new ParallelOptions {
                    MaxDegreeOfParallelism = 1
                },
                    l =>
                {
                    try
                    {
                        var translationOutput = TranslationServiceFacade.TranslateArray(
                            batches[l].ToArray(),
                            sourceLanguage,
                            targetLanguage);
                        int batchStartIndexInDocument = 0;
                        for (int i = 0; i < l; i++)
                        {
                            batchStartIndexInDocument = batchStartIndexInDocument + batches[i].Count();
                        }

                        // Apply translated batch to document
                        for (int j = 0; j < translationOutput.Length; j++)
                        {
                            int indexInDocument = j + batchStartIndexInDocument + 1;
                            var newValue        = translationOutput[j];
                            texts.Take(indexInDocument).Last().Text = newValue;
                        }
                    }
                    catch (Exception ex)
                    {
                        exceptions.Enqueue(ex);
                    }
                });

                // Throw the exceptions here after the loop completes.
                if (exceptions.Count > 0)
                {
                    throw new AggregateException(exceptions);
                }
                doc.MainDocumentPart.Document.Save();
                doc.Close();

                //doc.MainDocumentPart.PutXDocument();
            }
        }