admin管理员组

文章数量:1122832

So basically I am trying to generate html from docx using openxml powertools. It handles whole thing except header and footer. I have file name main.docx, I am trying to create temporary docx file for header and footer and put header and footer of main docx to temp docx in Main Body. So I can use those temp docx to generate html and at end merge all. In this process it's difficult to copy header of file to main body of temp docx. Anyone tried ??? I also tried using documentbuilder but it does not have such method

  {
      var fi = new FileInfo(file);
      Console.WriteLine(fi.Name);
      byte[] byteArray = File.ReadAllBytes(fi.FullName);

      using (MemoryStream memoryStream = new MemoryStream())
      {
          memoryStream.Write(byteArray, 0, byteArray.Length);
          using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true))
          {
              var headerText = ExtractHeader(wDoc);
              var footerText = ExtractFooter(wDoc);

              var headerDocx = CreateHeaderFooterDocx(headerText,fi.Name);
              //var footerDocx = CreateHeaderFooterDocx(footerText, "footer.docx");

              var headerHTML = ConvertToHtml(headerDocx, outputDirectory,file);
              //var FooterHTML = ConvertToHtml(footerDocx, outputDirectory);
              var MainBodyHTML = ConvertToHtml(file, outputDirectory, file);

              var completeHtml = $@"
                  <html>
                  <head><title>DocxToHTML</title></head>
                  <body>
                  <header></header>
                                          {MainBodyHTML}
                  <footer></footer>
                  </body>
                  </html>";

              File.WriteAllText(fi.Length+".html", completeHtml, Encoding.UTF8);

          }
      }
  }

  public static string CreateHeaderFooterDocx(XElement headerFooterText, string fileName)
  {
      string tempFilePath = Path.Combine(Path.GetTempPath(), fileName);
      

      using (WordprocessingDocument tempDoc = WordprocessingDocument.Create(tempFilePath, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
      {
          MainDocumentPart mainPart = tempDoc.AddMainDocumentPart();

          Body body = new Body();

          foreach (var element in headerFooterText.Elements())
          {
              if (element.Name.LocalName == "Paragraph")
              {
                  Paragraph paragraph = new Paragraph(
                      new Run(new Text(element.Value))
                  );
                  body.Append(paragraph);
              }

          }

          mainPart.Document = new Document(body);

          tempDoc.Close();
      }

    return tempFilePath;
}

 public static XElement ConvertToHtml(string file,string outputDirectory,string originalFilePath)
 {
     var fi = new FileInfo(file);
     Console.WriteLine(fi.Name);
     byte[] byteArray = File.ReadAllBytes(fi.FullName);
     using (MemoryStream memoryStream = new MemoryStream())
     {
         using (WordprocessingDocument originalDoc = WordprocessingDocument.Open(originalFilePath, false))
         {

             var originalCoreFilePropertiesPart = originalDoc.CoreFilePropertiesPart;
             var originalCoreProps = originalDoc.PackageProperties;
             var originalExtendedProps = originalDoc.ExtendedFilePropertiesPart?.Properties;
             memoryStream.Write(byteArray, 0, byteArray.Length);
             using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true))
             {
                 if (originalFilePath != file)
                 {

                     CoreFilePropertiesPart coreFilePropertiesPart = originalDoc.CoreFilePropertiesPart;

                     if (coreFilePropertiesPart != null && coreFilePropertiesPart.GetXDocument().Root != null)
                     {
                         wDoc.AddCoreFilePropertiesPart();
                         XDocument xDocument = wDoc.CoreFilePropertiesPart.GetXDocument();
                         xDocument.Declaration.Standalone = "yes";
                         xDocument.Declaration.Encoding = "UTF-8";
                         XDocument xDocument2 = coreFilePropertiesPart.GetXDocument();
                         xDocument.Add(xDocument2.Root);
                     }

                     ExtendedFilePropertiesPart extendedFilePropertiesPart = originalDoc.ExtendedFilePropertiesPart;
                     if (extendedFilePropertiesPart != null)
                     {
                         wDoc.AddExtendedFilePropertiesPart();
                         XDocument xDocument3 = wDoc.ExtendedFilePropertiesPart.GetXDocument();
                         xDocument3.Declaration.Standalone = "yes";
                         xDocument3.Declaration.Encoding = "UTF-8";
                         xDocument3.Add(extendedFilePropertiesPart.GetXDocument().Root);
                     }

                     CustomFilePropertiesPart customFilePropertiesPart = originalDoc.CustomFilePropertiesPart;
                     if (customFilePropertiesPart != null)
                     {
                         wDoc.AddCustomFilePropertiesPart();
                         XDocument xDocument4 = wDoc.CustomFilePropertiesPart.GetXDocument();
                         xDocument4.Declaration.Standalone = "yes";
                         xDocument4.Declaration.Encoding = "UTF-8";
                         xDocument4.Add(customFilePropertiesPart.GetXDocument().Root);
                     }
                 }
                 var destFileName = new FileInfo(fi.Name.Replace(".docx", ".html"));
                 if (outputDirectory != null && outputDirectory != string.Empty)
                 {
                     DirectoryInfo di = new DirectoryInfo(outputDirectory);
                     if (!di.Exists)
                     {
                         throw new OpenXmlPowerToolsException("Output directory does not exist");
                     }
                     destFileName = new FileInfo(Path.Combine(di.FullName, destFileName.Name));
                 }
                 var imageDirectoryName = destFileName.FullName.Substring(0, destFileName.FullName.Length - 5) + "_files";
                 int imageCounter = 0;
                 var pageTitle = fi.FullName;
                 var part = wDoc.CoreFilePropertiesPart;
                 if (part != null)
                 {
                     pageTitle = (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? fi.FullName;
                 }
                 //if(part==null)
                 //{
                 if (originalCoreProps != null)
                 {
                     var packageProperties = wDoc.PackageProperties;
                     packageProperties.Creator = originalCoreProps.Creator;
                     packageProperties.Title = originalCoreProps.Title;
                     packageProperties.Subject = originalCoreProps.Subject;
                     packageProperties.Description = originalCoreProps.Description;
                     packageProperties.Created = originalCoreProps.Created;
                 }

                 //// Copy extended properties (app.xml) if they exist
                 //if (originalExtendedProps != null)
                 //{
                 //    var extendedPart = wDoc.AddExtendedFilePropertiesPart();
                 //    extendedPart.Properties = (DocumentFormat.OpenXml.ExtendedProperties.Properties)originalExtendedProps.Clone();
                 //}
                 //part = originalCoreFilePropertiesPart;
                 //}

                 WmlToHtmlConverterSettings settings = new WmlToHtmlConverterSettings()
                 {
                     AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }",
                     PageTitle = pageTitle,
                     FabricateCssClasses = true,
                     CssClassPrefix = "pt-",
                     RestrictToSupportedLanguages = false,
                     RestrictToSupportedNumberingFormats = false,
                     ImageHandler = imageInfo =>
                     {
                         DirectoryInfo localDirInfo = new DirectoryInfo(imageDirectoryName);
                         if (!localDirInfo.Exists)
                             localDirInfo.Create();
                         ++imageCounter;
                         string extension = imageInfo.ContentType.Split('/')[1].ToLower();
                         ImageFormat imageFormat = null;
                         if (extension == "png")
                             imageFormat = ImageFormat.Png;
                         else if (extension == "gif")
                             imageFormat = ImageFormat.Gif;
                         else if (extension == "bmp")
                             imageFormat = ImageFormat.Bmp;
                         else if (extension == "jpeg")
                             imageFormat = ImageFormat.Jpeg;
                         else if (extension == "tiff")
                         {
                             extension = "gif";
                             imageFormat = ImageFormat.Gif;
                         }
                         else if (extension == "x-wmf")
                         {
                             extension = "wmf";
                             imageFormat = ImageFormat.Wmf;
                         }

                         if (imageFormat == null)
                             return null;

                         string imageFileName = imageDirectoryName + "/image" +
                             imageCounter.ToString() + "." + extension;
                         try
                         {
                             imageInfo.Bitmap.Save(imageFileName, imageFormat);
                         }
                         catch (System.Runtime.InteropServices.ExternalException)
                         {
                             return null;
                         }
                         string imageSource = localDirInfo.Name + "/image" +
                             imageCounter.ToString() + "." + extension;

                         XElement img = new XElement(Xhtml.img,
                             new XAttribute(NoNamespace.src, imageSource),
                             imageInfo.ImgStyleAttribute,
                             imageInfo.AltText != null ?
                                 new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
                         return img;
                     }
                 };
                 XElement htmlElement = WmlToHtmlConverter.ConvertToHtml(wDoc, settings);
                 
                 return htmlElement;

                 //var html = new XDocument(
                 //    new XDocumentType("html", null, null, null),
                 //    htmlElement);
                 //var htmlString = html.ToString(SaveOptions.DisableFormatting);
                 //File.WriteAllText(destFileName.FullName, htmlString, Encoding.UTF8);
             }
         }
     }
 }
 private static XElement ExtractHeader(WordprocessingDocument wordDoc)
 {
     var headerParts = wordDoc.MainDocumentPart.HeaderParts.FirstOrDefault();
     if (headerParts == null)
     {
         return new XElement("header");
     }

     var headerXML = XElement.Parse(headerParts.RootElement.OuterXml);
     return headerXML;
 }

 private static XElement ExtractFooter(WordprocessingDocument wordDoc)
 {
     var footerParts = wordDoc.MainDocumentPart.FooterParts.FirstOrDefault();
     if (footerParts == null)
     {
         return new XElement("footer");
     }

     var footerXml = XElement.Parse(footerParts.RootElement.OuterXml);
     return footerXml;
 }

I tried extracting xml and putting in main body but temp docx does not hep it. Reason I want in above way is that I can get images and link and also stylings.

本文标签: htmlHeader and Footer in main BodyStack Overflow