简体   繁体   English

如何从方法生成后打开PDF文件

[英]How to open PDF file after being generated from a method

I need your help in opening the generated PDF file automatically after it is being written from a bean. 在从bean编写生成的PDF文件后,我需要您的帮助。 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. 我使用iText库来编写PDF文件,在下面的方法中,我能够生成PDF,但我不知道如何为用户动态打开它。

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? 我希望PDF文件在写入后显示给用户以允许他打印文件,那么我该怎么做呢?

You can just open the pdf file in the default pdf viewer installed in on the user's machine. 您只需在用户计算机上安装的默认pdf查看器中打开pdf文件即可。

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.) (Adobe Reader,Nitro PDF等)

Once the file is opened in the default viewer, the user will be able to print it by pressing ctrl + P 在默认查看器中打开文件后,用户可以通过按ctrl + P进行打印

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM