1
나는 아파치 poi를 사용하여 낱말 문서 책갈피를 교환하고 pdf로 개조하는 것을 시도했다. 외견 상으로는 작동해야하지만 서로 연결되는 많은 오류가 있습니다. 나는 하나를 해결하고 다음은 다음에 나타납니다. 내가 뭘 잘못 했니? 내가 가지고 있어야하는 도서관은 무엇입니까?자바 워드 문서 책갈피를 대체하고 pdf로
나는 아파치 poi를 사용하여 낱말 문서 책갈피를 교환하고 pdf로 개조하는 것을 시도했다. 외견 상으로는 작동해야하지만 서로 연결되는 많은 오류가 있습니다. 나는 하나를 해결하고 다음은 다음에 나타납니다. 내가 뭘 잘못 했니? 내가 가지고 있어야하는 도서관은 무엇입니까?자바 워드 문서 책갈피를 대체하고 pdf로
먼저 수동으로가 아니라 maven을 사용하여 필요한 jar 파일을 다운로드하십시오.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
그리고 jar 파일을 다음 다운로드됩니다 : pom.xml
파일과 같은
을 그리고 나 또한 몇 가지 자바 코드를 작성, 희망이 편리합니다
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<XWPFParagraph> paraList,
String bookmarkName, String bookmarkValue) {
Iterator<XWPFParagraph> paraIter = null;
XWPFParagraph para = null;
List<CTBookmark> bookmarkList = null;
Iterator<CTBookmark> 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<XWPFTable> tableList = null;
Iterator<XWPFTable> tableIter = null;
List<XWPFTableRow> rowList = null;
Iterator<XWPFTableRow> rowIter = null;
List<XWPFTableCell> cellList = null;
Iterator<XWPFTableCell> 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 ");
}
}
}
}