0

I'm writing a program that would allow me to create several PDF Files with a set of given names (from a text file, each name represented by a line in the file). These PDF Files will have a watermark on each page.

Here's the code I'm using:

    public class watermark {    
        public static void main(String[] args) {

            try {
                BufferedReader br = new BufferedReader(new FileReader("D:\\Documents\\java\\listcos.txt"));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }

                try {
                    String a = line;
                    PdfReader Read_PDF_To_Watermark = new PdfReader("Sample.pdf");
                    int number_of_pages = Read_PDF_To_Watermark.getNumberOfPages();
                    PdfStamper stamp = new PdfStamper(Read_PDF_To_Watermark, new FileOutputStream("New_" + line + ".pdf"));
                    int i = 0;
                    Image watermark_image = Image.getInstance("watermark.jpg");
                    watermark_image.setAbsolutePosition(20, 40);
                    PdfContentByte add_watermark;            
                    while (i < number_of_pages) {
                      i++;
                      add_watermark = stamp.getUnderContent(i);
                      add_watermark.addImage(watermark_image);
                    }
                    stamp.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("Done");
            }catch (IOException e) {
                e.printStackTrace();}
        }
    }

I was able to produce a .pdf file but the name I got was "New_null.pdf". Plus, I can only generate one .pdf files. I'd like to know how to generate as many .pdf files as the number of names in the given text file.

Any idea would be greatly appreciated.

Thanks a lot in advance.

Zestos.

Zestos
  • 15
  • 1
  • 7
  • you got null because line has reached the end of the file and is null. you have create a pdf inside that while loop, using the file name from each line. right not you are only creating one pdf, with name = end of file which is null. review your code – MihaiC Dec 09 '14 at 14:56
  • @MihaiC: Thank you very much for pointing that out. I realized that I just need to move the " } " after `while` to the line below `System.out.println("Done");`. – Zestos Dec 09 '14 at 15:06
  • That was it :) Does it work now? – MihaiC Dec 09 '14 at 15:07
  • Yes. It worked great. And the problem of creating multiples Pdf files has also been solved. As the line is printed out, it also created a file with the printed name. Really amazing! – Zestos Dec 09 '14 at 15:09
  • I know it's really silly to ask this question. But how can I accept the answer? – Zestos Dec 09 '14 at 15:10
  • Just click the tick mark next to my answer (under the vote counter) – MihaiC Dec 09 '14 at 15:10

1 Answers1

1

Create the pdf inside the while loop which gets the lines. Right now, line is null because you reached End of File. Move everything in the loop!.

MihaiC
  • 1,618
  • 1
  • 10
  • 14