Exemplo n.º 1
0
        private static void HeadersAndFootersWithImagesAndTablesUsingInsertPicture()
        {
            Console.WriteLine("\tHeadersAndFootersWithImagesAndTablesUsingInsertPicture()");

            // Create a new document.
            using (DocX document = DocX.Create(@"docs\HeadersAndFootersWithImagesAndTablesUsingInsertPicture.docx"))
            {
                // Add a template logo image to this document.
                RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
                rd.Up(2);
                Image logo = document.AddImage(rd.Path + @"\images\logo_the_happy_builder.png");

                // Add Headers and Footers to this document.
                document.AddHeaders();
                document.AddFooters();

                // Force the first page to have a different Header and Footer.
                document.DifferentFirstPage = true;

                // Force odd & even pages to have different Headers and Footers.
                document.DifferentOddAndEvenPages = true;

                // Get the first, odd and even Headers for this document.
                Header header_first = document.Headers.first;
                Header header_odd = document.Headers.odd;
                Header header_even = document.Headers.even;

                // Get the first, odd and even Footer for this document.
                Footer footer_first = document.Footers.first;
                Footer footer_odd = document.Footers.odd;
                Footer footer_even = document.Footers.even;

                // Insert a Paragraph into the first Header.
                Paragraph p0 = header_first.InsertParagraph();
                p0.Append("Hello First Header.").Bold();

                // Insert a Paragraph into the odd Header.
                Paragraph p1 = header_odd.InsertParagraph();
                p1.Append("Hello Odd Header.").Bold();

                // Insert a Paragraph into the even Header.
                Paragraph p2 = header_even.InsertParagraph();
                p2.Append("Hello Even Header.").Bold();

                // Insert a Paragraph into the first Footer.
                Paragraph p3 = footer_first.InsertParagraph();
                p3.Append("Hello First Footer.").Bold();

                // Insert a Paragraph into the odd Footer.
                Paragraph p4 = footer_odd.InsertParagraph();
                p4.Append("Hello Odd Footer.").Bold();

                // Insert a Paragraph into the even Header.
                Paragraph p5 = footer_even.InsertParagraph();
                p5.Append("Hello Even Footer.").Bold();

                // Insert a Paragraph into the document.
                Paragraph p6 = document.InsertParagraph();
                p6.AppendLine("Hello First page.");

                // Create a second page to show that the first page has its own header and footer.
                p6.InsertPageBreakAfterSelf();

                // Insert a Paragraph after the page break.
                Paragraph p7 = document.InsertParagraph();
                p7.AppendLine("Hello Second page.");

                // Create a third page to show that even and odd pages have different headers and footers.
                p7.InsertPageBreakAfterSelf();

                // Insert a Paragraph after the page break.
                Paragraph p8 = document.InsertParagraph();
                p8.AppendLine("Hello Third page.");

                //Insert a next page break, which is a section break combined with a page break
                document.InsertSectionPageBreak();

                //Insert a paragraph after the "Next" page break
                Paragraph p9 = document.InsertParagraph();
                p9.Append("Next page section break.");

                //Insert a continuous section break
                document.InsertSection();

                //Create a paragraph in the new section
                var p10 = document.InsertParagraph();
                p10.Append("Continuous section paragraph.");

                // Inserting logo into footer and header into Tables

                #region Company Logo in Header in Table
                // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
                Table header_first_table = header_first.InsertTable(1, 2);
                header_first_table.Design = TableDesign.TableGrid;
                header_first_table.AutoFit = AutoFit.Window;
                // Get the upper right Paragraph in the layout_table.
                Paragraph upperRightParagraph = header_first.Tables[0].Rows[0].Cells[1].Paragraphs[0];
                // Insert this template logo into the upper right Paragraph of Table.
                upperRightParagraph.InsertPicture(logo.CreatePicture());
                upperRightParagraph.Alignment = Alignment.right;

                // Get the upper left Paragraph in the layout_table.
                Paragraph upperLeftParagraphFirstTable = header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0];
                upperLeftParagraphFirstTable.Append("Company Name - DocX Corporation");
                #endregion

                #region Company Logo in Header in Invisible Table
                // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
                Table header_second_table = header_odd.InsertTable(1, 2);
                header_second_table.Design = TableDesign.None;
                header_second_table.AutoFit = AutoFit.Window;
                // Get the upper right Paragraph in the layout_table.
                Paragraph upperRightParagraphSecondTable = header_second_table.Rows[0].Cells[1].Paragraphs[0];
                // Insert this template logo into the upper right Paragraph of Table.
                upperRightParagraphSecondTable.InsertPicture(logo.CreatePicture());
                upperRightParagraphSecondTable.Alignment = Alignment.right;

                // Get the upper left Paragraph in the layout_table.
                Paragraph upperLeftParagraphSecondTable = header_second_table.Rows[0].Cells[0].Paragraphs[0];
                upperLeftParagraphSecondTable.Append("Company Name - DocX Corporation");
                #endregion

                #region Company Logo in Footer in Table
                // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
                Table footer_first_table = footer_first.InsertTable(1, 2);
                footer_first_table.Design = TableDesign.TableGrid;
                footer_first_table.AutoFit = AutoFit.Window;
                // Get the upper right Paragraph in the layout_table.
                Paragraph upperRightParagraphFooterParagraph = footer_first.Tables[0].Rows[0].Cells[1].Paragraphs[0];
                // Insert this template logo into the upper right Paragraph of Table.
                upperRightParagraphFooterParagraph.InsertPicture(logo.CreatePicture());
                upperRightParagraphFooterParagraph.Alignment = Alignment.right;

                // Get the upper left Paragraph in the layout_table.
                Paragraph upperLeftParagraphFirstTableFooter = footer_first.Tables[0].Rows[0].Cells[0].Paragraphs[0];
                upperLeftParagraphFirstTableFooter.Append("Company Name - DocX Corporation");
                #endregion

                #region Company Logo in Header in Invisible Table
                // Insert Table into First Header - Create a new Table with 2 columns and 1 rows.
                Table footer_second_table = footer_odd.InsertTable(1, 2);
                footer_second_table.Design = TableDesign.None;
                footer_second_table.AutoFit = AutoFit.Window;
                // Get the upper right Paragraph in the layout_table.
                Paragraph upperRightParagraphSecondTableFooter = footer_second_table.Rows[0].Cells[1].Paragraphs[0];
                // Insert this template logo into the upper right Paragraph of Table.
                upperRightParagraphSecondTableFooter.InsertPicture(logo.CreatePicture());
                upperRightParagraphSecondTableFooter.Alignment = Alignment.right;

                // Get the upper left Paragraph in the layout_table.
                Paragraph upperLeftParagraphSecondTableFooter = footer_second_table.Rows[0].Cells[0].Paragraphs[0];
                upperLeftParagraphSecondTableFooter.Append("Company Name - DocX Corporation");
                #endregion

                // Save all changes to this document.
                document.Save();

                Console.WriteLine("\tCreated: docs\\HeadersAndFootersWithImagesAndTablesUsingInsertPicture.docx\n");
            }// Release this document from memory.
        }
