WordHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using DocumentFormat.OpenXml.Packaging;
  2. using DocumentFormat.OpenXml.Wordprocessing;
  3. using Hotline.Share.Dtos.ExportWord;
  4. using Hotline.Share.Tools;
  5. using HtmlToOpenXml;
  6. using OpenHtmlToPdf;
  7. namespace Hotline.Application.ExportWord
  8. {
  9. public static class WordHelper
  10. {
  11. public static Stream ConvertHtmlToPdf(string htmlContent)
  12. {
  13. var pdf = OpenHtmlToPdf.Pdf
  14. .From(htmlContent)
  15. .WithObjectSetting("web.defaultEncoding", "utf-8")
  16. .Content();
  17. var stream = new MemoryStream(pdf)
  18. {
  19. Position = 0
  20. };
  21. return stream;
  22. }
  23. public static Stream ConvertHtmlToWord(string htmlContent)
  24. {
  25. var memoryStream = new MemoryStream();
  26. using (var wordDoc = WordprocessingDocument.Create(memoryStream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
  27. {
  28. MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
  29. mainPart.Document = new Document(new Body());
  30. HtmlConverter converter = new HtmlConverter(mainPart);
  31. var validator = new HtmlImageValidator();
  32. string validatedHtml = validator.ValidateAndReplaceImagesAsync(htmlContent);
  33. try
  34. {
  35. var paragraphs = converter.Parse(validatedHtml);
  36. foreach (var paragraph in paragraphs)
  37. {
  38. mainPart.Document.Body.Append(paragraph);
  39. }
  40. mainPart.Document.Save();
  41. }
  42. catch (Exception e)
  43. {
  44. var msg = e.Message;
  45. // ignore
  46. }
  47. }
  48. memoryStream.Position = 0;
  49. return memoryStream;
  50. }
  51. public static byte[] WordByte<T>(string templatePath, T data) where T : IWordExportTemplate
  52. {
  53. return new WordExportService(new WordExportProvider()).TemplateCreateWord(templatePath, data).WordBytes;
  54. }
  55. public static Stream WordStream<T>(string templatePath, T data) where T : IWordExportTemplate
  56. {
  57. var bytes = new WordExportService(new WordExportProvider()).TemplateCreateWord(templatePath, data).WordBytes;
  58. Stream stream = new MemoryStream(bytes);
  59. return stream;
  60. }
  61. public static bool WordDocX<T>(string templatePath, T data, string savePath) where T : IWordExportTemplate
  62. {
  63. System.IO.File.WriteAllBytes(ExistDir(savePath), WordByte(templatePath, data));
  64. return true;
  65. }
  66. private static string ExistDir(string path)
  67. {
  68. string text = path.Replace("\\", "/");
  69. if (text.IndexOf("/") > 0)
  70. {
  71. string path2 = text.Substring(0, text.LastIndexOf("/"));
  72. if (!Directory.Exists(path2))
  73. {
  74. Directory.CreateDirectory(path2);
  75. }
  76. }
  77. return text;
  78. }
  79. /// <summary>
  80. /// ZipStream 压缩
  81. /// </summary>
  82. /// <param name="streams">Dictionary(string, Stream) 文件名和Stream</param>
  83. /// <returns></returns>
  84. public static byte[] ConvertZipStream(Dictionary<string, Stream> streams)
  85. {
  86. byte[] buffer = new byte[6500];
  87. MemoryStream returnStream = new();
  88. var zipMs = new MemoryStream();
  89. using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(zipMs))
  90. {
  91. zipStream.SetLevel(9);//设置 压缩等级 (9级 500KB 压缩成了96KB)
  92. foreach (var kv in streams)
  93. {
  94. string fileName = kv.Key;
  95. using (var streamInput = kv.Value)
  96. {
  97. ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
  98. zipEntry.IsUnicodeText = true;
  99. zipStream.PutNextEntry(zipEntry);
  100. while (true)
  101. {
  102. var readCount = streamInput.Read(buffer, 0, buffer.Length);
  103. if (readCount > 0)
  104. {
  105. zipStream.Write(buffer, 0, readCount);
  106. }
  107. else
  108. {
  109. break;
  110. }
  111. }
  112. zipStream.Flush();
  113. }
  114. }
  115. zipStream.Finish();
  116. zipMs.Position = 0;
  117. zipMs.CopyTo(returnStream, 5600);
  118. }
  119. returnStream.Position = 0;
  120. //Stream转Byte[]
  121. byte[] returnBytes = new byte[returnStream.Length];
  122. returnStream.Read(returnBytes, 0, returnBytes.Length);
  123. returnStream.Seek(0, SeekOrigin.Begin);
  124. return returnBytes;
  125. }
  126. }
  127. }