从方法生成后如何打开PDF文件
How to open PDF file after being generated from a method
我需要你的帮助来自动打开从 bean 写入生成的 PDF 文件。我使用 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();
}
我希望 PDF 文件在写入后显示给用户以允许他打印文件,那么我该怎么做呢?
您只需在用户计算机上安装的默认 pdf 查看器中打开 pdf
文件即可。
使用这个。
Desktop.getDesktop().open(new File("path/to/pdf"));
文件将在目标计算机上安装的应用程序中打开。 (Adobe Reader、Nitro PDF 等)
在默认查看器中打开文件后,用户可以通过按 ctrl + P[= 来打印它12=]
我需要你的帮助来自动打开从 bean 写入生成的 PDF 文件。我使用 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();
}
我希望 PDF 文件在写入后显示给用户以允许他打印文件,那么我该怎么做呢?
您只需在用户计算机上安装的默认 pdf 查看器中打开 pdf
文件即可。
使用这个。
Desktop.getDesktop().open(new File("path/to/pdf"));
文件将在目标计算机上安装的应用程序中打开。 (Adobe Reader、Nitro PDF 等)
在默认查看器中打开文件后,用户可以通过按 ctrl + P[= 来打印它12=]