gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:42 24 4
gpt4 key购买 nike

在从 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

打印它

关于java - 从方法生成后如何打开PDF文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31368253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com