Exemple #1
0
        public static void Test_Style_01()
        {
            SetDirectory();
            string file = "test_Style_01.docx";

            Trace.WriteLine("create docx \"{0}\" using OXmlDoc", file);

            using (WordprocessingDocument doc = WordprocessingDocument.Create(zPath.Combine(_directory, file), WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = doc.AddMainDocumentPart();

                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());

                //body.AppendChild(Test_OXmlCreator.CreateSectionProperties());
                SectionProperties sectionProperties = body.AppendChild(new SectionProperties());
                Test_OpenXml_Creator.SetSectionPage(sectionProperties);

                //SectionProperties sectionProperties = new SectionProperties();
                //// PageSize, <w:pgSz w:w="11907" w:h="16839"/>, 11907 1/20 point = 21 cm, 16839 1/20 point = 29.7 cm, unit = 1/20 point, 11907 1/20 point = 595.35 point
                //sectionProperties.AppendChild(new PageSize { Width = 11907, Height = 16839 });
                //// top 1.27 cm, bottom 1.27 cm, left 2.5 cm, right 2.5 cm, header 0.5 cm, footer 0.5 cm
                //// <w:pgMar w:top="720" w:right="1418" w:bottom="720" w:left="1418" w:header="284" w:footer="284" w:gutter="0"/>
                //sectionProperties.AppendChild(new PageMargin { Top = 720, Bottom = 720, Left = 1418, Right = 1418, Header = 284, Footer = 284 });
                //body.AppendChild(sectionProperties);

                //Trace.WriteLine("add StyleDefinitionsPart");
                Styles styles = Test_OpenXml_Creator.CreateStyles(mainPart);

                //Trace.WriteLine("create DocDefaults");
                styles.DocDefaults = Test_OpenXml_Creator.CreateDocDefaults();
                //string tinyParagraphStyleId = Test_OXmlCreator.CreateStyleTinyParagraph(styles);
                string tinyParagraphStyleId = "TinyParagraph";
                styles.Append(Test_OpenXml_Creator.CreateTinyParagraphStyle(tinyParagraphStyleId));

                //string styleAliases = "style_01";
                //Trace.WriteLine($"create style {styleId}");


                //Paragraph paragraph = body.AppendChild(new Paragraph());
                //Run run = paragraph.AppendChild(new Run());
                //run.RunProperties = new RunProperties(new RunFonts() { Ascii = "Arial" });

                //RunProperties runProperties = new RunProperties(new RunFonts() { Ascii = "Arial" });
                //Run r = package.MainDocumentPart.Document.Descendants<Run>().First();
                //r.PrependChild<RunProperties>(rPr);

                ParagraphProperties paragraphProperties = new ParagraphProperties();
                // ParagraphStyleId, <w:pStyle>
                paragraphProperties.ParagraphStyleId = new ParagraphStyleId {
                    Val = tinyParagraphStyleId
                };

                //AddText(body);
                //AddText(body, paragraphProperties);
                body.Append(Test_OpenXml_Creator.CreateText_01(paragraphProperties));
            }
        }
Exemple #2
0
        public static void Test_DocDefaults_01()
        {
            SetDirectory();
            string file = "test_DocDefaults_01.docx";

            Trace.WriteLine("create docx \"{0}\" using OXmlDoc", file);

            using (WordprocessingDocument doc = WordprocessingDocument.Create(zPath.Combine(_directory, file), WordprocessingDocumentType.Document))
            {
                // Add a main document part.
                MainDocumentPart mainPart = doc.AddMainDocumentPart();

                // Create the document structure and add some text.
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());


                // Get the Styles part for this document.
                //StyleDefinitionsPart styleDefinitionsPart = mainPart.StyleDefinitionsPart;

                bool createDocDefaults = true;

                if (createDocDefaults)
                {
                    Styles styles = Test_OpenXml_Creator.CreateStyles(mainPart);
                    styles.DocDefaults = Test_OpenXml_Creator.CreateDocDefaults();

                    //Trace.WriteLine("add StyleDefinitionsPart");
                    //StyleDefinitionsPart styleDefinitionsPart = mainPart.AddNewPart<StyleDefinitionsPart>();
                    //Styles styles = new Styles();
                    //styleDefinitionsPart.Styles = styles;
                    ////styleDefinitionsPart.Styles.Save();
                    //styles.Save();

                    //Trace.WriteLine("create DocDefaults");
                    //DocDefaults docDefaults = new DocDefaults();
                    //RunPropertiesDefault runPropertiesDefault = new RunPropertiesDefault();
                    //RunPropertiesBaseStyle runPropertiesBaseStyle = new RunPropertiesBaseStyle();
                    //runPropertiesBaseStyle.RunFonts = new RunFonts { Ascii = "Arial" };
                    //runPropertiesBaseStyle.FontSize = new FontSize { Val = "22"  };
                    //runPropertiesDefault.RunPropertiesBaseStyle = runPropertiesBaseStyle;
                    //docDefaults.RunPropertiesDefault = runPropertiesDefault;
                    //styles.DocDefaults = docDefaults;

                    ////RunPropertiesDefault RunPropertiesDefault
                    //docDefaults.ParagraphPropertiesDefault = new ParagraphPropertiesDefault();

                    //string styleId = "style_01";
                    //Trace.WriteLine($"create style {styleId}");

                    //Style style = new Style()
                    //{
                    //    Type = StyleValues.Paragraph,
                    //    StyleId = styleId,
                    //    CustomStyle = true,
                    //    Default = false
                    //};

                    ////styleDefinitionsPart.Styles.Append(style);
                    //styles.Append(style);
                }
                //AddText(body);
                body.Append(Test_OpenXml_Creator.CreateText_01());
            }
        }