Exemplo n.º 2
0
        // Create an invoice template.
        private static DocX CreateInvoiceTemplate()
        {
            // Create a new document.
            DocX document = DocX.Create(@"docs\InvoiceTemplate.docx");

            // Create a table for layout purposes (This table will be invisible).
            Table layout_table = document.InsertTable(2, 2);
            layout_table.Design = TableDesign.TableNormal;
            layout_table.AutoFit = AutoFit.Window;

            // Dark formatting
            Formatting dark_formatting = new Formatting();
            dark_formatting.Bold = true;
            dark_formatting.Size = 12;
            dark_formatting.FontColor = Color.FromArgb(31, 73, 125);

            // Light formatting
            Formatting light_formatting = new Formatting();
            light_formatting.Italic = true;
            light_formatting.Size = 11;
            light_formatting.FontColor = Color.FromArgb(79, 129, 189);

            #region Company Name
            // Get the upper left Paragraph in the layout_table.
            Paragraph upper_left_paragraph = layout_table.Rows[0].Cells[0].Paragraphs[0];

            // Create a custom property called company_name
            CustomProperty company_name = new CustomProperty("company_name", "Company Name");

            // Insert a field of type doc property (This will display the custom property 'company_name')
            layout_table.Rows[0].Cells[0].Paragraphs[0].InsertDocProperty(company_name, f: dark_formatting);

            // Force the next text insert to be on a new line.
            upper_left_paragraph.InsertText("\n", false);
            #endregion

            #region Company Slogan
            // Create a custom property called company_slogan
            CustomProperty company_slogan = new CustomProperty("company_slogan", "Company slogan goes here.");

            // Insert a field of type doc property (This will display the custom property 'company_slogan')
            upper_left_paragraph.InsertDocProperty(company_slogan, f: light_formatting);
            #endregion

            #region Company Logo
            // Get the upper right Paragraph in the layout_table.
            Paragraph upper_right_paragraph = layout_table.Rows[0].Cells[1].Paragraphs[0];

            // Add a template logo image to this document.
            RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
            rd.Up(2);
            Image logo = document.AddImage(rd.Path + @"\images\logo_template.png");

            // Insert this template logo into the upper right Paragraph.
            upper_right_paragraph.InsertPicture(logo.CreatePicture());

            upper_right_paragraph.Alignment = Alignment.right;
            #endregion

            // Custom properties cannot contain newlines, so the company address must be split into 3 custom properties.
            #region Hired Company Address
            // Create a custom property called company_address_line_one
            CustomProperty hired_company_address_line_one = new CustomProperty("hired_company_address_line_one", "Street Address,");

            // Get the lower left Paragraph in the layout_table.
            Paragraph lower_left_paragraph = layout_table.Rows[1].Cells[0].Paragraphs[0];
            lower_left_paragraph.InsertText("TO:\n", false, dark_formatting);

            // Insert a field of type doc property (This will display the custom property 'hired_company_address_line_one')
            lower_left_paragraph.InsertDocProperty(hired_company_address_line_one, f: light_formatting);

            // Force the next text insert to be on a new line.
            lower_left_paragraph.InsertText("\n", false);

            // Create a custom property called company_address_line_two
            CustomProperty hired_company_address_line_two = new CustomProperty("hired_company_address_line_two", "City,");

            // Insert a field of type doc property (This will display the custom property 'hired_company_address_line_two')
            lower_left_paragraph.InsertDocProperty(hired_company_address_line_two, f: light_formatting);

            // Force the next text insert to be on a new line.
            lower_left_paragraph.InsertText("\n", false);

            // Create a custom property called company_address_line_two
            CustomProperty hired_company_address_line_three = new CustomProperty("hired_company_address_line_three", "Zip Code");

            // Insert a field of type doc property (This will display the custom property 'hired_company_address_line_three')
            lower_left_paragraph.InsertDocProperty(hired_company_address_line_three, f: light_formatting);
            #endregion

            #region Date & Invoice number
            // Get the lower right Paragraph from the layout table.
            Paragraph lower_right_paragraph = layout_table.Rows[1].Cells[1].Paragraphs[0];

            CustomProperty invoice_date = new CustomProperty("invoice_date", DateTime.Today.Date.ToString("d"));
            lower_right_paragraph.InsertText("Date: ", false, dark_formatting);
            lower_right_paragraph.InsertDocProperty(invoice_date, f: light_formatting);

            CustomProperty invoice_number = new CustomProperty("invoice_number", 1);
            lower_right_paragraph.InsertText("\nInvoice: ", false, dark_formatting);
            lower_right_paragraph.InsertText("#", false, light_formatting);
            lower_right_paragraph.InsertDocProperty(invoice_number, f: light_formatting);

            lower_right_paragraph.Alignment = Alignment.right;
            #endregion

            // Insert an empty Paragraph between two Tables, so that they do not touch.
            document.InsertParagraph(string.Empty, false);

            // This table will hold all of the invoice data.
            Table invoice_table = document.InsertTable(4, 4);
            invoice_table.Design = TableDesign.LightShadingAccent1;
            invoice_table.Alignment = Alignment.center;

            // A nice thank you Paragraph.
            Paragraph thankyou = document.InsertParagraph("\nThank you for your business, we hope to work with you again soon.", false, dark_formatting);
            thankyou.Alignment = Alignment.center;

            #region Hired company details
            CustomProperty hired_company_details_line_one = new CustomProperty("hired_company_details_line_one", "Street Address, City, ZIP Code");
            CustomProperty hired_company_details_line_two = new CustomProperty("hired_company_details_line_two", "Phone: 000-000-0000, Fax: 000-000-0000, e-mail: [email protected]");

            Paragraph companyDetails = document.InsertParagraph(string.Empty, false);
            companyDetails.InsertDocProperty(hired_company_details_line_one, f: light_formatting);
            companyDetails.InsertText("\n", false);
            companyDetails.InsertDocProperty(hired_company_details_line_two, f: light_formatting);
            companyDetails.Alignment = Alignment.center;
            #endregion

            // Return the document now that it has been created.
            return document;
        }
