Originariamente inviata da
andbin
È per qualcosa di "didattico" o per qualcosa di più reale e professionale? Nel primo caso, devi essere tu a dover scrivere del codice per fare la conversione? Oppure ti basta usare librerie e/o tools già fatti?
Comunque hai già provato a cercare in rete "java rtf to pdf" ?
In realtà ho provato ad usare questo metodo, ma mi restituisce NullPointerException
codice:
String inputFile = "C:/Users/utente/Documents/prova.rtf";
String outputFile = "C:/Users/utente/Documents/sample_converted.pdf";
URL resource = getClass().getResource("/it/files/prova.rtf");
File propFile = Paths.get(resource.toURI()).toFile();
System.out.println(propFile.getPath());
// create a new document
try {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
System.out.println("open the document for modifications\n");
// open the document for modifications
document.open();
// create a new parser to load the RTF file
System.out.println("create a new parser to load the RTF file\n");
RtfParser parser = new RtfParser(null);
// read the rtf file into a compatible document
System.out.println("read the rtf file into a compatible document\n");
System.out.println((document != null));
System.out.println((parser != null));
parser.convertRtfDocument(new FileInputStream(propFile.getPath()), document);
// save the pdf to disk
System.out.println(" save the pdf to disk\n");
document.close();
System.out.println("Finished");
return true;
} catch (DocumentException ex) {
System.out.println("document");
ex.printStackTrace();
} catch (FileNotFoundException e2) {
System.out.println("file not found");
e2.printStackTrace();
} catch (IOException e3) {
System.out.println("io exception");
e3.printStackTrace();
}
return false;
praticamente termina prima di eseguire questa riga:
codice:
parser.convertRtfDocument(new FileInputStream(propFile.getPath()), document);