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

sey*_*tan 1 java itext apache-poi

我尝试使用apache poi替换word文档书签并将其转换为pdf。它看起来必须有效,但我有很多相互链接的错误。我解决了一个,然后出现下一个。我做错了什么?我必须拥有哪些库?

Kam*_*dov 5

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

\n\n
<dependencies>\n    <dependency>\n      <groupId>junit</groupId>\n      <artifactId>junit</artifactId>\n      <version>3.8.1</version>\n      <scope>test</scope>\n    </dependency>\n    <dependency>\n      <groupId>fr.opensagres.xdocreport</groupId>\n      <artifactId>org.apache.poi.xwpf.converter.core</artifactId>\n      <version>1.0.5</version>\n    </dependency>\n\n    <dependency>\n      <groupId>fr.opensagres.xdocreport</groupId>\n      <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>\n      <version>1.0.5</version>\n    </dependency>\n\n    <dependency>\n      <groupId>fr.opensagres.xdocreport</groupId>\n      <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>\n      <version>1.0.5</version>\n    </dependency>\n  </dependencies>\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且将下载以下 jar 文件:

\n\n

在此输入图像描述

\n\n

我还写了一些java代码,希望能派上用场:

\n\n
package com.company;\n\n\nimport com.lowagie.text.pdf.BaseFont;\nimport org.apache.poi.xwpf.converter.pdf.PdfConverter;\nimport org.apache.poi.xwpf.converter.pdf.PdfOptions;\nimport org.apache.poi.xwpf.usermodel.*;\nimport org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;\nimport org.w3c.dom.Node;\n\nimport java.io.*;\nimport java.util.Iterator;\nimport java.util.List;\n\n\npublic class Main {\n\n\n    public static class DOCXTest {\n\n        public XWPFDocument document = null;\n\n        public  DOCXTest() {\n        }\n\n        public  final void openFile(String filename) throws IOException {\n            File file = null;\n            FileInputStream fis = null;\n            try {\n                file = new File(filename);\n                fis = new FileInputStream(file);\n                this.document = new XWPFDocument(fis);\n            }\n            finally {\n                try {\n                    if(fis != null) {\n                        fis.close();\n                        fis = null;\n                    }\n                }\n                catch(IOException ioEx) {\n                }\n            }\n        }\n\n        public final void saveAs(String filename) throws IOException {\n            File file = null;\n            FileOutputStream fos = null;\n            try {\n                file = new File(filename);\n                fos = new FileOutputStream(file);\n                this.document.write(fos);\n            }\n            finally {\n                if(fos != null) {\n                    fos.close();\n                    fos = null;\n                }\n            }\n        }\n\n\n\n\n        private final void procParaList(List<XWPFParagraph> paraList,\n                                        String bookmarkName, String bookmarkValue) {\n            Iterator<XWPFParagraph> paraIter = null;\n            XWPFParagraph para = null;\n            List<CTBookmark> bookmarkList = null;\n            Iterator<CTBookmark> bookmarkIter = null;\n            CTBookmark bookmark = null;\n            XWPFRun run = null;\n            Node nextNode = null;\n\n            paraIter = paraList.iterator();\n            while(paraIter.hasNext()) {\n                para = paraIter.next();\n                bookmarkList = para.getCTP().getBookmarkStartList();\n                bookmarkIter = bookmarkList.iterator();\n\n                while(bookmarkIter.hasNext()) {\n                    bookmark = bookmarkIter.next();\n                    if(bookmark.getName().equals(bookmarkName)) {\n                        run = para.createRun();\n                        run.setText(bookmarkValue);\n                        nextNode = bookmark.getDomNode().getNextSibling();\n                        while(!(nextNode.getNodeName().contains("bookmarkEnd"))) {\n                            para.getCTP().getDomNode().removeChild(nextNode);\n                            nextNode = bookmark.getDomNode().getNextSibling();\n                        }\n                        para.getCTP().getDomNode().insertBefore(\n                                run.getCTR().getDomNode(),\n                                nextNode);\n                    }\n                }\n            }\n        }\n\n\n        public final void insertAtBookmark(String bookmarkName, String bookmarkValue) {\n            List<XWPFTable> tableList = null;\n            Iterator<XWPFTable> tableIter = null;\n            List<XWPFTableRow> rowList = null;\n            Iterator<XWPFTableRow> rowIter = null;\n            List<XWPFTableCell> cellList = null;\n            Iterator<XWPFTableCell> cellIter = null;\n            XWPFTable table = null;\n            XWPFTableRow row = null;\n            XWPFTableCell cell = null;\n\n            this.procParaList(this.document.ge\n            tableList = this.document.getTables();\n            tableIter = tableList.iterator();\n            while(tableIter.hasNext()) {\n                table = tableIter.next();\n                rowList = table.getRows();\n                rowIter = rowList.iterator();\n                while(rowIter.hasNext()) {\n                    row = rowIter.next();\n                    cellList = row.getTableCells();\n                    cellIter = cellList.iterator();\n                    while(cellIter.hasNext()) {\n                        cell = cellIter.next();\n                        this.procParaList(cell.getParagraphs(),\n                                bookmarkName,\n                                bookmarkValue);\n                    }\n                }\n            }\n        }\n\n\n    }\n\n\n\n    public static boolean LogEnabled = true;\n\n\n\n    public static void main(String[] args) {\n        AddLog("Start");\n        try {\n\n            DOCXTest docxTest = new DOCXTest();\n            docxTest.openFile("D:/template.docx");\n            docxTest.insertAtBookmark("FIO", "Ibadov Kamil \xc6\x8fl\xc9\x99sg\xc9\x99r");\n            docxTest.saveAs("D:/replaced.docx");\n            File outFile = new File("D:/replaced.pdf");\n            outFile.getParentFile().mkdirs();\n\n            OutputStream out = new FileOutputStream(outFile);\n            PdfOptions options = PdfOptions.create().fontEncoding(BaseFont.IDENTITY_H);\n            PdfConverter.getInstance().convert(docxTest.document, out, options);\n\n\n            AddLog("End");\n\n        } catch (Exception e) {\n            AddLog(e.getMessage());\n        }\n    }\n\n\n    public static void AddLog(String LogMessage) {\n        if (LogEnabled) {\n            try {\n                System.out.println(LogMessage);\n                BufferedWriter out = new BufferedWriter(new FileWriter("logs.txt"));\n                out.write(LogMessage);\n                out.close();\n            }\n            catch (IOException e)\n            {\n                System.out.println("Exception ");\n            }\n\n        }\n\n    }\n\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n