導航:首頁 > 文檔加密 > pdfjava

pdfjava

發布時間:2022-01-30 07:15:35

❶ 如何用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性能優化中文版PDF下載

文件比較大,給你下載地址吧:
http://download.csdn.net/download/hx0_0_8/8434567

❸ java生成pdf幾種常見方式

用Spire.PDF for Java來生成PDF文檔的效果不錯,支持格式化操作比較多,可以參考下 Java 中創建 PDF 文檔

❹ java 如何讀取PDF文件內容

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();
}
}
}

❺ 如何用java讀取pdf文檔的部分內容

你需要用到PDFbox api

https://pdfbox.apache.org/1.8/cookbook/textextraction.html

例子如下

importjava.io.File;
importjava.io.IOException;
importorg.apache.pdfbox.pdmodel.PDDocument;
importorg.apache.pdfbox.text.PDFTextStripper;
importorg.apache.pdfbox.text.PDFTextStripperByArea;

try{
PDDocumentdocument=null;
document=PDDocument.load(newFile("test.pdf"));
document.getClass();
if(!document.isEncrypted()){
PDFTextStripperByAreastripper=newPDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripperTstripper=newPDFTextStripper();
Stringst=Tstripper.getText(document);
System.out.println("Text:"+st);
}
}catch(Exceptione){
e.printStackTrace();
}

❻ java如何對pdf文件實現數字簽名

用Spire.Doc for Java 可以添加及驗證數字簽名,參考代碼:

importcom.spire.pdf.*;
importcom.spire.pdf.graphics.PdfImage;
importcom.spire.pdf.graphics.PdfTrueTypeFont;
importcom.spire.pdf.security.GraphicMode;
importcom.spire.pdf.security.PdfCertificate;
importcom.spire.pdf.security.PdfCertificationFlags;
importcom.spire.pdf.security.PdfSignature;

importjava.awt.*;
importjava.awt.geom.Point2D;
importjava.awt.geom.Rectangle2D;

publicclassAddCertificate{
publicstaticvoidmain(String[]args){
//載入PDF文檔
PdfDocumentdoc=newPdfDocument();
doc.loadFromFile("test.pdf");

//載入pfx證書,及證書秘鑰
PdfCertificatecert=newPdfCertificate("Cermia.pfx","123654yes!");

//添加數字簽名到指定頁面,並設置其位置和大小
PdfSignaturesignature=newPdfSignature(doc,doc.getPages().get(2),cert,"MySignature");
Rectangle2Drect=newRectangle2D.Float();
rect.setFrame(newPoint2D.Float((float)doc.getPages().get(0).getActualSize().getWidth()-340,(float)doc.getPages().get(0).getActualSize().getHeight()-230),newDimension(280,150));
signature.setBounds(rect);

//設置簽名為圖片加文本模式
signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail);

//設置簽名的內容
signature.setNameLabel("簽字者:");
signature.setName("Mia");
signature.setContactInfoLabel("聯系電話:");
signature.setContactInfo("02881705109");
signature.setDateLabel("日期:");
signature.setDate(newjava.util.Date());
signature.setLocationInfoLabel("地點:");
signature.setLocationInfo("成都");
signature.setReasonLabel("原因:");
signature.setReason("文檔所有者");
signature.setDistinguishedNameLabel("DN:");
signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName());
signature.setSignImageSource(PdfImage.fromFile("sign.png"));

//設置簽名的字體
signature.setSignDetailsFont(newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,9)));

//設置文檔許可權為禁止更改
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
signature.setCertificated(true);

//保存文檔
doc.saveToFile("AddSignature.pdf");
doc.close();
}
}

數字簽名添加效果:

數字簽名效果

參考原文

❼ java如何創建pdf文件,並將資料庫數據寫入pdf

寫入PDF? 可以,不過需要下載操作 PDF 的JAR包!操作起來不是太麻煩,不過就是生成的時間比較長。。。

flex 是 flash的一種延伸,flash 對於網站載入來說本來就比較慢,比較的消耗資源,生成PDF 可想而知!

