【发布时间】:2013-04-03 10:12:39
【问题描述】:
我需要使用 Java 将一些文本写入 PDF 文档。我为此写了一个代码。但我无法打开文件。如果你们有什么想法,请与我分享。
public class WritPDF
{
public static void main(String[] args)
{
Writer writer = null;
try
{
String text = "This is a text file";
File file = new File("C:/Users/PrinterTest/Hi1.pdf");
writer = new BufferedWriter(new FileWriter(file));;
writer.write(text);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (writer != null)
{
}
} catch (IOException e)
{}
}
}
}
【问题讨论】:
-
PDF 不是文本文件,您无法以这种方式打开它。 POI 是个好东西,另一种可能是使用 Velocity 模板
-
PDF 不是纯文本格式。使用您的代码,它只会创建普通的文本文件。您需要使用 PDF 库,例如 iText 或 Apache FOP。还有更多这样的库可用。
-
Apache POI 用于处理 Microsoft Office 文档,而不是 PDF 文件。
标签: java