Converting text files to PDF with whitespace

Is there an option to preserve white space for text converted to PDF? I’ve tried a few methods for the conversion but all of the white space is trimmed (see attached)


Code:

InputStream content = this.getClass().getResourceAsStream("/test.pages.txt");
Document pdf = new Document();
Page page = pdf.getPages().add();
TextFragment text = new TextFragment(IOUtils.toString(content, “UTF-8”));
page.getParagraphs().add(text);
pdf.save(“test.pages.txt.pdf”);

I’ve also tried the constructor:

InputStream content = this.getClass().getResourceAsStream("/test.pages.txt");
Document pdf = new Document(content, new AutoDetectedFormatLoadOptions());
pdf.save(“test.pages.txt.pdf”);

Hi Sid,


Thanks for using our API’s.

I have tested the scenario and I am able to
notice the same problem. For the sake of correction, I have logged this problem
as PDFNEWJAVA-35702 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.

Hi Sid,


Thanks for your patience. We have investigated your above reported issue and would like to suggest you to use Aspose.Words for Java for more flexible formatted text documents as following, it will help you to accomplish the task.

com.aspose.words.Document doc = new
com.aspose.words.Document(“c:/pdftest/test.pages.txt”); <o:p></o:p>

doc.save("c:/pdftest/test.pages.pdf", new PdfSaveOptions());


Best Regards,

We’re converting a large number of documents and the extra conversion step is adding up. Is there a technical reason why whitespace cannot be preserved directly in PDF?

Hi Sid,


The above workaround of using Aspose.Words is shared because Aspose.Pdf for Java is having some issues when transforming TXT files to PDF format. We are sorry for this inconvenience.