Guest User

Untitled

a guest
Dec 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. byte[] byteArray = File.ReadAllBytes("test.docx");
  2.  
  3. using (MemoryStream memoryStream = new MemoryStream())
  4. {
  5. memoryStream.Write(byteArray, 0, byteArray.Length);
  6. using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
  7. {
  8. int imageCounter = 0;
  9. HtmlConverterSettings settings = new HtmlConverterSettings()
  10. {
  11. AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }",
  12. PageTitle = "TEST",
  13. FabricateCssClasses = true,
  14. CssClassPrefix = "pt-",
  15. RestrictToSupportedLanguages = false,
  16. RestrictToSupportedNumberingFormats = false,
  17. ImageHandler = imageInfo =>
  18. {
  19. DirectoryInfo localDirInfo = new DirectoryInfo("img");
  20. if (!localDirInfo.Exists)
  21. localDirInfo.Create();
  22. ++imageCounter;
  23. string extension = imageInfo.ContentType.Split('/')[1].ToLower();
  24. ImageFormat imageFormat = null;
  25. if (extension == "png")
  26. {
  27. extension = "gif";
  28. imageFormat = ImageFormat.Gif;
  29. }
  30. else if (extension == "gif")
  31. imageFormat = ImageFormat.Gif;
  32. else if (extension == "bmp")
  33. imageFormat = ImageFormat.Bmp;
  34. else if (extension == "jpeg")
  35. imageFormat = ImageFormat.Jpeg;
  36. else if (extension == "tiff")
  37. {
  38. extension = "gif";
  39. imageFormat = ImageFormat.Gif;
  40. }
  41. else if (extension == "x-wmf")
  42. {
  43. extension = "wmf";
  44. imageFormat = ImageFormat.Wmf;
  45. }
  46. if (imageFormat == null)
  47. return null;
  48.  
  49. string imageFileName = "img/image" +
  50. imageCounter.ToString() + "." + extension;
  51. try
  52. {
  53. imageInfo.Bitmap.Save(imageFileName, imageFormat);
  54. }
  55. catch (System.Runtime.InteropServices.ExternalException)
  56. {
  57. return null;
  58. }
  59. XElement img = new XElement(Xhtml.img,
  60. new XAttribute(NoNamespace.src, imageFileName),
  61. imageInfo.ImgStyleAttribute,
  62. imageInfo.AltText != null ?
  63. new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
  64. return img;
  65. }
  66. };
  67. XElement html = HtmlConverter.ConvertToHtml(doc, settings);
  68. File.WriteAllText("test.html", html.ToStringNewLineOnAttributes());
  69. };
  70. }
Add Comment
Please, Sign In to add comment