其次 flex 發布到jobss tomcat 等伺服器中時,你的 flex 是編譯成flash,還是直接將xml放入容器中讓其自動生成flash呢? 如果是的話,那反應就更慢了。。。

再後者,flex 雖然說是 flash 只要客戶端支持flash就能看見 你的網站,但flex 需要在jobss tomcat 等容器中配置的,有免費的有收費的,它們的配置方法都不一樣的。。。

好了就說到這里,雖然有點廢話,並且與主題無關,但希望對LZ有幫助!

❽ Java能直接修改pdf文件嗎

貌似沒見過這個插件,還有pdf有一部分是根本不能修改的,哪怕用專業工具都不行,如果pdf裡面的文字可以讀取出來,倒是可以導入txt然後就很好修改了,pdf圖文混排,覺得java修改難度較大

❾ 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如何使用Java向PDF頁面中添加文本

試試這個教程,需要依賴免費版的Spire.Pdf.jar包

importjava.awt.*;
importjava.awt.geom.Point2D;
importjava.awt.geom.Rectangle2D;
importjava.io.*;
importcom.spire.pdf.PdfPageBase;
importcom.spire.pdf.graphics.*;

{

publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException{

//創建PdfDocument對象
PdfDocumentdoc=newPdfDocument();

//添加一頁
PdfPageBasepage=doc.getPages().add();

//標題文字
Stringtitle="標題";

//創建單色畫刷對象
PdfSolidBrushbrush1=newPdfSolidBrush(newPdfRGBColor(Color.BLUE));
PdfSolidBrushbrush2=newPdfSolidBrush(newPdfRGBColor(Color.BLACK));

//創建TrueType字體對象
PdfTrueTypeFontfont1=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,14),true);
PdfTrueTypeFontfont2=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,10),true);

//創建PdfStringFormat對象
PdfStringFormatformat1=newPdfStringFormat();
format1.setAlignment(PdfTextAlignment.Center);//設置文字居中

//使用drawString方法繪制標題文字
page.getCanvas().drawString(title,font1,brush1,newPoint2D.Float(page.getActualBounds(true).width/2,0),format1);

//從txt文件讀取內容到字元串
Stringbody=readFileToString("C:\Users\Administrator\Desktop\bodyText.txt");

//創建PdfStringFormat對象
PdfStringFormatformat2=newPdfStringFormat();
format2.setParagraphIndent(20);//設置段首縮進

//創建Rectangle2D對象
Rectangle2D.Floatrect=newRectangle2D.Float(0,30,page.getActualBounds(true).width,page.getActualBounds(true).height);

//使用drawString方法在矩形區域繪制主體文字
page.getCanvas().drawString(body,font2,brush2,rect,format2);

//保存到PDF文檔
doc.saveToFile("ouput.pdf");
}

//自定義方法讀取txt文件內容到字元串
(Stringfilepath)throwsFileNotFoundException,IOException{

StringBuildersb=newStringBuilder();
Strings="";
BufferedReaderbr=newBufferedReader(newFileReader(filepath));

while((s=br.readLine())!=null){
sb.append(s+" ");
}
br.close();
Stringstr=sb.toString();
returnstr;
}

}
閱讀全文

與pdfjava相關的資料

熱點內容
半夜解壓有什麼壞處 瀏覽:424
linux代理命令 瀏覽:637
調用tasking的編譯器編譯 瀏覽:292
青檸app是什麼 瀏覽:866
linuxapachephp56 瀏覽:395
安卓手機如何打開eng文件 瀏覽:22
看拉丁電視都用什麼app好 瀏覽:778
什麼是哲學pdf 瀏覽:508
hdfs的三個下載命令 瀏覽:523
java常用的排序演算法 瀏覽:357
51單片機連接adc 瀏覽:859
python命名變數報錯 瀏覽:120
安卓手機如何換windows系統 瀏覽:612
python中的類是什麼 瀏覽:631
我的英雄學院用哪個app可以看 瀏覽:36
excel插入選項卡對象命令 瀏覽:693
python字元全排列 瀏覽:505
824頁大瓜文件pdf 瀏覽:222
朔州ios源碼 瀏覽:251
演算法邏輯電路 瀏覽:943