Simple scenario--how to create new PDF with some text


#1

I have a chunk of plain text (a Java string). I want to create and save a new PDF document with 1 page with this text as content. Not sure of the best way to do this. I could create a Java File with this text then convert that to a PDF? Can I do this in memory with the Document object? I don’t see a way to add a page to an empty Document.


#2

@robertwadehall

Thank you for contacting support.

You may please try using below code snippet and visit product documentation for your kind reference.

String text = "Aspose Text Here";
Document document = new Document();
// Add new page
Page page = document.getPages().add();
// Set page margin as per your requirements
MarginInfo info = new MarginInfo(0,0,0,0);
page.getPageInfo().setMargin(info);
// Add text
TextFragment fragment = new TextFragment(text);
page.getParagraphs().add(fragment);
// Save document
document.save(dataDir + "Aspose_19.10.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.