破解 Aspose.Words 24.12,跳過 License 校驗,實現 HTML 向 Word/PDF 的轉換,附帶 Demo。

說明

在Java生態中處理Office文檔時,開發人員常面臨格式兼容性和功能完整性的挑戰。商業組件Aspose以其卓越的文檔處理能力成為企業級解決方案之一,支持Word、Excel、PDF等多種格式的精準轉換與操作。

請勿用于商業用途,若侵權請聯系我。

參考了一些網上大神的破解文章,因為網上現存的基本上不是最新版的,本文采用了比較新一點的 24.12版本進行破解,可以支持使用幾年了。

HTML轉Word和PDF功能

除了基本的文檔處理外,Aspose Words還提供了強大的HTML轉Word和PDF功能,支持復雜的樣式保留和格式轉換。

核心功能特點:

  1. 完整保留HTML中的樣式和布局
  2. 支持自定義頁眉頁腳(可添加公司Logo)
  3. 自動優化中英文字體(中文默認微軟雅黑,英文Times New Roman)
  4. 表格自動調整和優化
  5. 列表樣式自動修正
  6. 圖片自適應處理
  7. 生成高質量的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();}}
}

重要聲明

請勿用于商業用途,商業用途請購買官方正版,用于商業用途本人不承擔任何責任。

功能特點總結

  1. 格式保留:完整保留HTML中的樣式、布局和結構
  2. 字體優化:自動區分中英文應用不同字體
  3. 表格處理:自動調整表格寬度和樣式
  4. 列表修正:規范化列表符號和編號
  5. 頁眉頁腳:支持自定義頁眉頁腳和頁碼
  6. 高質量PDF:生成符合PDF 2.0標準的高質量文檔
  7. 批量處理:支持批量轉換多個HTML文件

該工具類特別適合需要將網頁內容或HTML報告轉換為正式Word/PDF文檔的場景,如報告生成、文檔歸檔等需求。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/97512.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/97512.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/97512.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

php連接rabbitmq例子

首先確保安裝好了Rabbitmq服務器。1.新建一個空白php項目&#xff0c;安裝php客戶端庫&#xff1a;composer require php-amqplib/php-amqplib2.生產者然后添加生產者代碼 (producer.php)<?php require_once __DIR__ . /vendor/autoload.php;use PhpAmqpLib\Connection\AMQ…

Docker Swarm vs Kubernetes vs Nomad:容器編排方案對比與選型建議

Docker Swarm vs Kubernetes vs Nomad&#xff1a;容器編排方案對比與選型建議 在微服務和云原生時代&#xff0c;容器編排成為支持大規模容器化應用的關鍵技術。本文將從問題背景、方案對比、優缺點分析、選型建議以及實際應用效果驗證五個方面&#xff0c;對Docker Swarm、Ku…

似然函數對數似然函數負對數似然函數

目錄1. 似然函數的定義2. 對數似然函數的定義3. 負對數似然函數的定義4. 負對數似然函數的優化5. 具體應用示例5.1 邏輯回歸中的負對數似然函數5.2 優化邏輯回歸的負對數似然函數1. 似然函數的定義 似然函數L(θ∣X)L(\theta | X)L(θ∣X)是在給定參數θ\thetaθ 下&#xff0…

鴻蒙地址選擇庫(ArkTs UI)

功能點&#xff1a;支持三級聯動、點擊確認返回省市區code及name&#xff08;安心&#xff09;、布局可以高度自定義 實現&#xff1a;TextPicker讀取本地json&#xff08;也可用第三方的json 不過需要自行調整了&#xff09; 先上圖吧、廢話下面再說&#xff1a; 湊和看吧、…

YOLO 目標檢測:數據集構建(LabelImg 實操)、評估指標(mAP/IOU)、 NMS 后處理

文章目錄基本知識介紹1.視覺處理三大任務2.訓練、驗證、測試、推理3.數據集3.1 數據集格式3.2 數據集標注4.上游任務和下游任務YOLO指標1.真實框&#xff08;Ground Truth Box&#xff09;與邊界框&#xff08;Bounding Box&#xff09;2.交并比&#xff08;IOU&#xff09;3.置…

進程狀態 —— Linux內核(Kernel)

&#x1f381;個人主頁&#xff1a;工藤新一 &#x1f50d;系列專欄&#xff1a;C面向對象&#xff08;類和對象篇&#xff09; &#x1f31f;心中的天空之城&#xff0c;終會照亮我前方的路 &#x1f389;歡迎大家點贊&#x1f44d;評論&#x1f4dd;收藏?文章 文章目錄進…

計算機視覺與深度學習 | 低照度圖像處理算法綜述:發展、技術與趨勢

文章目錄 一、發展歷程:從傳統模型到智能融合 (一)傳統模型構建階段(1970s-2016) (二)深度學習應用階段(2017-2020) (三)硬件-算法協同階段(2021至今) 二、技術分類與性能對比 (一)傳統方法體系 (二)深度學習方法 1. 監督學習模型 2. 無監督/自監督方法 3. 混…

責任鏈模式實踐-開放銀行數據保護及合規

責任鏈模式介紹什么是責任鏈模責任鏈模式是一種行為設計模式&#xff0c; 允許你將請求沿著處理者鏈進行發送。 收到請求后&#xff0c; 每個處理者均可對請求進行處理&#xff0c; 或將其傳遞給鏈上的下個處理者。責任鏈模式結構偽代碼基于責任鏈的開放銀行數據保護及合規實踐…