Exemplo n.º 3
0
        // Create an invoice for a factitious company called "The Happy Builder".
        private static DocX CreateInvoiceFromTemplate(DocX template)
        {
            #region Logo
            // A quick glance at the template shows us that the logo Paragraph is in row zero cell 1.
            Paragraph logo_paragraph = template.Tables[0].Rows[0].Cells[1].Paragraphs[0];
            // Remove the template Picture that is in this Paragraph.
            logo_paragraph.Pictures[0].Remove();

            // Add the Happy Builders logo to this document.
            RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
            rd.Up(2);
            Image logo = template.AddImage(rd.Path + @"\images\logo_the_happy_builder.png");

            // Insert the Happy Builders logo into this Paragraph.
            logo_paragraph.InsertPicture(logo.CreatePicture());
            #endregion

            #region Set CustomProperty values
            // Set the value of the custom property 'company_name'.
            template.AddCustomProperty(new CustomProperty("company_name", "The Happy Builder"));

            // Set the value of the custom property 'company_slogan'.
            template.AddCustomProperty(new CustomProperty("company_slogan", "No job too small"));

            // Set the value of the custom properties 'hired_company_address_line_one', 'hired_company_address_line_two' and 'hired_company_address_line_three'.
            template.AddCustomProperty(new CustomProperty("hired_company_address_line_one", "The Crooked House,"));
            template.AddCustomProperty(new CustomProperty("hired_company_address_line_two", "Dublin,"));
            template.AddCustomProperty(new CustomProperty("hired_company_address_line_three", "12345"));

            // Set the value of the custom property 'invoice_date'.
            template.AddCustomProperty(new CustomProperty("invoice_date", DateTime.Today.Date.ToString("d")));

            // Set the value of the custom property 'invoice_number'.
            template.AddCustomProperty(new CustomProperty("invoice_number", 1));

            // Set the value of the custom property 'hired_company_details_line_one' and 'hired_company_details_line_two'.
            template.AddCustomProperty(new CustomProperty("hired_company_details_line_one", "Business Street, Dublin, 12345"));
            template.AddCustomProperty(new CustomProperty("hired_company_details_line_two", "Phone: 012-345-6789, Fax: 012-345-6789, e-mail: [email protected]"));
            #endregion

            /*
             * InvoiceTemplate.docx contains a blank Table,
             * we want to replace this with a new Table that
             * contains all of our invoice data.
             */
            Table t = template.Tables[1];
            Table invoice_table = CreateAndInsertInvoiceTableAfter(t, ref template);
            t.Remove();

            // Return the template now that it has been modified to hold all of our custom data.
            return template;
        }
