Search code examples
javapdfitextjfreechartparagraphs

Using iText to open PDF documents after writing to them


Im using iText and the Document class in a JFrame to write PDFs but if i try to use the Runtime class to run it after creation i get an exception that i cant open it due to the locks still on it and if i run Unlocker on it, my JFrame has a lock token on it. How do i open the PDF if i want to write to it?

Document d = new Document();
.... code
d.close();
Runtime.getRuntime().exec("D:/PDFChartStuff.pdf");

Why does this post not meet stackoverflow's "quality standards"?


Solution

  • If you are using windows you can use rundll32 to launch a pdf file.

    Try something like this

    String pdfFile="D:/PDFChartStuff.pdf";
    if (pdfFile.toString().endsWith(".pdf")) {
                    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfFile);
                } else {
                   //For cross platform use
                    Desktop desktop = Desktop.getDesktop();
                    desktop.open(pdfFile);
                }