Showing posts with label OpenXML. Show all posts
Showing posts with label OpenXML. Show all posts

Tuesday, 31 July 2012

HTML To Word Document using OpenXMl



Download OpenXMLSDKTool.msi  and run it, you will  get DocumentFormat.OpenXml.dll 

From this link you will get HTMLtoOpenXML.dll

Add these two reference.

using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using NotesFor.HtmlToOpenXml;
...

static void Main(string[] args)
{
     const string filename = "D:\test.docx";
     string html = "<font size='10' face='arial'>Hi this is html to word convert</font>" ;

     if (File.Exists(filename)) File.Delete(filename);

     using (MemoryStream generatedDocument = new MemoryStream())
     {
          using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
          {
               MainDocumentPart mainPart = package.MainDocumentPart;
               if (mainPart == null)
               {
                    mainPart = package.AddMainDocumentPart();
                    new Document(new Body()).Save(mainPart);
               }

               HtmlConverter converter = new HtmlConverter(mainPart);
               Body body = mainPart.Document.Body;

               var paragraphs = converter.Parse(html);
               for (int i = 0; i < paragraphs.Count; i++)
               {
                    body.Append(paragraphs[i]);
               }

               mainPart.Document.Save();
          }

          File.WriteAllBytes(filename, generatedDocument.ToArray());
     }

     System.Diagnostics.Process.Start(filename);
}
                                                                 





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();
                }