Jacob use turn PDF, poi generate Word ( Note: only supports windows platform, you need jacob-1.14.3-x64.dll documents, stored in the C: \ Windows \ System32 path )
First, the steps of:
1 . Import the required Jar package 2 . Word document generation 3 . To PDF file
Second, the desired Jar package, as shown:
Third, generate Word file code:
// Returns Docx special characters need to be replaced, there are no duplicate entries // recommended parameters passed regular expression "\\ \\ $ {[^ {}] + \\}" public the ArrayList <String> getReplaceElementsInWord (String filePath, REGEX String) { String [] P = filePath.split ( "\\." ); IF (p.length> 0) { // Analyzing no extension file // compare the file extension IF (P [p.length -. 1] .equalsIgnoreCase ( "DOC" )) { the ArrayList <String> Al = new new the ArrayList <> (); File File = new new File (filePath); HWPFDocument Document = null ; try { InputStream is = new FileInputStream(file); document = new HWPFDocument(is); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Range range = document.getRange(); String rangeText = range.text(); CharSequence cs = rangeText.subSequence(0, rangeText.length()); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(cs); int startPosition = 0; while (matcher.find(startPosition)) { if (!al.contains(matcher.group())) { al.add(matcher.group()); } startPosition = matcher.end(); } return al; } else if (p[p.length - 1].equalsIgnoreCase("docx")) { ArrayList<String> al = new ArrayList<>(); XWPFDocument document = null; try { document = new XWPFDocument(POIXMLDocument.openPackage(filePath)); } catch (IOException e) { e.printStackTrace(); } // 遍历段落 Iterator<XWPFParagraph> itPara = document.getParagraphsIterator(); while (itPara.hasNext()) { XWPFParagraph paragraph = (XWPFParagraph) itPara.next(); String paragraphString = paragraph.getText(); CharSequence cs = paragraphString.subSequence(0, paragraphString.length()); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(cs); int startPosition = 0; while (matcher.find(startPosition)) { if (!al.contains(matcher.group())) { al.add(matcher.group()); } startPosition = matcher.end(); } } // 遍历表 Iterator<XWPFTable> itTable = document.getTablesIterator(); while (itTable.hasNext()) { XWPFTable table = (XWPFTable) itTable.next(); int rcount = table.getNumberOfRows(); for (int i = 0; i < rcount; i++) { XWPFTableRow row = table.getRow(i); List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) { String cellText = ""; cellText = cell.getText(); CharSequence cs = cellText.subSequence(0, cellText.length()); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(cs); int startPosition = 0; while (matcher.find(startPosition)) { if (!al.contains(matcher.group())) { al.add(matcher.group()); } startPosition =matcher.end (); } } } } return Al; } the else { return null ; } } the else { return null ; } } // alternative word needs to be replaced in the special character public static Boolean replaceAndGenerateWord (by srcPath String, String destPath, map <String, String> Map) { String [] SP = srcPath.split ( "\\." ); String [] DP = destPath.split ( "\\." ); IF ((sp.length> 0) && (dp.length> 0)) { // Analyzing no extension file // compare the file extension IF (SP [sp.length -. 1] .equalsIgnoreCase ( "docx" ) ) { the try { XWPFDocument Document = new new XWPFDocument (POIXMLDocument.openPackage (by srcPath)); // replace paragraph of text is designated the Iterator <XWPFParagraph> itPara = document.getParagraphsIterator (); the while (itPara.hasNext ()) { XWPFParagraph paragraph = (XWPFParagraph) itPara.next (); List <XWPFRun> = the runs paragraph.getRuns (); for (int i = 0; i < runs.size(); i++) { String onegaString = runs.get(i).getText(runs.get(i).getTextPosition()); for (Map.Entry<String, String> entry : map.entrySet()) { onegaString = onegaString.replace(entry.getKey(), entry.getValue()); } runs.get(i).setText(onegaString, 0); } } // 替换表格中的指定文字 Iterator<XWPFTable> itTable = document.getTablesIterator(); while (itTable.hasNext()) { XWPFTable table = (XWPFTable) itTable.next(); int remount = table.getNumberOfRows(); for (int i = 0; i < remount; i++) { XWPFTableRow row = table.getRow(i); List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) { String cellTextString = cell.getText(); for (Map.Entry<String, String> e : map.entrySet()) { if (cellTextString.contains(e.getKey())) cellTextString = cellTextString.replace(e.getKey(), e.getValue()); } cell.removeParagraph(0); cell.setText(cellTextString); } } } FileOutputStream outStream = null; the outStream = new new FileOutputStream(destPath); document.write(outStream); outStream.close (); return to true ; } the catch (Exception E) { e.printStackTrace (); return to false ; } // DOC only generate doc, docx will generate an error if } the else IF ((SP [SP .length -. 1] .equalsIgnoreCase ( "DOC")) && (DP [dp.length -. 1] .equalsIgnoreCase ( "DOC" ))) { HWPFDocument Document = null ; the try { Document = new new HWPFDocument (new FileInputStream(srcPath)); Range range = document.getRange(); for (Map.Entry<String, String> entry : map.entrySet()) { range.replaceText(entry.getKey(), entry.getValue()); } FileOutputStream outStream = new FileOutputStream(destPath); document.write(outStream); outStream.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } } else { return false; } } else { return false; } }
Four, Word to PDF Code:
public static void wordToPdf (fromAddress, String, String the toAddress) { ActiveXComponent AX = null ; the try { Long the startTime = System.currentTimeMillis (); AX = new new ActiveXComponent ( "the Word.Application" ); // set to open word document invisible ax. setProperty ( "the Visible", false ); // get all the contents of Word documents Dispatch docs = ax.getProperty ( "documents" ) .toDispatch (); // open a word document and set the word is not editable and without confirmation Dispatch doc = Dispatch.call(docs, "Open", fromAddress,// FileName false,// ConfirmConversions true // ReadOnly ).toDispatch(); File tofile = new File(toAddress); if (tofile.exists()) { tofile.delete(); } //word文件另存为pdf文件 Dispatch.call(doc,// "SaveAs", // toAddress, // FileName wdFormatPDF); //Close word document Dispatch.call (DOC, "use Close", false ); Long endTime = System.currentTimeMillis (); System.out.println ( "conversion was complete, took a total of" + (endTime - startTime) + "ms." ); } the catch (Exception E) { System.out.println ( "Error ========: document conversion failed:" + e.getMessage ()); } the finally { IF ! (AX = null ) AX .invoke ( "the Quit", new new the Variant [] {}); } }
Note: The code is written in 2017, if they are not compatible with it, please contact me at