iText文档获取URL

huangapple go评论83阅读模式
英文:

iText Document get URL

问题

这生成了项目根文件夹中的 test.pdf。现在我需要获取该文件的 java.net.URL。我该如何获取?

英文:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));

document.open();
Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
Chunk chunk = new Chunk("Hello World", font);

document.add(chunk);
document.close();

This generates test.pdf on root folder of my project. Now I need a java.net.URL of that file. How can I get that?

答案1

得分: 1

我相信这样的方法可能会起作用:

File output_file = new File("test.pdf");

// 将第2行更改为:
PdfWriter.getInstance(document, new FileOutputStream(output_file));

// 要获取 java.net.URL
URL url = output_file.toURI().toURL();
英文:

I believe something like this could work:

File output_file = new File("test.pdf");

//Change Line 2 to:
PdfWriter.getInstance(document, new FileOutputStream(output_file));

//To get the java.net.URL
URL url = output_file.toURI().toURL();

huangapple
  • 本文由 发表于 2020年8月10日 17:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定