javaweb在线打开pdf去掉右键打印按钮

之前找了好久没有找到好的办法,今天无意间看到的博客,解决了我的问题,这下也不用借助第三方pdf阅读器了,使用adobe reader 就可以在线打开pdf了,项目在一定程度上节省了费用。

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfEncryptor;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfHandle {
	
	public void hideBars(String inputFile, String outFile) {
		 //复制一个PDF
		// 创建一个reader
        try {
			PdfReader reader =new PdfReader(inputFile);
			int n = reader.getNumberOfPages();
			// 得到第一页
            Rectangle psize = reader.getPageSize(1);
            float width = psize.getHeight();
            float height = psize.getWidth();
            // step 1: 创建一个document对象
            Document document =new Document(new Rectangle(width, height));
         // step 2: 创建一个write
            PdfCopy writer =new PdfCopy(document,new FileOutputStream(outFile));
          //设置隐藏菜单栏和工具栏
            writer.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
            // step 3: 打开 document
            document.open();
            // step 4:一页一页添加内容
            int i = 0;
            while (i < n) {
                document.newPage();
                i++;
                PdfImportedPage page1 = writer.getImportedPage(reader, i);
                 writer.addPage(page1);
            }
            // step 5: 关闭document
            
            document.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	 public void notAllowPrint(String inputFile,String outFile)
	    {
	       try {
	           PdfReader reader =new PdfReader(inputFile);
	           //设置加密权限
	           PdfEncryptor.encrypt(reader,
	                  new FileOutputStream(outFile),
	                  null,
	                  null,
	                   PdfWriter.AllowAssembly |PdfWriter.AllowFillIn|PdfWriter.AllowScreenReaders,
	                   false);
	       }
	        catch(Exception e) {
	           e.printStackTrace();
	       }
	    }
	    public static void main(String args[])
	    {
	        PdfHandle pp=new PdfHandle();
	        pp.hideBars("e://3.pdf","e://4.pdf");
	        pp.notAllowPrint("e://4.pdf","e://5.pdf");
	    }
	   
	 
}

经过测试确实是OK,项目中需要的jar包有:itextpdf-5.3.2.jar 、bcprov-jdk15on-1.47.jar、bcpkix-jdk15on-1.47.jar、bcmail-jdk15on-1.47.jar

参考https://blog.csdn.net/tanguang_honesty/article/details/8530696 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值