html输出doc样式,Word格式处理控件Spire.Doc功能演示:在Java中将两种HTML样式的文档保存为PDF...

Word格式处理控件Spire.Doc功能演示:在Java中将两种HTML样式的文档保存为PDF

Spire.Doc for Java是一款专业的Java Word组件,开发人员使用它可以轻松地将Word文档创建、读取、编辑、转换和打印等功能集成到自己的Java应用程序中。

本文将介绍如何使用Spire.Doc for Java将HTML String和HTML file两种HTML样式的文档保存为PDF格式。可点击此处下载最新版测试。

*这么优秀的国产工具怎能错过呢!在线下单专享“一口价”,查看优惠价格!想要获取更多福利的朋友可以咨询在线客服哦~

HTML String另存为PDF格式

import com.spire.doc.*;

import java.io.*;

public class htmlStringToWord {

public static void main(String[] args) throws Exception {

String inputHtml = "InputHtml.txt";

//新建Document对象

Document document = new Document();

//添加section

Section sec = document.addSection();

String htmlText = readTextFromFile(inputHtml);

//添加段落并写入HTML文本

sec.addParagraph().appendHTML(htmlText);

//文档另存为PDF

document.saveToFile("HTMLstringToPDF.pdf", FileFormat.PDF);

}

public static String readTextFromFile(String fileName) throws IOException{

StringBuffer sb = new StringBuffer();

BufferedReader br = new BufferedReader(new FileReader(fileName));

String content = null;

while ((content = br.readLine()) != null) {

sb.append(content);

}

return sb.toString();

}

}

72f62fa3321de047ec2a47bb28d04fb8.png

HTML file另存为PDF格式

import com.spire.doc.*;

import com.spire.doc.documents.XHTMLValidationType;

public class htmlFileToWord {

public static void main(String[] args) throws Exception {

//加载HTML文档

Document document = new Document();

document.loadFromFile("InputHtmlFile.html", FileFormat.Html, XHTMLValidationType.None);

//文档另存为PDF

document.saveToFile("Result.pdf",FileFormat.PDF);

}

}

1951f0363432565b1338eb564d962726.png

还想要更多教程资源吗?您可以点击阅读【2020 · E-iceblue最新资源整合】,查找需要的教程资源。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用-iceblue:spire.doc.free:5.2.0获取指定内容在doc文档的第几页,可以按照以下步骤进行操作: 1. 打开需要处理doc文件 ```java Document document = new Document(); document.loadFromFile("test.doc"); ``` 2. 获取doc文件中的所有段落 ```java SectionCollection sections = document.getSections(); ParagraphCollection paragraphs = sections.get(0).getParagraphs(); ``` 3. 遍历所有段落,查找需要查找的内容所在的段落,记录下该段落的索引 ```java int pageIndex = -1; for (int i = 0; i < paragraphs.getCount(); i++) { Paragraph paragraph = paragraphs.get(i); String text = paragraph.getText(); if (text.contains("需要查找的内容")) { pageIndex = i; break; } } ``` 4. 如果找到了需要查找的内容所在的段落,则计算该段落所在的页面数 ```java if (pageIndex != -1) { DocumentObject obj = paragraphs.get(pageIndex); int page = document.getPageNumber(obj); System.out.println("需要查找的内容所在的页数为:" + page); } ``` 完整的代码如下: ```java import com.spire.doc.*; public class GetPageIndex { public static void main(String[] args) { //加载文档 Document document = new Document(); document.loadFromFile("test.doc"); //获取第一个节的所有段落 SectionCollection sections = document.getSections(); ParagraphCollection paragraphs = sections.get(0).getParagraphs(); //查找内容所在的段落 int pageIndex = -1; for (int i = 0; i < paragraphs.getCount(); i++) { Paragraph paragraph = paragraphs.get(i); String text = paragraph.getText(); if (text.contains("需要查找的内容")) { pageIndex = i; break; } } //计算内容所在的页数 if (pageIndex != -1) { DocumentObject obj = paragraphs.get(pageIndex); int page = document.getPageNumber(obj); System.out.println("需要查找的内容所在的页数为:" + page); } } } ``` 注意:以上代码使用的是Spire.Doc Free版本,如果使用的是Spire.Doc付费版本,则需要引入对应的jar包,并且需要授权才能使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值