導航:首頁 > 編程語言 > java第一步pdf

java第一步pdf

發布時間:2024-05-06 01:29:31

A. java操作pdf表格數據

用Java簡單的讀取pdf文件中的數據:
第一步:下載PDFBox-0.7.2.jar。提供一個下載地址: http://pdfhome.hope.com.cn/Resource.aspx?CID=63844604-5253-4ae1-b023-258c9e324061&RID=20cd8f94-1cee-40b6-a3df-0ef024f8e0d2解壓後,把lib文件下的PDFBox-0.7.2.jar,PDFBox-0.7.2-log4j.jar放到你classpath路徑下。(我把源碼以及jar包都放到下面的附件里,方面你的使用。)
第二步:寫個簡單的讀取pdf文件的程序。(PdfReader.java)
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class PdfReader {
public void readFdf(String file) throws Exception {
// 是否排序
boolean sort = false;
// pdf文件名
String pdfFile = file;
// 輸入文本文件名稱
String textFile = null;
// 編碼方式
String encoding = "UTF-8";
// 開始提取頁數
int startPage = 1;
// 結束提取頁數
int endPage = Integer.MAX_VALUE;
// 文件輸入流,生成文本文件
Writer output = null;
// 內存中存儲的PDF Document
PDDocument document = null;
try {
try {
// 首先當作一個URL來裝載文件,如果得到異常再從本地文件系統//去裝載文件
URL url = new URL(pdfFile);
//注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
// 獲取PDF的文件名
String fileName = url.getFile();
// 以原來PDF的名稱來命名新產生的txt文件
if (fileName.length() > 4) {
File outputFile = new File(fileName.substring(0, fileName
.length() - 4)
+ ".txt");
textFile = outputFile.getName();
}
} catch (MalformedURLException e) {
// 如果作為URL裝載得到異常則從文件系統裝載
//注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
if (pdfFile.length() > 4) {
textFile = pdfFile.substring(0, pdfFile.length() - 4)
+ ".txt";
}
}
// 文件輸入流,寫入文件倒textFile
output = new OutputStreamWriter(new FileOutputStream(textFile),
encoding);
// PDFTextStripper來提取文本
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
// 設置是否排序
stripper.setSortByPosition(sort);
// 設置起始頁
stripper.setStartPage(startPage);
// 設置結束頁
stripper.setEndPage(endPage);
// 調用PDFTextStripper的writeText提取並輸出文本
stripper.writeText(document, output);
} finally {
if (output != null) {
// 關閉輸出流
output.close();
}
if (document != null) {
// 關閉PDF Document
document.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PdfReader pdfReader = new PdfReader();
try {
// 取得E盤下的SpringGuide.pdf的內容
pdfReader.readFdf("E:\\SpringGuide.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}
這樣就簡單的完成了從pdf中讀取數據了。在你的pdf文件所在的目錄下生成一個同名的txt文件。

B. java中怎麼將word轉pdf

C. java處理pdf文件

FileInputStream 讀取文件流就OK 至於在頁面顯示

1、客戶機上要有PDF2、URL url =new URL("file:///"+ 你的文件路徑);response.setContentType(url.openConnection().getContentType());response.setHeader("Content-Disposition", "inline; filename="+ 文件名);或在jsp頁面中加入 <% response.setHeader("Content-disposition", "attachment; filename=*.pdf"); %> 以上會提示下載、保存 <% response.setHeader("Content-disposition", "filename=*.pdf"); %> 不要attachment; 就會直接打開,顯示pdf了

D. 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));

}
}

E. 怎麼用java動態生成pdf文檔

Flying-Saucer + iText + Velocity
1. 第一步
將jar包放到你的工程里,需要的jar如下:
bcprov-jdk15-140.jar
core-renderer.jar
iText-2.0.8.jar
iTextAsian.jar
velocity-1.4.jar
Jar包下載地址:http://code.google.com/p/flying-saucer/downloads/list
2. 第二步
設計模版,進行排版調整樣式,css樣式也可以導入@import 等,通過Velocity模版引擎動態替換 頁面內容,以下是模版內容:
<?xml version="1.0" encoding="UTF-8" ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PDF模版</title>
<style type="text/css">
<!--
body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
text-align: center;
color: #000000;
}
.oneColLiqCtrHdr #container {
width: 100%;
margin: 0 auto;
text-align: left;
}
div.header-left {display: none}
div.header-right {display: none}
div.footer-left {display: none}
div.footer-right {display: none}

F. 如何用純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 包下載地址:

G. Java如何使用Java創建一個空的PDF文檔

package com.yii;import java.io.IOException;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;// 需要下 apache pdfbox包和apache.commons.loggin烏,下載地址:http://pdfbox.apache.org/download.cgi 和 http://commons.apache.org/proper/commons-logging/download_logging.cgi// 在本示例中下載使用的是:pdfbox-2.0.7.jar // 將下載的pdfbox-2.0.7.jar添加到Eclipse項目依懶庫中。// 右鍵點擊:"java_apache_pdf_box"->"Bulid Path"->"Add External Artchives...",然後選篤下載的"pdfbox-2.0.7.jar"和"commons-logging-1.2.jar"文件 public class CreatingEmptyPdf {
public static void main(String args[]) throws IOException {

// Creating PDF document object
PDDocument document = new PDDocument();

// Add an empty page to it
document.addPage(new PDPage());

// Saving the document
document.save("F:/worksp/javaexamples/java_apache_pdf_box/BlankPdf.pdf");
System.out.println("PDF created");

// Closing the document
document.close();
}}

