Monday, 30 July 2012

add Subscript and Superscript text in a paragraph in the Word Document using OpenXML SDK


using (WordprocessingDocument wordDocument =
              WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
                {
                    // Add a main document part.
                    MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

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

                    RunProperties runProperties = new RunProperties();

                    //font family
                    RunFonts font1 = new RunFonts() { Ascii = "Arial" };
                    run.AppendChild(font1);

                    //font size
                    FontSize fs = new FontSize();
                    fs.Val = "22";
                    runProperties.AppendChild(fs);


                    //Super script
                    VerticalTextAlignment v = new VerticalTextAlignment();
                    v.Val = VerticalPositionValues.Superscript;
                    runProperties.AppendChild(v);
                    run.AppendChild(runProperties);
                    run.AppendChild(new Text("Create text in body – CreateWordprocessingDocument  using Superscript"));

                    mainPart.Document.Save();

                    wordDocument.Close();
                }

No comments:

Post a Comment