Product:
Product Version: 10.2 Java SDK
I want to convert a java string which is in-memory into a PDF. This string is multi line containing “\n”. Can I convert this into a PDF. I want the result PDF to just contain text and it can be multiple pages as well.
I checked the documentation. Text to PDF only has input file. I unfortunately dont have it in file input. but an inputStream of text content.
Hello. Yes, you can convert an in-memory text file to PDF with the Apryse SDK using the streamingPdfConversion function to convert your string. Here is a Java example for you what will convert a TXT byte array to a PDF document:
public static void ConvertTextInMemory(String InputFile, String Extension) {
PDFNet.initialize("[YOUR LICENSE KEY HERE]");
try {
Path path = Paths.get(InputFile);
byte[] fileData = Files.readAllBytes(path);
MemoryFilter memoryFilter = new MemoryFilter(fileData.length, false);
FilterWriter writer = new FilterWriter(memoryFilter);
writer.writeBuffer(fileData);
writer.flush();
memoryFilter.setAsInputFilter();
ConversionOptions options = new ConversionOptions();
options.setFileExtension(Extension);
DocumentConversion documentConversion = Convert.streamingPdfConversion(memoryFilter, options);
while (documentConversion.getConversionStatus() == DocumentConversion.e_incomplete) {
documentConversion.convertNextPage();
}
if (documentConversion.getConversionStatus() == DocumentConversion.e_success) {
PDFDoc out_doc = documentConversion.getDoc();
out_doc.save("output.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
} else {
System.out.println("Converison failed");
}
} catch (PDFNetException e) {
System.out.println(e);
}
catch (Exception e) {
System.out.println(e);
}
}