当前位置: 首页 > news >正文

doc转html后添加style和导航

public static void main(String[] args) throws Exception {docxToHtml();
}
public static void docxToHtml() throws Exception {//D:\zpdtolly\工作总结文档\zpd使用文档\v4\用户使用手册\客户端使用手册String sourceFileName = "C:\\Users\\luoguoqing\\Desktop\\20230920客户端v使用手册.doc";String targetFileName = "C:\\Users\\luoguoqing\\Desktop\\20230920客户端v使用手册.html";//下载图片位置String imagePathStr = "D:/doc2htmltest/image/";String style = "body{background:#f0eeee;text-align:center;}\n" +"div{width:85%;margin:0 auto;background:#ecdfdf;text-align:left;}\n" +"* {\n" +"  padding: 0;\n" +"  margin: 0;\n" +"  box-sizing: border-box;\n" +"}\n" +".aside {\n" +"  width: 240px;\n" +"  height: 100%;\n" +"  position: fixed;\n" +"  left: -240px;\n" +"  top: 0px;\n" +"  border-right: 1px solid #ccc;\n" +"  -ms-transition: all 0.3s linear;\n" +"  -moz-transition: all 0.3s linear;\n" +"  -webkit-transition: all 0.3s linear;\n" +"  transition: all 0.3s linear;\n" +"}\n" +".aside:hover{\n" +"  left: 0;\n" +"}\n" +".aside:hover + .article{\n" +"  padding-left: 260px;\n" +"}\n" +".nav-list{\n" +"  width: 100%;\n" +"  height: 100%;\n" +"  overflow: auto;\n" +"  padding: 10px 0px;\n" +"}\n" +".nav-mark{\n" +"  position: absolute;\n" +"  right: -20px;\n" +"  top: 50%;\n" +"  z-index: 2;\n" +"  height: 80px;\n" +"  width: 20px;\n" +"  margin-top: -40px;\n" +"  background-color: #44a7ff;\n" +"  box-shadow: 2px 0px 3px #eee;\n" +"  border-radius: 0 40px 40px 0;\n" +"  font-size: 12px;\n" +"  text-align: center;\n" +"  line-height: 24px;\n" +"  padding-top: 16px;\n" +"  color: #fff;\n" +"}\n" +".nav {\n" +"  display: block;\n" +"  width: 100%;\n" +"  height: 32px;\n" +"  line-height: 32px;\n" +"  font-size: 16px;\n" +"  color: #333;\n" +"  text-decoration: none;\n" +"  padding-left: 20px;\n" +"}\n" +".nav:hover {\n" +"  background-color: #44a7ff;\n" +"  color: #fff;\n" +"}\n" +".grade2 {\n" +"  text-indent: 1em;\n" +"}\n" +".grade3 {\n" +"  text-indent: 2em;\n" +"}";HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(sourceFileName));WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());wordToHtmlConverter.setPicturesManager((a, b, suggestedName, d, e) -> {// convertFileToBase64()out.println(suggestedName);//返回图片路径//return "image" + File.separator + suggestedName;//返回图片base64值return "data:image/"+b.getExtension().toLowerCase()+";base64,"+new String(Base64.encodeBase64(a));});wordToHtmlConverter.processDocument(wordDocument);List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();out.println(pics);Iterator var8 = pics.iterator();//转换图片imgToBase64 imgToBase64pl=new imgToBase64();int i=1;while(var8.hasNext()) {Picture pic = (Picture)var8.next();//下载图片// pic.writeImageContent(new FileOutputStream(imagePathStr + pic.suggestFullFileName()));//System.out.println("第"+i++ +":"+imgToBase64pl.convertFileToBase64(imagePathStr + pic.suggestFullFileName()));}Document htmlDocument = wordToHtmlConverter.getDocument();//生成空文档DOMSource domSource = new DOMSource(htmlDocument);//获取路径下htmlStreamResult streamResult = new StreamResult(new File(targetFileName));//设置html文件规范TransformerFactory tf = TransformerFactory.newInstance();Transformer serializer = tf.newTransformer();serializer.setOutputProperty("encoding", "utf-8");serializer.setOutputProperty("indent", "yes");serializer.setOutputProperty("method", "html");serializer.transform(domSource, streamResult);out.println("doc转换完毕!"+streamResult.getSystemId());addStyleToHtml a=new addStyleToHtml();a.addstyle(targetFileName,style);}

package com.lly.demo.util;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class addStyleToHtml {public void addstyle(String targetFileName, String style) throws IOException {File htmlFile = new File(targetFileName);Document doc = Jsoup.parse(htmlFile, "UTF-8");Element head = doc.head();head.append("<style>" + style + "</style>");Element spanA = doc.select("span:contains(TOC)").first(); // 获取第一个 span 元素Element spanB = doc.select("span:contains(第1章 前言)").last().parent().parent(); // 获取最后一个 span 元素// 创建新的<aside>元素Element aside = doc.createElement("aside");aside.addClass("aside");// 创建包含目录的<div>元素Element navList = doc.createElement("div");navList.addClass("nav-list");// 创建导航元素Element navMark = doc.createElement("div");navMark.addClass("nav-mark").text("导航");Element currentElement = spanA;while (currentElement != null) {System.out.println(currentElement.outerHtml());if (spanA != null && spanB != null) {navList.appendChild(currentElement);// 将<div>元素和导航元素添加到<aside>元素中aside.appendChild(navList);aside.appendChild(navMark);// 在目录后面的元素之前插入<aside>元素spanB.before(aside);} else {System.out.println("空的");}if (currentElement == spanB) {break;}currentElement = currentElement.nextElementSibling();}Element span = doc.select("span:contains(TOC)").first();//span.remove(); // 删除span元素// 输出更新后的HTML//System.out.println(doc.html());FileWriter writer = new FileWriter(htmlFile);writer.write(doc.outerHtml());writer.close();}}
http://www.lryc.cn/news/186831.html

相关文章:

  • Python中跨越多个文件使用全局变量
  • 设计模式 - 解释器模式
  • javascript禁止鼠标右键和复制功能
  • WebDAV之π-Disk派盘 + 咕咚云图
  • C语言-数组
  • Linux UWB Stack实现——MCPS调度接口(API)
  • el-tree中插入图标并且带提示信息
  • 竞赛选题 深度学习 YOLO 实现车牌识别算法
  • Direct3D网格(一)
  • C语言打印菱形
  • ElasticSearch搜索引擎:数据的写入流程
  • python3 调用 另外一个python脚本
  • 【13】c++设计模式——>简单工厂模式
  • 系统架构设计:2 论软件设计方法及其应用
  • 基于Winform的UDP通信
  • 掌握 BERT:自然语言处理 (NLP) 从初级到高级的综合指南(1)
  • Linux Ftrace介绍
  • Go语言进阶------>init()函数
  • 云计算:常用微服务框架
  • jmeter添加断言(详细图解)
  • few shot object detection via feature reweight笔记
  • 工会排队模式:电商新营销模式吸引消费者,提升销售!
  • 定档通知2024中国(北京)国际红外技术及设备展览会
  • 自助建站系统,一建建站系统api版,自动建站
  • 算法框架-LLM-1-Prompt设计(一)
  • 一个rar压缩包如何分成三个?
  • 批量获取拼多多商品详情数据,拼多多商品详情API接口
  • Redis Cluster Gossip Protocol: 目录
  • HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Span
  • 这些负载均衡都解决哪些问题?服务、网关、NGINX