npm install --global @dcloudio/uni-cli 時安裝失敗

這個日志顯示在執行 npm install --global dcloudio/uni-cli 時安裝失敗&#xff0c;核心錯誤是 UNABLE_TO_GET_GET_ISSUER_CERT_LOCALLY&#xff08;無法獲取本地頒發者證書&#xff09;&#xff0c;屬于 HTTPS 證書驗證失敗 問題。錯誤原因npm 訪問官方 registry&#xff08;…

吱吱企業通訊軟件可私有化部署,構建安全可控的通訊辦公平臺

在當今激烈的市場競爭環境中&#xff0c;企業通訊已成為制勝的關鍵因素。吱吱作為一款專為企業管理設計的IM即時辦公通訊軟件&#xff0c;提供了高度安全的通訊辦公環境&#xff0c;確保信息在內部流通的安全性與高效性&#xff0c;為企業數字化轉型奠定了堅實的基礎。 一、私有…

暄桐:唯有認真思考過死亡,才足以應對日常

暄桐是一間傳統美學教育教室&#xff0c;創辦于2011年&#xff0c;林曦是創辦人和授課老師&#xff0c;教授以書法為主的傳統文化和技藝&#xff0c;皆在以書法為起點&#xff0c;親近中國傳統之美&#xff0c;以實踐和所得&#xff0c;滋養當下生活。初聽莊子在妻子離世后“鼓…

目標檢測領域基本概念

基于提議的方法&#xff0c;也常被稱為兩階段 (Two-stage) 方法&#xff0c;是目標檢測領域的經典范式。它們將目標檢測任務分解為兩個主要步驟&#xff1a;階段一&#xff1a;區域提議 (Region Proposal Generation) 目標&#xff1a; 在圖像中生成一系列可能包含物體的候選區…

【開題答辯全過程】以 基于SpringBoot的流浪貓狗領養系統為例,包含答辯的問題和答案

個人簡介一名14年經驗的資深畢設內行人&#xff0c;語言擅長Java、php、微信小程序、Python、Golang、安卓Android等開發項目包括大數據、深度學習、網站、小程序、安卓、算法。平常會做一些項目定制化開發、代碼講解、答辯教學、文檔編寫、也懂一些降重方面的技巧。感謝大家的…

扣子(coze)實踐指南進階篇——創建工作流,并將工作流接入智能體

大家好&#xff0c;歡迎閱讀這份《智能體&#xff08;AIAgent&#xff09;開發指南》&#xff01; 在大模型和智能體快速發展的今天&#xff0c;很多朋友希望學習如何從零開始搭建一個屬于自己的智能體。本教程的特點是 完全基于國產大模型與火山推理引擎實現&#xff0c;不用翻…

【STM32】外部中斷(上)

【STM32】外部中斷前言一、中斷系統1.1 什么是中斷1.2 中斷優先級1.3 中斷嵌套1.4 中斷執行流程二、NVIC2.1NVIC基本結構2.2 NVIC優先級分組三、EXTI3.1 EXTI 外部中斷&#xff08;Extern Interrupt&#xff09;3.2 EXTI基本結構3.3 AFIO復用IO口3.4 EXTI內部框圖前言 【STM32…

TimeDP Learning to Generate Multi-Domain Time Series with Domain Prompts論文閱讀筆記

TimeDP Learning to Generate Multi-Domain Time Series with Domain Prompts 摘要 在跨域時序數據生成任務中&#xff0c;提出使用”時間序列語義原型“模塊定義時間序列原型來表示時間序列基&#xff0c;每個原型向量作為“詞”表示一些基本的時間序列特征。應用原型分配模塊…

Ubuntu安裝NVIDIA顯卡驅動

清理舊驅動 sudo apt purge nvidia* libnvidia* sudo apt autoremovesudo find /etc -name *nvidia* -exec sudo rm -rf {} sudo rm -rf /usr/local/cuda*禁用 nouveau echo blacklist nouveau options nouveau modeset0 | sudo tee /etc/modprobe.d/blacklist-nouveau.conf…

硬件工程師成長之路:從入門到精通的技術旅程

文章目錄前言第一階段&#xff1a;基礎知識的積累理論知識儲備動手實踐第二階段&#xff1a;專業技能的提升PCB設計嵌入式系統開發第三階段&#xff1a;專業方向的選擇射頻&#xff08;RF&#xff09;工程電源設計高速數字電路FPGA/ASIC設計第四階段&#xff1a;工程管理與視野…

PyTorch 張量(Tensor)詳解:從基礎到實戰

1. 引言在深度學習和科學計算領域&#xff0c;張量&#xff08;Tensor&#xff09; 是最基礎的數據結構。PyTorch 作為當前最流行的深度學習框架之一&#xff0c;其核心計算單元就是張量。與 NumPy 的 ndarray 類似&#xff0c;PyTorch 張量支持高效的數值計算&#xff0c;但額…

CPTS---Hospital

端口掃描 nmap -A -p- -n -Pn -T4 10.10.11.241 22/tcp open ssh OpenSSH 9.0p1 Ubuntu 1ubuntu8.5 (Ubuntu Linux; protocol 2.0) | ssh-hostkey: | 256 e1:4b:4b:3a:6d:18:66:69:39:f7:aa:74:b3:16:0a:aa (ECDSA) |_ 256 96:c1:dc:d8:97:20:95:e7:01:5…