Codenameone:创建PDF文件

时间:2018-05-01 01:03:41

标签: codenameone

我一直在尝试使用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; /找不到符号 import java.io.FileOutputStream;有代号的io有什么问题!! ??

plz澄清!

1 个答案:

答案 0 :(得分:1)

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

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

我们有一个教程,将原生OS SDK移植到the developer guidevideo form中的Codename One。

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

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