コード例 #1
0
        private static void UpdateStylesWithEffectsPartForToa(WordprocessingDocument doc)
        {
            StylesWithEffectsPart stylesWithEffectsPart = doc.MainDocumentPart.StylesWithEffectsPart;

            if (stylesWithEffectsPart == null)
            {
                return;
            }
            UpdatePartForToa(stylesWithEffectsPart);
        }
コード例 #2
0
        private static void UpdateStylesWithEffectsPartForTof(WordprocessingDocument doc)
        {
            StylesWithEffectsPart stylesWithEffectsPart = doc.MainDocumentPart.StylesWithEffectsPart;

            if (stylesWithEffectsPart == null)
            {
                throw new OpenXmlPowerToolsException("Document does not contain styles with effects part");
            }
            XDocument stylesWithEffectsXDoc = stylesWithEffectsPart.GetXDocument();

            UpdateAStylePartForTof(stylesWithEffectsXDoc);
            stylesWithEffectsPart.PutXDocument();
        }
コード例 #3
0
        private void UpdateStylesWithEffectsPartForTof(WordprocessingDocument doc)
        {
            StylesWithEffectsPart stylesWithEffectsPart = doc.MainDocumentPart.StylesWithEffectsPart;

            if (stylesWithEffectsPart == null)
            {
                stylesWithEffectsPart = doc.MainDocumentPart.AddNewPart <StylesWithEffectsPart>();
            }
            XDocument stylesWithEffectsXDoc = stylesWithEffectsPart.GetXDocument();

            UpdateAStylePartForTof(stylesWithEffectsXDoc);
            stylesWithEffectsPart.PutXDocument();
        }
コード例 #4
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);
            }
        }
コード例 #5
0
        /// <summary>
        /// This method gets a C# Document instance and prints every element of it to word.
        /// </summary>
        /// <param name="oDocument">The instance of Document.cs.</param>
        ///
        public void ToFile(Document oDocument)
        {
            // Delete the previous file in this dir and copy the new template there.
            if (File.Exists(wordFilePath))
            {
                File.Delete(wordFilePath);
            }
            File.Copy(templateFilePath, wordFilePath, true);
            // Open the JsonToWord.docx in edit mode.
            using (WordprocessingDocument wpd = WordprocessingDocument.Open(wordFilePath, true))
            {
                // Gets the body of the document and initializes the WordElementWriter.
                MainDocumentPart mainPart = wpd.MainDocumentPart;
                Body             body     = mainPart.Document.Body;
                wordWriter = new WordElementWriter(wpd);
                // Start style definitions
                // These lines are mandatory as they initialize the various style parts in the doc. The program crashes without these.
                StyleDefinitionsPart sdp = wpd.MainDocumentPart.StyleDefinitionsPart;
                if (sdp == null)
                {
                    sdp = wpd.MainDocumentPart.AddNewPart <StyleDefinitionsPart>();
                    Styles root = new Styles();
                    root.Save(sdp);
                }
                StylesWithEffectsPart sep = wpd.MainDocumentPart.StylesWithEffectsPart;
                if (sep == null)
                {
                    sep = wpd.MainDocumentPart.AddNewPart <StylesWithEffectsPart>();
                    Styles root = new Styles();
                    root.Save(sep);
                }
                // End of style definitions.
                // Call to method for replacing the title and the version on the cover page.
                this.ReplaceCover(wpd, oDocument);

                // Prints the front page with Title, MAnual version,HMI_Version,date of creation etc.
                Paragraph p        = body.AppendChild(new Paragraph());
                Run       runTitle = p.AppendChild(new Run(new Word.Text(oDocument.Title)));
                p.Append(new Run(new Break()));
                Run runType = p.AppendChild(new Run(new Word.Text(oDocument.Type.ToString() + " Manual")));
                p.Append(new Run(new Break()));
                Run runVersion = p.AppendChild(new Run(new Word.Text(" Manual Version : " + oDocument.Version)));
                p.Append(new Run(new Break()));
                Run runHMI_Version = p.AppendChild(new Run(new Word.Text("HMI version :" + oDocument.HMI_Version)));
                p.Append(new Run(new Break()));
                Run runDate = p.AppendChild(new Run(new Word.Text("Date : " + oDocument.DateCreated)));
                p.Append(new Run(new Break()));
                // The following 2 lines align the text of the previous paragraph to the center of the page.
                p.ParagraphProperties = new ParagraphProperties(new Justification());
                p.ParagraphProperties.Justification.Val = JustificationValues.Center;

                // The following line breaks to a new page.
                body.Append(new Paragraph(new Run(new Break()
                {
                    Type = BreakValues.Page
                })));

                // Inserts a TOC that includes headings through level 4.
                // This is the only piece of code that makes use of TocAdder project.
                XElement firstPara = wpd
                                     .MainDocumentPart
                                     .GetXDocument()
                                     .Descendants(W.p)
                                     .Skip(2)
                                     .FirstOrDefault();
                TocAdder.AddToc(wpd, firstPara,
                                @"TOC \o '1-4' \h \z \u", null, null);
                // Appends a page break.
                body.Append(new Paragraph(new Run(new Break()
                {
                    Type = BreakValues.Page
                })));
                // Prints the elements.
                foreach (Chapter chapter in oDocument.ChaptersList)
                {
                    // Get the chapter's title, place it to the doc and format it to Heading1.
                    Paragraph chapterTitle = body.AppendChild(new Paragraph(new Run(new Word.Text(chapter.Title))));
                    chapterTitle.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId());
                    chapterTitle.ParagraphProperties.ParagraphStyleId.Val = "Heading1";
                    // This paragraph is the one not used in the methods of WordElementWriter.
                    // It is here to serve as a future placeholder of elements, so the methods of WordElementWriter return a paragraph with the printed element.
                    Paragraph paragraph;
                    foreach (var block in chapter.ChapterContents)
                    {
                        foreach (BasicContentBlock element in block.ContentBlocks)
                        {
                            paragraph = new Paragraph();
                            wordWriter.AppendElement(element, paragraph);
                            body.Append(new Paragraph(new Run(new Break())));
                        }
                    }
                }

                wpd.Save();
            }
        }