private void AddParagraph(Body body, string idStyle, string text, JustificationValues justification = JustificationValues.Left)
        {
            Paragraph headingPar = wordUtils.CreateParagraphWithStyle(idStyle, justification);

            wordUtils.AddTextToParagraph(headingPar, text);
            body.AppendChild(headingPar);
        }
Example #2
0
        private void btnSimpleWordTest_Click(object sender, EventArgs e)
        {
            try
            {
                string filepath = "test.docx";
                string msg      = "Hello World!";
                using (WordprocessingDocument doc = WordprocessingDocument.Create(filepath,
                                                                                  DocumentFormat.OpenXml.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());

                    // Define the styles
                    Word.AddStyle(mainPart, "MyHeading1", "Titolone", "Verdana", 30, "0000FF", false, true, true);
                    Word.AddStyle(mainPart, "MyTypescript", "Macchina da scrivere", "Courier New", 10, "333333", true, false, false);

                    // Add MyHeading1 styled text
                    Paragraph headingPar = Word.CreateParagraphWithStyle("MyHeading1", JustificationValues.Center);
                    Word.AddTextToParagraph(headingPar, "Titolo con stile applicato");
                    body.AppendChild(headingPar);

                    // Add simple text
                    Paragraph para = body.AppendChild(new Paragraph());
                    Run       run  = para.AppendChild(new Run());
                    // String msg contains the text, "Hello, Word!"
                    run.AppendChild(new Text(msg));

                    // Add MyTypescript styled text

                    Paragraph typescriptParagraph = Word.CreateParagraphWithStyle("MyTypescript", JustificationValues.Left);
                    Word.AddTextToParagraph(typescriptParagraph, "È universalmente riconosciuto che un lettore che osserva il layout di una pagina viene distratto dal contenuto testuale se questo è leggibile. Lo scopo dell’utilizzo del Lorem Ipsum è che offre una normale distribuzione delle lettere (al contrario di quanto avviene se si utilizzano brevi frasi ripetute, ad esempio “testo qui”), apparendo come un normale blocco di testo leggibile. Molti software di impaginazione e di web design utilizzano Lorem Ipsum come testo modello. Molte versioni del testo sono state prodotte negli anni, a volte casualmente, a volte di proposito (ad esempio inserendo passaggi ironici).");
                    body.AppendChild(typescriptParagraph);

                    // Append a paragraph with styles
                    Paragraph newPar = createParagraphWithStyles();
                    body.AppendChild(newPar);

                    // Append a table
                    string[][] c = new string[4][];
                    for (int i = 0; i < c.Length; i++)
                    {
                        c[i] = new string[5];
                        for (int j = 0; j < c[i].Length; j++)
                        {
                            c[i][j] = i + "-" + j;
                        }
                    }
                    body.Append(Word.CreateTable(c, Word.GetTableProperties("#000000", BorderValues.Thick, "5000")));

                    // Append bullet list
                    string[] texts = { "First element", "Second Element", "Third Element" };
                    Word.CreateBulletNumberingPart(mainPart);
                    List <Paragraph> bulletList = new List <Paragraph>();
                    Word.CreateBulletOrNumberedList(100, 200, bulletList, texts.Length, texts);
                    foreach (Paragraph paragraph in bulletList)
                    {
                        body.Append(paragraph);
                    }

                    // Append numbered list
                    List <Paragraph> numberedList = new List <Paragraph>();
                    Word.CreateBulletOrNumberedList(100, 240, numberedList, texts.Length, texts, false);
                    foreach (Paragraph paragraph in numberedList)
                    {
                        body.Append(paragraph);
                    }

                    // Append image
                    Word.InsertPicture(doc, "panorama.jpg");
                }
                MessageBox.Show("Il documento è pronto!");
                Process.Start(filepath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problemi col documento. Se è aperto da un altro programma, chiudilo e riprova... \n" + ex.Message);
            }
        }