說明
在Java生態中處理Office文檔時,開發人員常面臨格式兼容性和功能完整性的挑戰。商業組件Aspose以其卓越的文檔處理能力成為企業級解決方案之一,支持Word、Excel、PDF等多種格式的精準轉換與操作。
請勿用于商業用途,若侵權請聯系我。
參考了一些網上大神的破解文章,因為網上現存的基本上不是最新版的,本文采用了比較新一點的 24.12版本進行破解,可以支持使用幾年了。
HTML轉Word和PDF功能
除了基本的文檔處理外,Aspose Words還提供了強大的HTML轉Word和PDF功能,支持復雜的樣式保留和格式轉換。
核心功能特點:
- 完整保留HTML中的樣式和布局
- 支持自定義頁眉頁腳(可添加公司Logo)
- 自動優化中英文字體(中文默認微軟雅黑,英文Times New Roman)
- 表格自動調整和優化
- 列表樣式自動修正
- 圖片自適應處理
- 生成高質量的PDF文檔
使用步驟:
1. pom 文件引入依賴
<dependencies><dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>24.12</version><classifier>jdk17</classifier></dependency>
</dependencies><repositories><repository><id>AsposeJavaAPI</id><name>Aspose Java API</name><url>https://releases.aspose.com/java/repo/</url></repository>
</repositories>
2. HTML轉Word和PDF工具類
package com.gene.project.genereport.utils;import com.aspose.words.*;
import com.aspose.words.Font;
import com.aspose.words.Shape;
import com.gene.common.utils.StringUtils;import java.awt.*;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;public class HtmlToWordGenerator {/*** 將 HTML 文件轉換為 Word 和 PDF 文件,并返回文件路徑** @param htmlFilePath HTML 文件路徑* @param logoImagePath 頁眉圖片路徑(本地路徑)* @param outputFileName 輸出文件名(不帶擴展名)* @param folderPath 輸出文件夾路徑* @return 包含 docx 和 pdf 文件路徑的 Map* @throws Exception 異常處理*/public static Map<String, Object> exportHtmlToWordAndPdf(String htmlFilePath,String logoImagePath,String outputFileName,String folderPath) throws Exception {registerWord2412();// 設置 HTML 加載選項HtmlLoadOptions optionsHtml = new HtmlLoadOptions();optionsHtml.setEncoding(StandardCharsets.UTF_8);optionsHtml.setMswVersion(MsWordVersion.WORD_2019);// 加載 HTML 文件Document doc = new Document(htmlFilePath, optionsHtml);doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);// 設置紙張、頁眉頁腳等頁面格式for (Section section : doc.getSections()) {PageSetup pageSetup = section.getPageSetup();pageSetup.setPaperSize(PaperSize.A4);pageSetup.setOrientation(Orientation.PORTRAIT);pageSetup.setTopMargin(36);pageSetup.setBottomMargin(36);pageSetup.setLeftMargin(36);pageSetup.setRightMargin(36);pageSetup.setHeaderDistance(0);pageSetup.setFooterDistance(36.0);if (StringUtils.isNotEmpty(logoImagePath)) {// 設置頁眉HeaderFooter header = new HeaderFooter(doc, HeaderFooterType.HEADER_PRIMARY);Paragraph headerPara = new Paragraph(doc);headerPara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);InputStream imageStream = Files.newInputStream(new File(logoImagePath).toPath());double usableWidth = pageSetup.getPageWidth() - pageSetup.getLeftMargin() - pageSetup.getRightMargin();double fixedHeight = 30;Shape imageShape = new Shape(doc, ShapeType.IMAGE);imageShape.setAspectRatioLocked(false);imageShape.getImageData().setImage(imageStream);imageShape.setWrapType(WrapType.INLINE);imageShape.setWidth(usableWidth);imageShape.setHeight(fixedHeight);headerPara.appendChild(imageShape);header.appendChild(headerPara);section.getHeadersFooters().add(header);}// 設置頁腳HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);Paragraph footerPara = new Paragraph(doc);footerPara.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);footerPara.appendField("PAGE", String.valueOf(FieldType.FIELD_PAGE));footerPara.appendChild(new Run(doc, " / "));footerPara.appendField("NUMPAGES", String.valueOf(FieldType.FIELD_NUM_PAGES));footer.appendChild(footerPara);section.getHeadersFooters().add(footer);}// 白色背景doc.setPageColor(Color.WHITE);// 文字運行NodeCollection runs = doc.getChildNodes(NodeType.RUN, true);for (int i = 0; i < runs.getCount(); i++) {Run run = (Run) runs.get(i);String text = run.getText();Font font = run.getFont();double originalSize = font.getSize();if (originalSize > 0) {font.setSize(Math.max(originalSize * 0.8, 6));}if (text.matches("^[\\u4e00-\\u9fa5\\p{Punct}\\s]+$")) {font.setName("Microsoft YaHei");} else if (text.matches("^[A-Za-z0-9\\p{Punct}\\s]+$")) {font.setName("Times New Roman");} else {font.setName("Microsoft YaHei");font.setNameAscii("Times New Roman");font.setNameFarEast("Microsoft YaHei");font.setNameOther("Microsoft YaHei");}}// 表格樣式設置NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);for (int t = 0; t < tables.getCount(); t++) {Table table = (Table) tables.get(t);table.setAlignment(TableAlignment.CENTER);table.setPreferredWidth(PreferredWidth.fromPercent(95));table.setAllowAutoFit(false);for (Row row : table.getRows()) {row.getRowFormat().setHeightRule(HeightRule.AUTO);row.getRowFormat().setHeight(20);for (Cell cell : row.getCells()) {cell.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);cell.getCellFormat().setTopPadding(5);cell.getCellFormat().setBottomPadding(5);cell.getCellFormat().setLeftPadding(5);cell.getCellFormat().setRightPadding(5);for (Paragraph para : cell.getParagraphs()) {para.getParagraphFormat().setSpaceBefore(0);para.getParagraphFormat().setSpaceAfter(0);para.getParagraphFormat().setLineSpacing(12);}}}}// 段落處理NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);boolean firstHeading1Found = false;for (int i = 0; i < paragraphs.getCount(); i++) {Paragraph para = (Paragraph) paragraphs.get(i);String styleName = para.getParagraphFormat().getStyleName();ParagraphFormat paragraphFormat = para.getParagraphFormat();if ("Heading 1".equals(styleName)) {if (firstHeading1Found) {paragraphFormat.setPageBreakBefore(true);} else {firstHeading1Found = true;}}if ("Heading 2".equals(styleName)) {paragraphFormat.setLeftIndent(0);paragraphFormat.setRightIndent(0);}ListFormat listFormat = para.getListFormat();if (listFormat.isListItem()) {ListLevel listLevel = listFormat.getListLevel();String bullet = listLevel.getNumberFormat();if ("\uF0B7".equals(bullet)) {listLevel.getFont().setName("Microsoft YaHei");listLevel.getFont().setNameAscii("Microsoft YaHei");listLevel.getFont().setNameFarEast("Microsoft YaHei");listLevel.getFont().setNameOther("Microsoft YaHei");if (para.getRuns().getCount() > 0) {double fontSize = para.getRuns().get(0).getFont().getSize();listLevel.getFont().setSize(fontSize);}listLevel.setNumberFormat("?");} else if (".".equals(bullet) || "·".equals(bullet)) {listLevel.getFont().setName("Times New Roman");listLevel.getFont().setNameAscii("Times New Roman");listLevel.getFont().setNameFarEast("Times New Roman");listLevel.getFont().setNameOther("Times New Roman");if (para.getRuns().getCount() > 0) {double fontSize = para.getRuns().get(0).getFont().getSize();listLevel.getFont().setSize(fontSize);}}}}Map<String, Object> result = new HashMap<>();// 輸出文件路徑String uuid = UUID.randomUUID().toString();String docxPath = folderPath + File.separator + outputFileName + "_" + uuid + ".docx";String pdfPath = folderPath + File.separator + outputFileName + "_" + uuid + ".pdf";// 保存 Word 文件OoxmlSaveOptions wordOptions = new OoxmlSaveOptions(SaveFormat.DOCX);wordOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);wordOptions.setCompressionLevel(CompressionLevel.MAXIMUM);doc.save(docxPath, wordOptions);result.put("docxFilePath", docxPath);// 保存 PDF 文件PdfSaveOptions pdfOptions = new PdfSaveOptions();pdfOptions.setUseCoreFonts(false);pdfOptions.setUseHighQualityRendering(true);pdfOptions.setJpegQuality(100);pdfOptions.setImageCompression(0);pdfOptions.setCompliance(PdfCompliance.PDF_20);pdfOptions.setFontEmbeddingMode(PdfFontEmbeddingMode.EMBED_ALL);pdfOptions.setExportDocumentStructure(true);pdfOptions.setDmlRenderingMode(DmlRenderingMode.DRAWING_ML);pdfOptions.setDmlEffectsRenderingMode(DmlEffectsRenderingMode.FINE);pdfOptions.setEmbedFullFonts(true);doc.save(pdfPath, pdfOptions);result.put("pdfFilePath", pdfPath);return result;}/*** 核心破解方法*/public static void registerWord2412() {try {Class<?> zzodClass = Class.forName("com.aspose.words.zzod");Constructor<?> constructors = zzodClass.getDeclaredConstructors()[0];constructors.setAccessible(true);Object instance = constructors.newInstance(null, null);Field zzWws = zzodClass.getDeclaredField("zzWws");zzWws.setAccessible(true);zzWws.set(instance, 1);Field zzVZC = zzodClass.getDeclaredField("zzVZC");zzVZC.setAccessible(true);zzVZC.set(instance, 1);Class<?> zz83Class = Class.forName("com.aspose.words.zz83");constructors.setAccessible(true);constructors.newInstance(null, null);Field zzZY4 = zz83Class.getDeclaredField("zzZY4");zzZY4.setAccessible(true);ArrayList<Object> zzwPValue = new ArrayList<>();zzwPValue.add(instance);zzZY4.set(null, zzwPValue);Class<?> zzXuRClass = Class.forName("com.aspose.words.zzXuR");Field zzWE8 = zzXuRClass.getDeclaredField("zzWE8");zzWE8.setAccessible(true);zzWE8.set(null, 128);Field zzZKj = zzXuRClass.getDeclaredField("zzZKj");zzZKj.setAccessible(true);zzZKj.set(null, false);} catch (Exception e) {e.printStackTrace();}}
}
3. 使用示例
public class HtmlToWordTest {public static void main(String[] args) {try {String htmlPath = "input.html";String logoPath = "company_logo.png";String outputName = "Report";String outputFolder = "output";Map<String, Object> result = HtmlToWordGenerator.exportHtmlToWordAndPdf(htmlPath, logoPath, outputName, outputFolder);System.out.println("Word文件生成成功: " + result.get("docxFilePath"));System.out.println("PDF文件生成成功: " + result.get("pdfFilePath"));} catch (Exception e) {e.printStackTrace();}}
}
重要聲明
請勿用于商業用途,商業用途請購買官方正版,用于商業用途本人不承擔任何責任。
功能特點總結
- 格式保留:完整保留HTML中的樣式、布局和結構
- 字體優化:自動區分中英文應用不同字體
- 表格處理:自動調整表格寬度和樣式
- 列表修正:規范化列表符號和編號
- 頁眉頁腳:支持自定義頁眉頁腳和頁碼
- 高質量PDF:生成符合PDF 2.0標準的高質量文檔
- 批量處理:支持批量轉換多個HTML文件
該工具類特別適合需要將網頁內容或HTML報告轉換為正式Word/PDF文檔的場景,如報告生成、文檔歸檔等需求。