导航:首页 > 文档加密 > java转pdf

java转pdf

发布时间:2022-02-06 01:18:45

1. java中怎么将文件excel文件转换成pdf格式

参考代码:

  1. import com.spire.xls.*;

  2. public class ExceltoPDF {

  3. public static void main(String[] args) {

  4. //加载一个Excel文档

  5. Workbook workbook = new Workbook();

  6. workbook.loadFromFile("Sample.xlsx");

  7. //设置转换时,工作表自适应PDF页面

  8. workbook.getConverterSetting().setSheetFitToPage(true);

  9. //将Excel保存为PDF

  10. workbook.saveToFile("ToPDF.pdf",FileFormat.PDF);

  11. }

  12. }

需要在java项目中添加对Spire.xls.jar的依赖

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

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

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

3:地址http://www.maxstocker.com/jdoctopdf/downloads.php

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

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

jodconverter的下载地址:http://www.artofsolving.com/opensource/jodconverter

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

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">http://www.microsoft.com/zh-cn/download/details.aspx?id=7。

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

3. java itext转换PDF

public void GenerateAllParts() {
Document document = new Document();

try {
PdfWriter.getInstance(document, new FileOutputStream("d:\\all.pdf"));

// 生成字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
// 标题字体
Font f30 = new Font(bfChinese, 30, Font.NORMAL, Color.BLACK);
// 正文字体
Font f12 = new Font(bfChinese, 12, Font.NORMAL, Color.BLACK);
Font f6 = new Font(bfChinese, 6, Font.NORMAL, Color.BLACK);
Font f8 = new Font(bfChinese, 8, Font.NORMAL, Color.BLACK);

document.open();

// 标题
document.add(new Paragraph("报表实例", f30));
// 换行
document.add(new Chunk("\n\n"));
//
document.add(
new Paragraph(
new Chunk(".......................点击查看报表", f12)
.setLocalGoto("table")));
// 换行
document.add(new Chunk("\n\n"));
document.add(
new Paragraph(
new Chunk(".......................点击查看图片", f12)
.setLocalGoto("image")));
document.add(new Chunk("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"));

///////////////////////////////////////////////////
// 报表位置
document.add(new Chunk("报表实例", f12).setLocalDestination("table"));
// 添加table实例
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
PdfPCell cell = new PdfPCell();
cell.setBackgroundColor(new Color(213, 141, 69));
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);

// 表格标题
cell.setPhrase(new Paragraph("标题一", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("标题二", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("标题三", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("标题四", f8));
table.addCell(cell);
cell.setPhrase(new Paragraph("标题五", f8));
table.addCell(cell);

// 表格数据
PdfPCell newcell = new PdfPCell();
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph("数据一", f8));
table.addCell(newcell);
newcell.setPhrase(new Paragraph("数据二", f8));
table.addCell(newcell);
newcell.setPhrase(new Paragraph("数据三", f8));
table.addCell(newcell);
newcell.setPhrase(new Paragraph("数据四", f8));
table.addCell(newcell);
newcell.setPhrase(new Paragraph("数据五", f8));
table.addCell(newcell);

document.add(table);
////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
// 添加连接
document.add(new Chunk("图片实例", f12).setLocalDestination("image"));
Image jpg = Image.getInstance("d:\\3.jpg");
document.add(jpg);
//////////////////////////////////////////////////////////

document.close();
} catch (Exception e) {
// TODO: handle exception
}
}

4. 怎样用Java把html转为pdf

java中利用第三方jar包iText 5.4.2就可以实现html转为pdf。

比如有如下index.html文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>HTML to PDF</title>

<link href="style.css" rel="stylesheet" type="text/css" />

</head>

<body>

<h1>HTML to PDF</h1>

<p>

<span class="itext">itext</span> 5.4.2 <span class="description"> converting HTML to PDF</span>

</p>

<table>

<tr>

<th class="label">Title</th>

<td>iText - Java HTML to PDF</td>

</tr>

<tr>

<th>URL</th>

<td>http://hmkcode.com/itext-html-to-pdf-using-java</td>

</tr>

</table>

</body>

</html>

样式文件:

h1 {

color:#ccc;

}

table tr td{

text-align:center;

border:1px solid gray;

padding:4px;

}

table tr th{

background-color:#84C7FD;

color:#fff;

width:100px;

}

.itext{

color:#84C7FD;

font-weight:bold;

}

.description{

color:gray;

}

后台转换的接口:

package com.hmkcode;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.pdf.PdfWriter;

import com.itextpdf.tool.xml.XMLWorkerHelper;

public class App

{

public static void main( String[] args )throws DocumentException, IOException

{

// step 1

Document document =new Document();

// step 2

PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("pdf.pdf"));

// step 3

document.open();

// step 4

XMLWorkerHelper.getInstance().parseXHtml(writer, document,

new FileInputStream("index.html"));

//step 5

document.close();

System.out.println("PDF Created!" );

}

}