Exemplo n.º 4
0
        private static void HyperlinksImagesTablesWithLists()
        {
            Console.WriteLine("\tHyperlinksImagesTablesWithLists()");

            // Create a document.
            using (DocX document = DocX.Create(@"docs\HyperlinksImagesTablesWithLists.docx"))
            {
                // Add a hyperlink into the document.
                Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));

                // created numbered lists
                var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered, 1);
                document.AddListItem(numberedList, "First sub list item", 1);
                document.AddListItem(numberedList, "Second List Item.");
                document.AddListItem(numberedList, "Third list item.");
                document.AddListItem(numberedList, "Nested item.", 1);
                document.AddListItem(numberedList, "Second nested item.", 1);

                // created bulleted lists

                var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
                document.AddListItem(bulletedList, "Second bullet item");
                document.AddListItem(bulletedList, "Sub bullet item", 1);
                document.AddListItem(bulletedList, "Second sub bullet item", 1);
                document.AddListItem(bulletedList, "Third bullet item");

                // Add a Table into the document.
                Table table = document.AddTable(2, 2);
                table.Design = TableDesign.ColorfulGridAccent2;
                table.Alignment = Alignment.center;
                table.Rows[0].Cells[0].Paragraphs[0].Append("1");
                table.Rows[0].Cells[1].Paragraphs[0].Append("2");
                table.Rows[1].Cells[0].Paragraphs[0].Append("3");
                table.Rows[1].Cells[1].Paragraphs[0].Append("4");

                Row newRow = table.InsertRow(table.Rows[1]);
                newRow.ReplaceText("4", "5");

                // Add an image into the document.
                RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
                rd.Up(2);
                Image image = document.AddImage(rd.Path + @"\images\logo_template.png");

                // Create a picture (A custom view of an Image).
                Picture picture = image.CreatePicture();
                picture.Rotation = 10;
                picture.SetPictureShape(BasicShapes.cube);

                // Insert a new Paragraph into the document.
                Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
                title.Alignment = Alignment.center;

                // Insert a new Paragraph into the document.
                Paragraph p1 = document.InsertParagraph();

                // Append content to the Paragraph
                p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
                p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append(".");
                p1.AppendLine();
                p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
                p1.AppendLine();
                p1.AppendLine("Can you check this Table of figures for me?");
                p1.AppendLine();

                // Insert the Table after Paragraph 1.
                p1.InsertTableAfterSelf(table);

                // Insert a new Paragraph into the document.
                Paragraph p2 = document.InsertParagraph();

                // Append content to the Paragraph.
                p2.AppendLine("Is it correct?");
                p2.AppendLine();
                p2.AppendLine("Adding bullet list below: ");

                document.InsertList(bulletedList);

                // Adding another paragraph to add table and bullet list after it
                Paragraph p3 = document.InsertParagraph();
                p3.AppendLine();
                p3.AppendLine("Adding another table...");

                // Adding another table
                Table table1 = document.AddTable(2, 2);
                table1.Design = TableDesign.ColorfulGridAccent2;
                table1.Alignment = Alignment.center;
                table1.Rows[0].Cells[0].Paragraphs[0].Append("1");
                table1.Rows[0].Cells[1].Paragraphs[0].Append("2");
                table1.Rows[1].Cells[0].Paragraphs[0].Append("3");
                table1.Rows[1].Cells[1].Paragraphs[0].Append("4");

                Paragraph p4 = document.InsertParagraph();
                p4.InsertTableBeforeSelf(table1);

                p4.AppendLine();

                // Insert numbered list after table
                Paragraph p5 = document.InsertParagraph();
                p5.AppendLine("Adding numbered list below: ");
                p5.AppendLine();
                document.InsertList(numberedList);

                // Save this document.
                document.Save();

                Console.WriteLine("\tCreated: docs\\HyperlinksImagesTablesWithLists.docx\n");
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a document with a Hyperlink, an Image and a Table.
        /// </summary>
        private static void HyperlinksImagesTables()
        {
            Console.WriteLine("\tHyperlinksImagesTables()");

            // Create a document.
            using (DocX document = DocX.Create(@"docs\HyperlinksImagesTables.docx"))
            {
                // Add a hyperlink into the document.
                Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));

                // Add a Table into the document.
                Table table = document.AddTable(2, 2);
                table.Design = TableDesign.ColorfulGridAccent2;
                table.Alignment = Alignment.center;
                table.Rows[0].Cells[0].Paragraphs[0].Append("1");
                table.Rows[0].Cells[1].Paragraphs[0].Append("2");
                table.Rows[1].Cells[0].Paragraphs[0].Append("3");
                table.Rows[1].Cells[1].Paragraphs[0].Append("4");

                Row newRow = table.InsertRow(table.Rows[1]);
                newRow.ReplaceText("4", "5");

                // Add an image into the document.
                RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
                rd.Up(2);
                Image image = document.AddImage(rd.Path + @"\images\logo_template.png");

                // Create a picture (A custom view of an Image).
                Picture picture = image.CreatePicture();
                picture.Rotation = 10;
                picture.SetPictureShape(BasicShapes.cube);

                // Insert a new Paragraph into the document.
                Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
                title.Alignment = Alignment.center;

                // Insert a new Paragraph into the document.
                Paragraph p1 = document.InsertParagraph();

                // Append content to the Paragraph
                p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
                p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append(".");
                p1.AppendLine();
                p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
                p1.AppendLine();
                p1.AppendLine("Can you check this Table of figures for me?");
                p1.AppendLine();

                // Insert the Table after Paragraph 1.
                p1.InsertTableAfterSelf(table);

                // Insert a new Paragraph into the document.
                Paragraph p2 = document.InsertParagraph();

                // Append content to the Paragraph.
                p2.AppendLine("Is it correct?");

                // Save this document.
                document.Save();

                Console.WriteLine("\tCreated: docs\\HyperlinksImagesTables.docx\n");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Create a document with two pictures. One picture is inserted normal way, the other one with rotation
        /// </summary>
        static void HelloWorldAddPictureToWord()
        {
            Console.WriteLine("\tHelloWorldAddPictureToWord()");

            // Create a document.
            using (DocX document = DocX.Create(@"docs\HelloWorldAddPictureToWord.docx"))
            {
                // Add an image into the document.
                RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
                rd.Up(2);
                Image image = document.AddImage(rd.Path + @"\images\logo_template.png");

                // Create a picture (A custom view of an Image).
                Picture picture = image.CreatePicture();
                picture.Rotation = 10;
                picture.SetPictureShape(BasicShapes.cube);

                // Insert a new Paragraph into the document.
                Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize(20).Font(new Font("Comic Sans MS"));
                title.Alignment = Alignment.center;

                // Insert a new Paragraph into the document.
                Paragraph p1 = document.InsertParagraph();

                // Append content to the Paragraph
                p1.AppendLine("Just below there should be a picture ").Append("picture").Bold().Append(" inserted in a non-conventional way.");
                p1.AppendLine();
                p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
                p1.AppendLine();

                // Insert a new Paragraph into the document.
                Paragraph p2 = document.InsertParagraph();
                // Append content to the Paragraph.

                p2.AppendLine("Is it correct?");
                p2.AppendLine();

                // Lets add another picture (without the fancy stuff)
                Picture pictureNormal = image.CreatePicture();

                Paragraph p3 = document.InsertParagraph();
                p3.AppendLine("Lets add another picture (without the fancy  rotation stuff)");
                p3.AppendLine();
                p3.AppendPicture(pictureNormal);

                // Save this document.
                document.Save();
                Console.WriteLine("\tCreated: docs\\HelloWorldAddPictureToWord.docx\n");
            }
        }