新聞中心
如何用純java代碼實(shí)現(xiàn)word轉(zhuǎn)pdf?
1:用apache pio 讀取doc文件,然后轉(zhuǎn)成html文件用Jsoup格式化html文件,最后用itext將html文件轉(zhuǎn)成pdf。

肇東ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
2:使用jdoctopdf來(lái)實(shí)現(xiàn),這是一個(gè)封裝好的包,可以把doc轉(zhuǎn)換成pdf,html,xml等格式,調(diào)用很方便。
3:地址
需要注意中文字體的寫入問(wèn)題。
4:使用jodconverter來(lái)調(diào)用openOffice的服務(wù)來(lái)轉(zhuǎn)換,openOffice有個(gè)各個(gè)平臺(tái)的版本,所以這種方法跟方法1一樣都是跨平臺(tái)的。
jodconverter的下載地址:
首先要安裝openOffice,下載地址:office.org/download/index.html" target="_blank"
5:安裝完后要啟動(dòng)openOffice的服務(wù),具體啟動(dòng)方法請(qǐng)自行g(shù)oogle。
6:效果最好的一種方法,但是需要window環(huán)境,而且速度是最慢的需要安裝msofficeWord以及SaveAsPDFandXPS.exe(word的一個(gè)插件,用來(lái)把word轉(zhuǎn)化為pdf)
7:Office版本是2007,因?yàn)镾aveAsPDFandXPS是微軟為office2007及以上版本開(kāi)發(fā)的插件。
8:SaveAsPDFandXPS下載地址:microsoft.com/zh-cn/download/details.aspx?id=7" target="_blank"。
9:需要轉(zhuǎn)換的工具 ,看你是linux還是word 。word還好不需要安裝。linux就麻煩了。
爪哇是一種可以撰寫跨平臺(tái)應(yīng)用軟件的面向?qū)ο蟮某绦蛟O(shè)計(jì)語(yǔ)言,是由Sun Microsystems公司于1995年5月推出的Java程序設(shè)計(jì)語(yǔ)言和Java平臺(tái)(即JavaEE, JavaME, JavaSE)的總稱。Java自面世后就非常流行,發(fā)展迅速,對(duì)C++語(yǔ)言形成了有力沖擊。Java技術(shù)具有卓越的通用性、高效性、平臺(tái)移植性和安全性,廣泛應(yīng)用于個(gè)人PC、數(shù)據(jù)中心、游戲控制臺(tái)、科學(xué)超級(jí)計(jì)算機(jī)、移動(dòng)電話和互聯(lián)網(wǎng),同時(shí)擁有全球最大的開(kāi)發(fā)者專業(yè)社群。在全球云計(jì)算和移動(dòng)互聯(lián)網(wǎng)的產(chǎn)業(yè)環(huán)境下,Java更具備了顯著優(yōu)勢(shì)和廣闊前景。
如何用純java代碼實(shí)現(xiàn)word轉(zhuǎn)pdf
幾種方案:
方法一:用apache pio 讀取doc文件,然后轉(zhuǎn)成html文件用Jsoup格式化html文件,最后用itext將html文件轉(zhuǎn)成pdf。
方法2:使用jdoctopdf來(lái)實(shí)現(xiàn),這是一個(gè)封裝好的包,可以把doc轉(zhuǎn)換成pdf,html,xml等格式,調(diào)用很方便
地址:
需要注意中文字體的寫入問(wèn)題。
方法3:使用jodconverter來(lái)調(diào)用openOffice的服務(wù)來(lái)轉(zhuǎn)換,openOffice有個(gè)各個(gè)平臺(tái)的版本,所以這種方法跟方法1一樣都是跨平臺(tái)的。
jodconverter的下載地址:
首先要安裝openOffice,下載地址:
安裝完后要啟動(dòng)openOffice的服務(wù),具體啟動(dòng)方法請(qǐng)自行g(shù)oogle
方法4:效果最好的一種方法,但是需要window環(huán)境,而且速度是最慢的需要安裝msofficeWord以及SaveAsPDFandXPS.exe(word的一個(gè)插件,用來(lái)把word轉(zhuǎn)化為pdf)
Office版本是2007,因?yàn)镾aveAsPDFandXPS是微軟為office2007及以上版本開(kāi)發(fā)的插件
SaveAsPDFandXPS下載地址:
jacob 包下載地址:
word文檔轉(zhuǎn)換成pdf 用java代碼怎樣實(shí)現(xiàn)?
Java code String getWord(String path) { System.out.println(); System.out.println(); File file File(path); String returnString ; InputStream is; try { is FileInputStream(file); WordExtractor extractor WordExtractor(); returnString = extractor.extractText(is); System.out.println(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return returnString; } 將內(nèi)容寫入到pdf中Java codeIOException{ System.out.println(); System.out.println(); ; // create the file File(filePath); file.exists()){ Document doc ; FileOutputStream fos ; PdfWriter pdfWriter ; try { BaseFont bFont ); Font font Font(bFont); font.setSize(8); doc Document(PageSize.A4, margin, margin, margin, margin); fos FileOutputStream(file); pdfWriter = PdfWriter.getInstance(doc, fos); doc.open(); doc.addTitle(filePath.replace()); doc.add(new Paragraph(content, font)); System.out.println(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ ){ doc.close(); } ){ pdfWriter.close(); } ){ fos.close(); } } } } 心愿對(duì)你有用哈,我也是以前做測(cè)試的時(shí)候?qū)懙牧恕?/p>
Java如何把一個(gè)PDF轉(zhuǎn)為tif
安裝Office時(shí),默認(rèn)安裝Microsoft Office Document Image Writer組件(沒(méi)有重新添加),在Word中,選文件——打印——打印機(jī)選“Microsoft Office Document Image Writer”——屬性——高級(jí)——輸入格式選“TIFF-黑白傳真”——設(shè)置默認(rèn)輸入的文件夾——確定——確定,打印完成后,將輸出到剛設(shè)置的默認(rèn)文件夾中。
java代碼doc轉(zhuǎn)pdf提高效率的方法
使用了jacob.jar來(lái)調(diào)用activex控件,本機(jī)需安裝WPS或pdfcreator
001 package experiments;
002
003 import com.jacob.activeX.ActiveXComponent;
004 import com.jacob.com.Dispatch;
005 import com.jacob.com.DispatchEvents;
006 import com.jacob.com.Variant;
007 import java.io.File;
008 import java.util.logging.Level;
009 import java.util.logging.Logger;
010
011 public class Doc2Pdf {
012
013 public static Converter newConverter(String name) {
014 if (name.equals("wps")) {
015 return new Wps();
016 } else if (name.equals("pdfcreator")) {
017 return new PdfCreator();
018 }
019 return null;
020 }
021
022 public synchronized static boolean convert(String word, String pdf) {
023 return newConverter("pdfcreator").convert(word, pdf);
024 }
025
026 public static interface Converter {
027
028 public boolean convert(String word, String pdf);
029 }
030
031 public static class Wps implements Converter {
032
033 public synchronized boolean convert(String word, String pdf) {
034 File pdfFile = new File(pdf);
035 File wordFile = new File(word);
036 ActiveXComponent wps = null;
037 try {
038 wps = new ActiveXComponent("wps.application");
039 ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", newVariant(wordFile.getAbsolutePath()));
040 doc.invoke("ExportPdf", new Variant(pdfFile.getAbsolutePath()));
041 doc.invoke("Close");
042 doc.safeRelease();
043 } catch (Exception ex) {
044 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
045 return false;
046 } catch (Error ex) {
047 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
048 return false;
049 } finally {
050 if (wps != null) {
051 wps.invoke("Terminate");
052 wps.safeRelease();
053 }
054 }
055 return true;
056 }
057 }
058
059 public static class PdfCreator implements Converter {
060
061 public static final int STATUS_IN_PROGRESS = 2;
062 public static final int STATUS_WITH_ERRORS = 1;
063 public static final int STATUS_READY = 0;
064 private ActiveXComponent pdfCreator;
065 private DispatchEvents dispatcher;
066 private volatile int status;
067 private Variant defaultPrinter;
068
069 private void init() {
070 pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator");
071 dispatcher = new DispatchEvents(pdfCreator, this);
072 pdfCreator.setProperty("cVisible", new Variant(false));
073 pdfCreator.invoke("cStart", new Variant[]{newVariant("/NoProcessingAtStartup"), new Variant(true)});
074 setCOption("UseAutosave", 1);
075 setCOption("UseAutosaveDirectory", 1);
076 setCOption("AutosaveFormat", 0); // 0 = PDF
077 defaultPrinter = pdfCreator.getProperty("cDefaultPrinter");
078 status = STATUS_IN_PROGRESS;
079 pdfCreator.setProperty("cDefaultprinter", "PDFCreator");
080 pdfCreator.invoke("cClearCache");
081 pdfCreator.setProperty("cPrinterStop", false);
082 }
083
084 private void setCOption(String property, Object value) {
085 Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]);
086 }
087
088 private void close() {
089 if (pdfCreator != null) {
090 pdfCreator.setProperty("cDefaultprinter", defaultPrinter);
091 pdfCreator.invoke("cClearCache");
092 pdfCreator.setProperty("cPrinterStop", true);
093 pdfCreator.invoke("cClose");
094 pdfCreator.safeRelease();
095 pdfCreator = null;
096 }
097 if (dispatcher != null) {
098 dispatcher.safeRelease();
099 dispatcher = null;
100 }
101 }
102
103 public synchronized boolean convert(String word, String pdf) {
104 File pdfFile = new File(pdf);
105 File wordFile = new File(word);
106 try {
107 init();
108 setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath());
109 if (pdfFile.exists()) {
110 pdfFile.delete();
111 }
112 pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath());
113 int seconds = 0;
114 while (isInProcess()) {
115 seconds++;
116 if (seconds 30) { // timeout
117 throw new Exception("convertion timeout!");
118 }
119 Thread.sleep(1000);
120 }
121 if (isWithErrors()) return false;
122 // 由于轉(zhuǎn)換前設(shè)置cOption的AutosaveFilename不能保證輸出的文件名與設(shè)置的相同(pdfcreator會(huì)加入/修改后綴名)
123 // 所以這里讓pdfcreator使用自動(dòng)生成的文件名進(jìn)行輸出,然后在保存后將其重命名為目標(biāo)文件名
124 File outputFile = newFile(pdfCreator.getPropertyAsString("cOutputFilename"));
125 if (outputFile.exists()) {
126 outputFile.renameTo(pdfFile);
127 }
128 } catch (InterruptedException ex) {
129 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
130 return false;
131 } catch (Exception ex) {
132 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
133 return false;
134 } catch (Error ex) {
135 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
136 return false;
137 } finally {
138 close();
139 }
140 return true;
141 }
142
143 private boolean isInProcess() {
144 return status == STATUS_IN_PROGRESS;
145 }
146
147 private boolean isWithErrors() {
148 return status == STATUS_WITH_ERRORS;
149 }
150
151 // eReady event
152 public void eReady(Variant[] args) {
153 status = STATUS_READY;
154 }
155
156 // eError event
157 public void eError(Variant[] args) {
158 status = STATUS_WITH_ERRORS;
159 }
160 }
161
162 public static void main(String[] args) {
163 convert("e:\\test.doc", "e:\\output.pdf");
164 }
165 }
新聞標(biāo)題:javapdf轉(zhuǎn)換代碼 javapdf轉(zhuǎn)word
轉(zhuǎn)載來(lái)于:http://www.dlmjj.cn/article/hicodd.html
其他資訊
- 如何使用SQL將數(shù)據(jù)導(dǎo)入現(xiàn)有的數(shù)據(jù)庫(kù)?(sql導(dǎo)入已有數(shù)據(jù)庫(kù))
- Nginx技巧:通過(guò)會(huì)話緩存和超時(shí)設(shè)置優(yōu)化SSL
- 讓開(kāi)發(fā)人員交付出色用戶體驗(yàn)的5種方法
- 云控同步數(shù)據(jù)庫(kù),保障數(shù)據(jù)安全與高效管理(云控同步數(shù)據(jù)庫(kù))
- SQL代碼操作:輕松創(chuàng)建數(shù)據(jù)庫(kù)連接(sql用代碼創(chuàng)建數(shù)據(jù)庫(kù)連接)


咨詢
建站咨詢
