當前位置:首頁 » 密碼管理 » javapdf加密文件

javapdf加密文件

發布時間: 2023-02-18 08:29:57

❶ 如何使用java代碼壓縮PDF文件

用java代碼壓縮應用到程序了,代碼一般是比較復雜的,對pdf文件的mate標簽優化,這類標簽包括三類,pdf文件不是網頁就是個文件,何況我們可以用pdf壓縮工具壓縮,下面有個解決方法,樓主可以做參照。

1:點擊打開工具,打開主頁面上有三個功能進行選擇,我們選擇pdf文件壓縮。

❷ java 如何給pdf文件加水印

Java生成PDF 加密 水印

1、iText簡介

iText是一個開放源碼的Java類庫,可以用來方便地生成PDF文件。大家通過訪問http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948
下載最新版本的類庫,下載完成之後會得到一個.jar包,把這個包加入JDK的classpath即可使用。

如果生成的PDF文件中需要出現中文、日文、韓文字元,則還需要通過訪問http://itext.sourceforge.net/downloads/iTextAsian.jar
下載iTextAsian.jar包。

關於iText類庫的使用,http://www.lowagie.com/iText/tutorial/index.html
有比較詳細的教程。該教程從入門開始,比較系統地介紹了在PDF文件中放入文字、圖片、表格等的方法和技巧。

讀完這片教程,大致就可以做一些從簡單到復雜的PDF文件了。不過,試圖通過教程解決在生成PDF文件過程中遇到的所有困難無疑是一種奢望。所以,閱讀iText的api文檔顯得非常重要。讀者在下載類庫的同時,也可以下載類庫的文檔。

註:如果以上兩個下載鏈接無法下載而且通過網路也找不到這個jar包的同志可以留下郵箱地址,我會在兩個工作日之內發郵件過去。

以下部分我是我調試通過的源代碼,提供大家參考:
import java.awt.*;
import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import
com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;

/**
* 最近的項目中使用Itext將txt文件轉換為PDF文件, 並且實現對文件的一些許可權控制。
現實對pdf文件加
*密,添加水印等。
*/
public class PDFConvertBL
{
//
txt原始文件的路徑
private static final String txtFilePath = "d:/11.txt";

// 生成的pdf文件路徑
private static final String pdfFilePath =
"d:/22.pdf";
// 添加水印圖片路徑
// private static final String
imageFilePath = "D:/33.jpg";
// 生成臨時文件前綴
private static final
String prefix = "tempFile";
// 所有者密碼
private static final String
OWNERPASSWORD = "12345678";

/**
* txt文件轉換為pdf文件
*
* @param txtFile
txt文件路徑
* @param pdfFile pdf文件路徑
* @param userPassWord
用戶密碼
* @param waterMarkName 水印內容
* @param permission
操作許可權
*/
public static void generatePDFWithTxt(String txtFile,
String pdfFile, String userPassWord, String
waterMarkName,
int permission)
{

try
{
// 生成臨時文件
File file =
File.createTempFile(prefix, ".pdf");
//
創建pdf文件到臨時文件
if (createPDFFile(txtFile, file))

{
// 增加水印和加密
waterMark(file.getPath(),
pdfFile, userPassWord, OWNERPASSWORD, waterMarkName, permission);

}
}
catch (Exception e)
{

e.printStackTrace();
}

}

/**
* 創建PDF文檔
*
* @param txtFilePath
txt文件路徑(源文件)
* @param pdfFilePath pdf文件路徑(新文件)
*/
private
static boolean createPDFFile(String txtFilePath, File file)
{

// 設置紙張
Rectangle rect = new Rectangle(PageSize.A4);
//
設置頁碼
HeaderFooter footer = new HeaderFooter(new Phrase("頁碼:",
setChineseFont()), true);

footer.setBorder(Rectangle.NO_BORDER);
// step1
Document
doc = new Document(rect, 50, 50, 50, 50);

doc.setFooter(footer);
try
{
FileReader
fileRead = new FileReader(txtFilePath);
BufferedReader read = new
BufferedReader(fileRead);
// 設置pdf文件生成路徑 step2

PdfWriter.getInstance(doc, new FileOutputStream(file));
//
打開pdf文件 step3
doc.open();
// 實例化Paragraph
獲取寫入pdf文件的內容,調用支持中文的方法. step4
while (read.ready())

{
// 添加內容到pdf(這里將會按照txt文件的原始樣式輸出)

doc.add(new Paragraph(read.readLine(), setChineseFont()));

}
// 關閉pdf文件 step5
doc.close();

return true;
}
catch (Exception e)

{
e.printStackTrace();
return false;

}
}

/**
* 在pdf文件中添加水印
*
* @param inputFile
原始文件
* @param outputFile 水印輸出文件
* @param waterMarkName
水印名字
*/
private static void waterMark(String inputFile, String
outputFile, String userPassWord, String ownerPassWord,

String waterMarkName, int permission)
{
try

{
PdfReader reader = new PdfReader(inputFile);

PdfStamper stamper = new PdfStamper(reader, new
FileOutputStream(outputFile));
// 設置密碼

stamper.setEncryption(userPassWord.getBytes(), ownerPassWord.getBytes(),
permission, false);
BaseFont base =
BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
int total = reader.getNumberOfPages() +
1;
// Image image =
Image.getInstance(imageFilePath);
//
image.setAbsolutePosition(200, 400);
PdfContentByte
under;
int j = waterMarkName.length();
char c =
0;
int rise = 0;
for (int i = 1; i < total;
i++)
{
rise = 500;
under =
stamper.getUnderContent(i);
// 添加圖片
//
under.addImage(image);
under.beginText();

under.setColorFill(Color.CYAN);
under.setFontAndSize(base,
30);
// 設置水印文字字體傾斜 開始
if (j >=
15)
{
under.setTextMatrix(200,
120);
for (int k = 0; k < j;
k++)
{

under.setTextRise(rise);
c =
waterMarkName.charAt(k);
under.showText(c +
"");
rise -= 20;

}
}
else

{
under.setTextMatrix(180, 100);

for (int k = 0; k < j; k++)

{
under.setTextRise(rise);

c = waterMarkName.charAt(k);
under.showText(c +
"");
rise -= 18;

}
}
// 字體設置結束

under.endText();
// 畫一個圓
//
under.ellipse(250, 450, 350, 550);
//
under.setLineWidth(1f);
// under.stroke();

}
stamper.close();
}
catch (Exception
e)
{
e.printStackTrace();
}
}

/**
* 設置中文
*
* @return Font
*/

private static Font setChineseFont()
{
BaseFont base =
null;
Font fontChinese = null;
try

{
base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.EMBEDDED);
fontChinese = new Font(base, 12,
Font.NORMAL);
}
catch (DocumentException e)

{
e.printStackTrace();
}
catch (IOException
e)
{
e.printStackTrace();
}

return fontChinese;
}

