3

I need your help in opening the generated PDF file automatically after it is being written from a bean. I used iText libraries to write a PDF file and in the below method, I am able to generate the PDF, but I do not know how to open it on the fly for the users.

public static void main(String[] args) throws DocumentException, FileNotFoundException, IOException {

     Document document = new Document(PageSize.A4, 50, 50, 50, 50);

     document.open();

    BaseFont bf = BaseFont.createFont(
        "c://windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font font = new Font(bf, 25);
    PdfPTable table = new PdfPTable(1);
    PdfPCell  cell = new PdfPCell(new Phrase("Good Morning", font));
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
    table.addCell(cell);
    document.add(table);

    document.close();      

 }

I want the PDF file to be shown to the user after it is being written to allow him to print the file, so how can I do this?

99maas
  • 1,239
  • 12
  • 34
  • 59
  • Desktop.getDesktop().open(yourfile) ? – Ueli Hofstetter Jul 12 '15 at 13:31
  • @chuchichaeschtli No I don't want it to be saved first of all, I need only to open it once it is generated without saving it – 99maas Jul 12 '15 at 15:20
  • @chuchichaeschtli Please refer to the updated post. Also, is there a way to show the dialog box for Saving or opening the PDF file? – 99maas Jul 12 '15 at 15:26
  • 1
    So you have an application installed on the user's computer (after all you use the `main` method) and want to show a PDF without saving it, i.e. one which exists only in memory. AFAIK that is not possible using standard PDF viewers existing on the user's computer. You could try a java PDF viewer component instead, e.g. jPedal. – mkl Jul 12 '15 at 15:55
  • @mkl Thanks mkl for the explanation now I got the point – 99maas Jul 12 '15 at 16:51

1 Answers1

4

You can just open the pdf file in the default pdf viewer installed in on the user's machine.

Use this.

Desktop.getDesktop().open(new File("path/to/pdf"));

The file will be opened in the application whichever is installed on the target machine. (Adobe Reader, Nitro PDF, etc.)

Once the file is opened in the default viewer, the user will be able to print it by pressing ctrl + P

Shrinivas Shukla
  • 4,325
  • 2
  • 21
  • 33
  • The above line will open the PDF file, if it is saved on the Desktop. My requirement is a little bit different, once I write the PDF and reaches the document.close(); I need to to open the PDF file automatically without Saving it in the user machine similar to generating it on the fly and showing it to the user – 99maas Jul 12 '15 at 15:25
  • Or is there a way to show the dialog box for Saving or opening the PDF file? – 99maas Jul 12 '15 at 15:27
  • What you need is PDF renderer OR PDF viewer. This has already been answered [here](http://stackoverflow.com/a/4437985/4338417). – Shrinivas Shukla Jul 12 '15 at 16:56
  • Actually I am using Primefaces and I am new to it and this is my first time that I am writing a PDF file and I do not know how to display it on the browser. – 99maas Jul 12 '15 at 23:10