-1

I need to display a report rendered in PDF format in JSP but the PDF should be printable if you are accessing the report via web. The report also allows the user to save the report as PDF format, but the saved PDF should not be allowed to be printed.

Please help me. Currently we can save the report as PDF but we cannot print it and when the report is accessed via web, the report cannot be printed.

I am using NetBeans and GlassFish

Goals:

  1. Save the report but report is not allowed to be printed.
  2. Print the report if it is accessed via web.
sshine
  • 15,635
  • 1
  • 41
  • 66
mitch_teno
  • 29
  • 2
  • 7

1 Answers1

0

You might need to set permissions for your PDF. In your code during the creation of the PDF you should add something like this:

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setEncryption("Hello".getBytes(), "World".getBytes(),
        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
    stamper.close();
    reader.close();
}

My answer was based on examples found here.

EDIT: Also you might find this question useful, in it they provided this solution:

PdfReader reader = new PdfReader("my-old-file.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("my-new-file.pdf"));
stamper.setEncryption("my-owner-password".getBytes(), "my-user-password".getBytes(),
PdfWriter.AllowPrinting | PdfWriter.AllowCopy, PdfWriter.STRENGTH40BITS);
stamper.close();

But someone stated that in Adobe Reader 9.3 the printing option is not being properly disabled. I have had some unexpected behavior with latest versions of Adobe Reader so give the code a try and see what happens.

Community
  • 1
  • 1
SantLev
  • 140
  • 3
  • 15
  • thank you for the reply, yes. currently we are using the iText library. But we can only disable print button and save the file as pdf. but we also need to enable print button on the web application and disable the print button on the saved pdf file. – mitch_teno Dec 02 '15 at 13:18
  • I edited the answer so that now it contains the information that I wanted to provide, thank you for your observations. – SantLev Dec 02 '15 at 14:55
  • Thank you for the reply. I tried using the code provided above, the PDF was allowed to be printed when accessed via web application but when i tried saving it to PDF format, the PDF also has the facility to print. Is it possible to be able to do this? – mitch_teno Dec 03 '15 at 01:47
  • Try removing the " PdfWriter.ALLOW_PRINTING| " and opening without the owner password (that means, open it with the user password or without password at all if you didn't set a user password). – SantLev Dec 03 '15 at 12:19