0
votes

i've been trying to create a PDF file using Itext library,

this was my method:

private void printPDF() throws 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();



}

however when i run i got multiple errors related to io; "cannot find symbol import java.io.File;"/ cannot find symbol import java.io.FileNotFoundException;/ cannot find symbol import java.io.FileOutputStream; is there any problem with io in codename one!!??

plz clarify !

1
Ps: i've used a similar approach in a java project and it worked brilliantly! - Chams Mansouri
i have seen a previous answer recommending using the "FileSystemStorage", i have done so and it figured out all..now i'm only confused where to store my file in order to check it locally on my laptop!!?? - Chams Mansouri

1 Answers

2
votes

This won't work. You changed the classpath to add PdfWriter. This will compile on the desktop and will even run in the simulator but will fail on the device as the library wouldn't be supported there.

You can wrap libraries as cn1libs but a jar might use arbitrary Java code which might be a problem see https://www.codenameone.com/blog/why-we-dont-support-the-full-java-api.html

We have a tutorial on porting native OS SDK's to Codename One in the developer guide and in video form.

Specifically to your questions it's best to generate the PDF on the server and download it to the device. You'd save yourself a lot of headaches with that approach.

Files can be stored either in the Storage or FileSystemStorage API's both look similar but are different and incompatible. You can read about them here. If you use Storage there is only one place. If you use FileSystemStorage the app home is the right place to store your stuff.