H. 濡備綍榪愮敤Java緇勪歡itext鐢熸垚pdf

絎涓姝:涓嬭澆 iText5.5.6鐨勫帇緙╂枃浠,瑙e帇寰楀埌鏍稿績jar鍖卛textpdf-5.5.6.jar

涓嬭澆 extrajars-2.3.zip,瑙e帇鍚,寰楀埌鏀鎸佷腑鏂囩殑itext.asian.jar

絎浜屾:欏圭洰Build Path娣誨姞鍒氬垰鐨勪袱涓猨ar鍖

絎涓夋:寮濮嬪啓浠g爜

importjava.io.FileOutputStream;
importcom.itextpdf.text.Document;
importcom.itextpdf.text.Font;
importcom.itextpdf.text.Paragraph;
importcom.itextpdf.text.pdf.BaseFont;
importcom.itextpdf.text.pdf.PdfWriter;

publicclassPDFDemo{
//main鍑芥暟鎶涘嚭寮傚父,褰撶劧涔熷彲浠trycatch榪涜屽勭悊
publicstaticvoidmain(String[]args)throwsException{
//---------------絎涓闃舵靛噯澶-------------------------
//鍒涘緩涓涓狣ocument瀵硅薄
Documentdocument=newDocument();
//鍒涘緩PDF鍐欏叆鍣錛岄氳繃PDF鍐欏叆鍣ㄥ皢鏂囨。瀵硅薄鍐欏叆紓佺洏(絎涓涓鍙傛暟:鏂囨。瀵硅薄,絎浜屼釜鍙傛暟,杈撳嚭嫻)
PdfWriterpdfWriter=PdfWriter.getInstance(document,newFileOutputStream("c:\abcd.pdf"));
//鎵撳紑Document鏂囨。
document.open();
//鍚慏ocument鏂囨。涓娣誨姞鍐呭
//---------------絎浜岄樁孌靛啓鍏-------------------------
//鏂板緩孌佃惤絎涓孌
Paragraphp=newParagraph();
p.add("HelloWorldHappy");

//璁劇疆涓鏂囧瓧浣
BaseFontbaseFont=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",true);
Fontfont=newFont(baseFont);
//鏂板緩孌佃惤絎浜屾,鏀鎸佷腑鏂
Paragraphp2=newParagraph();
p2.setFont(font);
p2.add("闈炲父椋庝簯");
document.add(p);
document.add(p2);
//---------------絎涓夐樁孌墊敹灝-------------------------
//娣誨姞瀹屾瘯,鍏抽棴鏂囨。
document.close();
}
}

鏁堟灉灞曠ず

I. 怎麼用java代碼生成pdf文檔

用java代碼生成pdf文檔
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
// 創建一個Document對象
Document document = new Document();

try
{
// 生成名為 HelloWorld.pdf 的文檔
PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
// 添加PDF文檔的一些信息
document.addTitle("Hello World example");
document.addAuthor("Bruno Lowagie");
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello World, step 3, metadata");
document.addCreator("My program using iText");
// 打開文檔,將要寫入內容
document.open();
// 插入一個段落
document.add(new Paragraph("Hello World!"));

}
catch (DocumentException de)
{
System.err.println(de.getMessage());
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
// 關閉打開的文檔
document.close();
}
}
編譯運行以後,我們可以在運行的目錄發現生成的HelloWorld.pdf,打開可以看到我們寫的文字:

J. 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第一步pdf相關的資料

熱點內容
qq炫舞生名道演唱會 瀏覽:927
手機圖片怎麼轉文件夾 瀏覽:838
附近數據線廠在哪裡 瀏覽:294
類似秋霞影院的網站有哪些 瀏覽:489
thinkphp讀取配置文件 瀏覽:911
個稅app在哪裡填寫贍養父母 瀏覽:341
打開cad時總彈出一個文件 瀏覽:87
刪除一個文件夾找不到了 瀏覽:654
電腦桌面文件管理哪個軟體好 瀏覽:188
蘋果數據線頭歪了 瀏覽:135
ghostwin764位系統鏡像文件 瀏覽:443
感測器視頻教程下載 瀏覽:95
flash源文件賀卡下載 瀏覽:434
如何提高網路扶貧的效果 瀏覽:654
飛車軟體文件夾叫什麼 瀏覽:242
刷ec用什麼編程器 瀏覽:765
方菱數控u盤文件格式 瀏覽:260
編程為什麼輸出兩個變數 瀏覽:490
衛星大鍋2017用升級嗎 瀏覽:113
玉米win10系統下載 瀏覽:134

友情鏈接