參考代碼:
import com.spire.xls.*;
public class ExceltoPDF {
public static void main(String[] args) {
//載入一個Excel文檔
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//設置轉換時,工作表自適應PDF頁面
workbook.getConverterSetting().setSheetFitToPage(true);
//將Excel保存為PDF
workbook.saveToFile("ToPDF.pdf",FileFormat.PDF);
}
}
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,
所以不能給你太多幫助,
希望你早日成功!