资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

javapdf转换代码 javapdf转word

如何用纯java代码实现word转pdf?

1:用apache pio 读取doc文件,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。

肇东ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

2:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便。

3:地址

需要注意中文字体的写入问题。

4:使用jodconverter来调用openOffice的服务来转换,openOffice有个各个平台的版本,所以这种方法跟方法1一样都是跨平台的。

jodconverter的下载地址:

首先要安装openOffice,下载地址:office.org/download/index.html" target="_blank"

5:安装完后要启动openOffice的服务,具体启动方法请自行google。

6:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf)

7:Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件。

8:SaveAsPDFandXPS下载地址:microsoft.com/zh-cn/download/details.aspx?id=7" target="_blank"。

9:需要转换的工具 ,看你是linux还是word 。word还好不需要安装。linux就麻烦了。

爪哇是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。Java自面世后就非常流行,发展迅速,对C++语言形成了有力冲击。Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。

如何用纯java代码实现word转pdf

几种方案:

方法一:用apache pio 读取doc文件,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。

方法2:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便

地址:

需要注意中文字体的写入问题。

方法3:使用jodconverter来调用openOffice的服务来转换,openOffice有个各个平台的版本,所以这种方法跟方法1一样都是跨平台的。

jodconverter的下载地址:

首先要安装openOffice,下载地址:

安装完后要启动openOffice的服务,具体启动方法请自行google

方法4:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf)

Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件

SaveAsPDFandXPS下载地址:

jacob 包下载地址:

word文档转换成pdf 用java代码怎样实现?

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; } 将内容写入到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(); } } } } 心愿对你有用哈,我也是以前做测试的时候写的了……

Java如何把一个PDF转为tif

安装Office时,默认安装Microsoft Office Document Image Writer组件(没有重新添加),在Word中,选文件——打印——打印机选“Microsoft Office Document Image Writer”——属性——高级——输入格式选“TIFF-黑白传真”——设置默认输入的文件夹——确定——确定,打印完成后,将输出到刚设置的默认文件夹中。

java代码doc转pdf提高效率的方法

使用了jacob.jar来调用activex控件,本机需安装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 // 由于转换前设置cOption的AutosaveFilename不能保证输出的文件名与设置的相同(pdfcreator会加入/修改后缀名)

123 // 所以这里让pdfcreator使用自动生成的文件名进行输出,然后在保存后将其重命名为目标文件名

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 }


新闻标题:javapdf转换代码 javapdf转word
当前网址:http://cdkjz.cn/article/hicodd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220