【问题标题】:Codenameone: Creating A PDF file代号:创建 PDF 文件
【发布时间】:2018-05-01 12:03:30
【问题描述】:

我一直在尝试使用 Itext 库创建 PDF 文件,

这是我的方法:

private void printPDF() 抛出 FileNotFoundException、DocumentException、IOException {

           OutputStream file = new FileOutputStream(new File("D:\\test.pdf"));
           Document document = new Document();

           PdfWriter.getInstance(document, file);


           document.open();

           document.add(new Paragraph("Your name is "+ l1.getText()));
           document.add(new Paragraph("****************************"));
           document.add(new Paragraph("                            "));
           document.add(new Paragraph("                            "));
           document.add(new Paragraph("Your surname is "+ l2.getText()));
           document.add(new Paragraph("****************************"));
           document.add(new Paragraph("                            "));
           document.add(new Paragraph("                            "));
           document.add(new Paragraph("Your Appointment Date is "+ l3.getText()));
           document.add(new Paragraph("****************************"));
           document.add(new Paragraph("                            "));
           document.add(new Paragraph("                            "));
           document.add(new Paragraph(new Date().toString()));


           document.close();

           file.close();



}

但是,当我运行时,我遇到了多个与 io 相关的错误; “找不到标志 import java.io.File;"/找不到符号 import java.io.FileNotFoundException;/找不到符号 导入 java.io.FileOutputStream;代号一的io有问题吗!!??

请澄清!

【问题讨论】:

  • Ps:我在一个java项目中使用过类似的方法,效果很好!
  • 我已经看到以前的答案推荐使用“FileSystemStorage”,我已经这样做了,它想通了所有..现在我只是很困惑在哪里存储我的文件以便在本地检查它我的笔记本电脑!!??

标签: codenameone


【解决方案1】:

这行不通。您更改了类路径以添加 PdfWriter。这将在桌面上编译,甚至可以在模拟器中运行,但在设备上会失败,因为那里不支持库。

您可以将库包装为 cn1libs,但 jar 可能会使用任意 Java 代码,这可能是个问题,请参阅 https://www.codenameone.com/blog/why-we-dont-support-the-full-java-api.html

我们在the developer guidevideo form 中有一个关于将本机 OS SDK 移植到 Codename One 的教程。

特别是针对您的问题,最好在服务器上生成 PDF 并将其下载到设备上。使用这种方法可以省去很多麻烦。

文件可以存储在StorageFileSystemStorage API 中,两者看起来相似但又不同且不兼容。你可以阅读他们here。如果您使用Storage,则只有一个位置。如果您使用FileSystemStorage,则应用主页是存储您的资料的正确位置。

【讨论】:

    猜你喜欢
    • 2013-03-17
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2013-03-26
    • 2011-06-14
    • 2018-10-12
    • 2011-10-08
    相关资源
    最近更新 更多