java word书签_java替换word文档书签并转换为pdf

这篇博客介绍了一种使用Java处理Word文档中书签的方法,包括替换书签内容和将处理后的文档转换为PDF。通过Apache POI库和相关转换工具,实现了对Word文档的读取、修改及保存,并提供了详细的代码示例。
摘要由CSDN通过智能技术生成

首先使用maven下载必要的jar文件,而不是手动。 pom.xml文件看起来像:

junit

junit

3.8.1

test

fr.opensagres.xdocreport

org.apache.poi.xwpf.converter.core

1.0.5

fr.opensagres.xdocreport

org.apache.poi.xwpf.converter.pdf

1.0.5

fr.opensagres.xdocreport

org.apache.poi.xwpf.converter.xhtml

1.0.5

与以下jar文件将被下载:

e1DJU.png

而且我也写了一些Java代码,希望能派上用场:

package com.company;

import com.lowagie.text.pdf.BaseFont;

import org.apache.poi.xwpf.converter.pdf.PdfConverter;

import org.apache.poi.xwpf.converter.pdf.PdfOptions;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;

import org.w3c.dom.Node;

import java.io.*;

import java.util.Iterator;

import java.util.List;

public class Main {

public static class DOCXTest {

public XWPFDocument document = null;

public DOCXTest() {

}

public final void openFile(String filename) throws IOException {

File file = null;

FileInputStream fis = null;

try {

file = new File(filename);

fis = new FileInputStream(file);

this.document = new XWPFDocument(fis);

}

finally {

try {

if(fis != null) {

fis.close();

fis = null;

}

}

catch(IOException ioEx) {

}

}

}

public final void saveAs(String filename) throws IOException {

File file = null;

FileOutputStream fos = null;

try {

file = new File(filename);

fos = new FileOutputStream(file);

this.document.write(fos);

}

finally {

if(fos != null) {

fos.close();

fos = null;

}

}

}

private final void procParaList(List paraList,

String bookmarkName, String bookmarkValue) {

Iterator paraIter = null;

XWPFParagraph para = null;

List bookmarkList = null;

Iterator bookmarkIter = null;

CTBookmark bookmark = null;

XWPFRun run = null;

Node nextNode = null;

paraIter = paraList.iterator();

while(paraIter.hasNext()) {

para = paraIter.next();

bookmarkList = para.getCTP().getBookmarkStartList();

bookmarkIter = bookmarkList.iterator();

while(bookmarkIter.hasNext()) {

bookmark = bookmarkIter.next();

if(bookmark.getName().equals(bookmarkName)) {

run = para.createRun();

run.setText(bookmarkValue);

nextNode = bookmark.getDomNode().getNextSibling();

while(!(nextNode.getNodeName().contains("bookmarkEnd"))) {

para.getCTP().getDomNode().removeChild(nextNode);

nextNode = bookmark.getDomNode().getNextSibling();

}

para.getCTP().getDomNode().insertBefore(

run.getCTR().getDomNode(),

nextNode);

}

}

}

}

public final void insertAtBookmark(String bookmarkName, String bookmarkValue) {

List tableList = null;

Iterator tableIter = null;

List rowList = null;

Iterator rowIter = null;

List cellList = null;

Iterator cellIter = null;

XWPFTable table = null;

XWPFTableRow row = null;

XWPFTableCell cell = null;

this.procParaList(this.document.ge

tableList = this.document.getTables();

tableIter = tableList.iterator();

while(tableIter.hasNext()) {

table = tableIter.next();

rowList = table.getRows();

rowIter = rowList.iterator();

while(rowIter.hasNext()) {

row = rowIter.next();

cellList = row.getTableCells();

cellIter = cellList.iterator();

while(cellIter.hasNext()) {

cell = cellIter.next();

this.procParaList(cell.getParagraphs(),

bookmarkName,

bookmarkValue);

}

}

}

}

}

public static boolean LogEnabled = true;

public static void main(String[] args) {

AddLog("Start");

try {

DOCXTest docxTest = new DOCXTest();

docxTest.openFile("D:/template.docx");

docxTest.insertAtBookmark("FIO", "Ibadov Kamil Ələsgər");

docxTest.saveAs("D:/replaced.docx");

File outFile = new File("D:/replaced.pdf");

outFile.getParentFile().mkdirs();

OutputStream out = new FileOutputStream(outFile);

PdfOptions options = PdfOptions.create().fontEncoding(BaseFont.IDENTITY_H);

PdfConverter.getInstance().convert(docxTest.document, out, options);

AddLog("End");

} catch (Exception e) {

AddLog(e.getMessage());

}

}

public static void AddLog(String LogMessage) {

if (LogEnabled) {

try {

System.out.println(LogMessage);

BufferedWriter out = new BufferedWriter(new FileWriter("logs.txt"));

out.write(LogMessage);

out.close();

}

catch (IOException e)

{

System.out.println("Exception ");

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值