核心代碼如下
package com.hmkcode;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;
public class App {
public static final String HTML = "<h1>Hello</h1>"
+ "<p>This was created using iText</p>"
+ "<a href='hmkcode.com'>hmkcode.com</a>";
public static void main( String[] args ) throws FileNotFoundException, IOException
{
HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
System.out.println( "PDF Created!" );
}
}
② 如何用純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就麻煩了。
③ 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));
}
}
④ 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
}
}
⑤ java 怎麼把txt轉換成pdf
這個框架:
http://sourceforge.net/projects/itext/
開源的,而且使用起來比較簡單
⑥ 如何通過Java編碼實現 txt,xls,ppt,doc等文檔轉換成PDF
用POI來讀取這些文檔,然後生成 PDF,你可以網路一下POI。生成PDF 的具體標准我沒有看過,應該有那些成熟的JAVA 的類包的。
⑦ java中怎麼將word轉pdf
能安裝第三方軟體的話,可以考慮以使用Spire.Doc for Java:
你可以在Java程序中添加 Spire.Doc.jar 文件作為依賴項。可以從這個鏈接下載 JAR 文件;如果使用Maven,則可以通過在 pom.xml 文件中添加以下代碼導入 JAR 文件。
repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository></repositories><dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.3</version>
</dependency></dependencies>
將Word轉換成PDF:
Spire.Doc for Java擁有強大的文件轉換功能,其提供了 Document. saveToFile(String fileName, FileFormat fileFormat) 方法可將 Word 文檔轉為多種格式的目標文件,下面是轉為 PDF 的方法步驟:
實例化Document類的對象。
調用Document.loadFromFile()方法載入 Word 文檔。
調用Document.saveToFile()方法將 Word 保存為 PDF 格式,並指定保存路徑。
Java代碼如下:
import com.spire.doc.*;public class WordToPDF{
public static void main(String[] args) {
//實例化Document類的對象
Document doc = new Document();
//載入Word
doc.loadFromFile("測試.docx");
//保存為PDF格式
doc.saveToFile("WordToPDF.pdf",FileFormat.PDF);
}
}
希望對您有幫助。
⑧ 如何用java實現導出PDF格式的文檔
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfWriter; public class PdfTest { public static void main(String[] args) throws Exception { Document pdfDoc = new Document(); // 將要生成的 pdf 文件的路徑輸出流 FileOutputStream pdfFile = new FileOutputStream(new File("F:/study/test/firstPdf.pdf")); // pdf 文件中的一個文欄位落 Paragraph paragraph = new Paragraph("My first PDF file with an image ..."); Image image = Image.getInstance("F:/study/test/洛克 李.jpg"); // 用 Document 對象、File 對象獲得 PdfWriter 輸出流對象 PdfWriter.getInstance(pdfDoc, pdfFile); pdfDoc.open(); // 打開 Document 文檔 // 添加一個文欄位落、一張圖片 pdfDoc.add(paragraph); pdfDoc.add(image); pdfDoc.close(); } }
⑨ Java 使用 openoffice 轉換TXT 為 PDF,出現中文亂碼問題
建議你用notepad++(或UE)吧txt文件的文件編碼改為utf-8,再轉一下pdf試試,估計八成能解決你的問題。
⑩ java txt轉化pdf格式出錯
讀取txt文檔最後記得flush一下