/*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/ package part3.chapter12; import java.io.FileOutputStream;
import java.io.IOException; import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper; public class EncryptionPdf {
/** User password. */
public static byte[] USER = "Hello".getBytes();
/** Owner password. */
public static byte[] OWNER = "World".getBytes(); /** The resulting PDF file. */
public static final String RESULT1
= "results/part3/chapter12/encryption.pdf";
/** The resulting PDF file. */
public static final String RESULT2
= "results/part3/chapter12/encryption_decrypted.pdf";
/** The resulting PDF file. */
public static final String RESULT3
= "results/part3/chapter12/encryption_encrypted.pdf"; /**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
writer.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
writer.createXmpMetadata();
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
// step 5
document.close();
} /**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @param dest the resulting PDF
* @throws IOException
* @throws DocumentException
*/
public void decryptPdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src, OWNER);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();
} /**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @param dest the resulting PDF
* @throws IOException
* @throws DocumentException
*/
public void encryptPdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.setEncryption(USER, OWNER,
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
stamper.close();
reader.close();
} /**
* Main method.
*
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args) throws IOException, DocumentException {
EncryptionPdf metadata = new EncryptionPdf();
metadata.createPdf(RESULT1);
metadata.decryptPdf(RESULT1, RESULT2);
metadata.encryptPdf(RESULT2, RESULT3);
}
}

http://itextpdf.com/examples/iia.php?id=219

itext Protecting your PDF的更多相关文章

  1. 使用iText库创建PDF文件

    前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...

  2. flying-saucer + iText + Freemarker实现pdf的导出, 支持中文、css以及图片

    前言 项目中有个需求,需要将合同内容导出成pdf.上网查阅到了 iText , iText 是一个生成PDF文档的开源Java库,能够动态的从XML或者数据库生成PDF,同时还可以对文档进行加密,权限 ...

  3. IText实现对PDF文档属性的基本设置

    一.Itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  4. 解决部分浏览器不能显示itext生成的PDF文件文本域内容问题

    利用Itext可以实现pdf的高效动态生成,但在实践过程中遇到了一个问题: 即itext利用map中的值设置到pdf模板上建立的文本域中时:能成功生成,但是在部分浏览器上(360,QQ,等浏览器)无法 ...

  5. [Web Pdf] flying-saucer + iText + Freemarker生成pdf 跨页问题

    转载于: https://blog.csdn.net/qq_31980421/article/details/79662988 flying-saucer + iText +  Freemarker实 ...

  6. C# iText 7 切分PDF,处理PDF页面大小

    一.itext 我要使用itext做一个pdf的页面大小一致性处理,然后再根据数据切分出需要的pdf. iText的官网有关于它的介绍,https://itextpdf.com/ 然后在官网可以查找a ...

  7. iText实现导出pdf文件java代码实现例子

    ///////////////////////////////////主类////////////////////////////////////////// package com.iText; i ...

  8. iText C# 合并PDF文件流,以及A5变A4时内容默认放在最底下的问题的解决方法;ASP.NET 实现Base64文件流下载PDF

    /// <summary> 合併PDF檔(集合) </summary> /// <param name="files">欲合併PDF檔之集合(一 ...

  9. 使用itext直接替换PDF中的文本

    直接说问题,itext没有直接提供替换PDF中文本的接口(查看资料得到的结论是PDF不支持这种操作),不过存在解决思路:在需要替换的文本上覆盖新的文本.按照这个思路我们需要解决以下几个问题: itex ...

随机推荐

  1. oracle开发学习篇之集合函数

    集合函数; declare type list_nested ) not null; v_all list_nested := list_nested('changan','hubei','shang ...

  2. BTSync FREE vs BTSync PRO

    Although both BitTorrent Sync 2.0 FREE and PRO ensure high file transfer speed and ultimate security ...

  3. SpringMVC怎么获取前台传来的数组

    var tollerlist = new Array(); for(var k in objToller){ tollerlist.push(k); } $.ajax({ type:"pos ...

  4. java中的几种对象(PO,VO,DAO,BO,POJO)

    一.PO :(persistant object ),持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生成PO是不错的选择. 二.VO :(value object) ...

  5. My安装EclipseJS代码提示(Spket插件)

    最近须要大量使用JS来开发,可是MyEclipse2014自带的JS编辑器没有代码提示的功能,开发效率有点低,所以安装了一个Spket的插件,过程很easy,SVN插件的安装比这个更简单. Spket ...

  6. list C++实现

    模仿STL中list,实现了其大部分功能.list可以高效地利用内存资源,常数时间的插入删除操作.并且,list除了erase外,不怎么存在迭代器失效的现象. #include<iostream ...

  7. java基础学习总结——多态(动态绑定)

    一.面向对象最核心的机制——动态绑定,也叫多态

  8. MVC无限级分类01,分层架构,引入缓存,完成领域模型与视图模型的映射

    本系列将使用zTree来创建.编辑关于品牌.车系.车型的无限级分类,使用datagrid显示,源码在github.先上最终效果: datagrid显示所有记录.分页,提供添加.修改.删除按钮,并提供简 ...

  9. java内存溢出示例(堆溢出、栈溢出)

    堆溢出: /** * @author LXA * 堆溢出 */ public class Heap { public static void main(String[] args) { ArrayLi ...

  10. quartz 中JobExecutionContext的使用

    假如execute方法中需要一些额外的数据怎么办?比如说execute 中希望发送一封邮件,但是我需要知道邮件的发送者.接收者等信息? 存在两种解决方案: 1.JobDataMap类:    每个Jo ...