-1

I have developed a java mail api program which will send the mail and it also attaches the pdf file , so finally a mail is delivered in which pdf file is attached and that pdf will be password protected so for that i am using itext library

so I have develop this code below

private static final byte[]  USER = "password 1234".getBytes();
private static final byte[] OWNER =  "password 1234".getBytes();


  //  attachment part
             MimeBodyPart attachPart = new MimeBodyPart();
             String filename = "c:\\index.pdf";

             PdfReader reader = new PdfReader(filename);
             PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
             stamper.setEncryption(USER, OWNER,
                     PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
                 stamper.close();
                 reader.close();
             DataSource source = new FileDataSource(filename);
             attachPart.setDataHandler(new DataHandler(source));
             attachPart.setFileName(filename);

but i am getting this below error ,please advise how to proceed

Exception in thread "main" java.io.FileNotFoundException: c:\index.pdf (The requested operation cannot be performed on a file with a user-mapped section open)

now can you please advise i want to make that pdf file as password protected through my java program itself for example i want to modify my below program such as that for opening an pdf file password 1234 is created and whenever an mail is sent the client should open the pdf file but before opening he should enter 1234 in the pop up box of pdf file to see it , can you please advise how can i achieve this in java program itself please. Thanks in advance below is my java program

gfhtg hghg
  • 11
  • 1
  • 7
  • Have you checked **index.pdf** exist in **C** drive ? I doubt it as exception states File is Not found . – Neeraj Jain May 27 '15 at 09:41
  • First thing is not to try to read and write to the same file at the same time. Write to a temporary file, and attach that. – RealSkeptic May 27 '15 at 09:42
  • @RealSkeptic Thanks , can you please update my post that will help to grasp more Thanks inadvance – gfhtg hghg May 27 '15 at 09:43
  • @NeerajJain yeah the file exisits very much – gfhtg hghg May 27 '15 at 09:43
  • I refreshed to make sure, but you are still reading and writing to the same file. – RealSkeptic May 27 '15 at 09:44
  • @RealSkeptic Thanks can you please update the code in my post so i can grasp more thanks in dvance please free feel to make changes – gfhtg hghg May 27 '15 at 09:50
  • That's not how things work on StackOverflow. First, you don't write two questions about the same problem. You update your original question. Second, people are not supposed to edit your question except to make it more easy to understand for other people, and you should be doing that yourself. Please read the [information in the help center](http://stackoverflow.com/help) to understand how to work with StackOverflow. – RealSkeptic May 27 '15 at 09:52
  • @gfhtghghg Clarity is important when asking a question. If my answer solved your problem, please accept it. If not, please elaborate. For instance: if your intention is to send an encrypted PDF by mail, why do you want to write that mail to your file system? Why not create the encrypted PDF in memory? (My answer shows you how that's done.) – Bruno Lowagie May 27 '15 at 09:57

1 Answers1

3

This is wrong:

String filename_src = "c:\\index.pdf";
PdfReader reader = new PdfReader(filename);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));

When you create a new file new FileOutputStream(filename), the file with the path filename will be overwritten. It will be 0 bytes long.

When PdfReader tries to access the file in order to encrypt it, it won't find any file anymore.

It is only normal that you get an exception: you can not read and write the same file at the same time. See How to update a PDF without creating a new PDF?

You need something like this:

String filename_src = "c:\\index.pdf";
String filename_dest = "c:\\index_encrypted.pdf";
PdfReader reader = new PdfReader(filename_src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename_dest));

Or maybe you want to use the new file as an attachment of a mail as you described here: getting error while making a pdf as password protected in java

In that case, you can do this:

String filename_src = "c:\\index.pdf";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(filename_src);
PdfStamper stamper = new PdfStamper(reader, baos);
// Do the encrypting stuff
stamper.close();
byte[] result = baos.toByteArray();

Now you don't create the encrypted PDF on the file system, but you keep it in memory and you can write the bytes straight to your mail attachment.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165