转换结果:

5. java word转pdf

可以试试spire.doc for java转换Word到PDF

6. 大神们!如何用java将上传的文件转换成为pdf

我曾经也尝试使用纯java技术去解析word文档,并且使用了apache的jacob,POI等项目,但是由于Microsoft Word使用的doc不是标准DOC文件,而是自己加处理过的,所以现在解析微软的doc都只能靠破解与猜解,据我所知,现在的技术只能从word中提取出来文字,所以使用纯java不太可能实现。
如果使用windows平台的话,可以选择使用微软的一些word控件来达到目的.

7. java导出PDF文档

java导出pdf需要用到iText库,iText是着名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf
的文档,而且可以将XML、Html文件转化为PDF文件。
iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用
iText类库了。
代码如下:

public class createPdf {
//自己做的一个简单例子,中间有图片之类的
//先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document
Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);
public void getPDFdemo() throws DocumentException, IOException{
//这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见:
//首先
//字体的定义:这里用的是自带的jar里面的字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
// 当然你也可以用你电脑里面带的字体库
//BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//定义字体 注意在最新的包里面 颜色是封装的
Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));
//生成pdf的第一个步骤:
//保存本地指定路径
saveLocal();
document.open();
ByteArrayOutputStream ba = new ByteArrayOutputStream();
// PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
//获取此编译的文件路径
String path = this.getClass().getClassLoader().getResource("").getPath();
//获取根路径
String filePath = path.substring(1, path.length()-15);
//获取图片路径 找到你需要往pdf上生成的图片
//这里根据自己的获取的路径写 只要找到图片位置就可以
String picPath = filePath +"\\WebContent" +"\\images\\";
//往PDF中添加段落
Paragraph pHeader = new Paragraph();
pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1)));
//pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 );
document.add(pHeader);//在文档中加入你写的内容
//获取图片
Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png");
//定义图片在文档中显示的绝对位置
img2.scaleAbsolute(137.0F, 140.0F);
img2.setAbsolutePosition(330.0F, 37.0F);
//将图片添加到文档中
document.add(img2);
//关闭文档
document.close();
/*//设置文档保存的文件名
response.setHeader("Content-
disposition", "attachment;filename=\""+ new String(("CCF会员资格确认
函.pdf").getBytes("GBK"),"ISO-8859-1") + "\"");
//设置类型
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();*/
}
public static void main(String[]args) throws DocumentException, IOException{
createPdf pdf= new createPdf();
pdf.getPDFdemo();
}

//指定一个文件进行保存 这里吧文件保存到D盘的text.pdf
public void saveLocal() throws IOException, DocumentException{
//直接生成PDF 制定生成到D盘test.pdf
File file = new File("D:\\text2.pdf");
file.createNewFile();
PdfWriter.getInstance(document, new FileOutputStream(file));

}
}

8. 用java 实现 word、jpg转PDF

可以用apache的batik
也可以用itext,
flying saucer好像也可以实现,
不过flying saucer很久没有维护过了。
具体怎么实现你再研究一下吧
因为我目前也在研究中,
不过是从svg,xml转PDF,
所以不能给你太多帮助,
希望你早日成功!

阅读全文

与java转pdf相关的资料

热点内容
拍卖程序员 浏览:101
电脑的图片放在哪个文件夹 浏览:274
unsignedintjava 浏览:216
编译器下载地址 浏览:42
什么是面对对象编程 浏览:708
b站服务器什么时候恢复 浏览:721
6p相当于安卓机什么水准 浏览:498
能否给隐藏相册加密 浏览:596
糖心app改什么名 浏览:823
战地1控服务器如何部署 浏览:394
xp还原系统输入命令 浏览:323
mysql命令行版本 浏览:303
如何进入itunes找文件夹 浏览:832
CAD中重复命令使用 浏览:478
心智pdf 浏览:475
网站电台直播间源码 浏览:852
文件夹14c和18c的区别 浏览:36
android隐式调用 浏览:668
plc的编程指令边沿继电器 浏览:723
voc文件夹 浏览:866