public static void main(String[] args)
{

generatePDFWithTxt(txtFilePath, pdfFilePath, "123", "水印文字", 16);

}
}

文章參考一些網路博客稍加修改調試,特此申明
http://hi..com/sx_python/item/15081531ad7d1bc21b96965e

❸ java怎麼解決加密的pdf文件過大問題

不行的話建議可以換一個,有免費版的Spire.PDF for Java來生成和加密PDF文件的效果也都不錯,測試過生成的結果文檔沒出現什麼問題,你可以參考下面的代碼,看能不能解決你的問題:

importcom.spire.pdf.PdfDocument;
importcom.spire.pdf.PdfPageBase;
importcom.spire.pdf.graphics.*;
importcom.spire.pdf.security.PdfEncryptionKeySize;
importcom.spire.pdf.security.PdfPermissionsFlags;

importjava.awt.*;
importjava.awt.geom.Point2D;
importjava.util.EnumSet;

publicclassCreatePDF{
publicstaticvoidmain(String[]args){
//創建PdfDocument對象
PdfDocumentdoc=newPdfDocument();

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

//繪入文字
Stringstr="Java基礎語法 Java保留字:現有Java版本尚未使用,但以後版本可能會作為關鍵字使用。 "+
"byValue、cast、future、generic、inner、operator、outer、rest";
//創建單色畫刷對象
PdfSolidBrushbrush1=newPdfSolidBrush(newPdfRGBColor(Color.BLUE));
//創建TrueType字體對象
PdfTrueTypeFontfont1=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,14),true);
PdfStringFormatformat1=newPdfStringFormat();//創建PDF字元串格式
format1.setAlignment(PdfTextAlignment.Center);//設置文本居中
format1.setLineSpacing(25f);//設置行距
//創建Rectangle2D對象
page.getCanvas().drawString(str,font1,brush1,newPoint2D.Float((float)(page.getActualBounds(true).getWidth()/2),0),format1);

//加密PDF文檔
PdfEncryptionKeySizekeySize=PdfEncryptionKeySize.Key_128_Bit;
StringopenPassword="open";
StringpermissionPassword="permission";
EnumSetflags=EnumSet.of(PdfPermissionsFlags.Print,PdfPermissionsFlags.Fill_Fields);
doc.getSecurity().encrypt(openPassword,permissionPassword,flags,keySize);

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

生成效果如下:

❹ 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 中 pdfbox 怎麼解除加密pdf文件中設置的限制

1、用 Adobe Acrobat Professional就可以對 PDF文件進行加密 步驟 用Adobe Acrobat Professional打開要加密的文件->文件->保全->保全此文件->使用密碼限制開啟或編輯->新建->使用密碼->下一步->輸入策略名稱->下一步->將 要求密碼來開啟文件

熱點內容
淘寶androidsdk 發布:2025-08-22 06:52:04 瀏覽:935
編程掙錢嗎 發布:2025-08-22 06:31:21 瀏覽:1000
敬請存儲 發布:2025-08-22 06:25:42 瀏覽:609
linuxphp7配置 發布:2025-08-22 06:17:01 瀏覽:414
shellftp腳本 發布:2025-08-22 06:11:57 瀏覽:796
sql資料庫打開 發布:2025-08-22 05:58:36 瀏覽:888
伺服器IP怎麼找回 發布:2025-08-22 05:41:28 瀏覽:606
手機百度怎樣上傳視頻 發布:2025-08-22 05:28:08 瀏覽:832
亂碼源碼 發布:2025-08-22 05:26:41 瀏覽:204
c語言中基本的數據類型 發布:2025-08-22 05:24:25 瀏覽:809