0

Below code is for writing pdf file;

BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontSelector fs = new FontSelector();
fs.addFont(new Font(bf));                
String fileName = filePath3 + "//DEVIATION_REPORT.pdf";
OutputStream file = new FileOutputStream(new File(fileName));
Font smallFont = new Font(Font.FontFamily.COURIER, 6, Font.NORMAL);
Font headerFont = new Font(Font.FontFamily.HELVETICA, 7, Font.BOLD);
Font tabFont = new Font(Font.FontFamily.HELVETICA, 5, Font.NORMAL);
Font rusFont = new Font(bf, 5);                
Font blueFont = new Font(bf, 5);
blueFont.setColor(BaseColor.BLUE);                
Font redFont = new Font(bf, 5);
redFont.setColor(BaseColor.RED);

String comType;
if (compType == 2)
   comType = "Character";
else 
   comType = "Word";

Document doc = new Document();
PdfWriter.getInstance(doc, file);                
doc.open();            

Image image1 = Image.getInstance(cmdpath + "ScRp.jpg");
image1.setAlignment(Element.ALIGN_LEFT);

Paragraph prg = new Paragraph("Compare", smallFont);
prg.setAlignment(Element.ALIGN_RIGHT);
doc.add(image1);
doc.add(prg);
doc.add(new Paragraph("__________________________________________________"));
doc.add(new Paragraph("Passed Report"));
doc.add(new Paragraph(" "));

PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Paragraph("No", headerFont));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setPaddingBottom(5);                
PdfPCell cell2 = new PdfPCell(new Paragraph("Details", headerFont));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setPaddingBottom(5);             
table.addCell(cell1);
table.addCell(cell2);
doc.add(table);
doc.newPage();
doc.close();
file.close();

How do I do if i want to make the pdf is non editable. I've tried to use

 PdfEncryptor.encrypt(  
 new PdfReader(fileName),  
 new FileOutputStream("HelloWorldProtected.pdf"),  
 null,  
 "StrongPassword".getBytes(),  
 PdfWriter.AllowPrinting,  
 PdfWriter.STRENGTH128BITS);  

but compiler stops here. I waited more than 15 minutes but nothing happen.

I even make file.setreadOnly(), but it doesn't user to save it after edit. It still allow user to edit. User can Save AS the document and replace with the non editable one.

Is there any other way we can make the file as non editable. Please advice.

Nitesh Verma
  • 1,795
  • 4
  • 27
  • 46
chinna_82
  • 6,353
  • 17
  • 79
  • 134

1 Answers1

0

You might want to use iText for such kind of work if its fine with your project. I cant see any other option as if now. I have personally used iText and it is quite robust. let me know if this helps.

Edit:

  1. Encryption settings
  2. http://viralpatel.net/blogs/password-protect-pdf-itext-java/
  3. http://www.jarfinder.com/index.php/java/info/com.lowagie.text.pdf.interfaces.PdfEncryptionSettings
  4. Try searching more on google and SO

EDIT: The long waiting of 15 min. problem might be of file write permissions on your file path.

Nitesh Verma
  • 1,795
  • 4
  • 27
  • 46
  • appreciated if you could share your working code on the readonly part. – chinna_82 Aug 21 '13 at 07:09
  • as mentioned as my question.. when I do `PdfWriter rptWritter = PdfWriter.getInstance(doc, file); rptWritter.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);` debugger stops here for long time. There is no respond even I waited 15minutes. – chinna_82 Aug 21 '13 at 07:57
  • The long waiting of 15 min. problem might be of file write permissions on your file path. – Nitesh Verma Aug 22 '13 at 06:09
  • sorry forget the update..the long wait is because the jar file. Once i add the required jar file. It working fine. There is one issue, the user still can save as the pdf. Any idea how i can stop this? – chinna_82 Aug 22 '13 at 06:58
  • Could you be more clear "the user still can save as the pdf" I did.nt get you on that. – Nitesh Verma Aug 22 '13 at 08:21
  • the pdf file..even though protected with password and user cannot edit the contents. They still can save as the pdf as another copy with same name in different location and they able to edit after they save as. – chinna_82 Aug 22 '13 at 09:49