Aspose PDF Java - Create PDF from scratch

Hi guys,


Id like to ask how I can create a PDF from scratch? Created PDF contains text in 2 columns and an image at the bottom. I have the text values and image directory stored in string variables already, but I do not know how to create a PDF from them.

Regards,

Hi Junmil,


Thanks for your interest in our API’s.

In order to add text in two columns, please try using following code snippet. For your reference, I have also attached the output generated over my end.

[Java]

com.aspose.pdf.Document doc = new
com.aspose.pdf.Document();<o:p></o:p>

// specify the left margin info for the PDF file

doc.getPageInfo().getMargin().setLeft(40);

// specify the Right margin info for the PDF file

doc.getPageInfo().getMargin().setRight(40);

Page page = doc.getPages().add();

com.aspose.pdf.drawing.Graph graph1 = new com.aspose.pdf.drawing.Graph(500, 2);

// Add the line to paraphraphs collection of section object

page.getParagraphs().add(graph1);

//specify the coordinates for the line

float[] posArr = new float[] { 1, 2, 500, 2 };

com.aspose.pdf.drawing.Line l1 = new com.aspose.pdf.drawing.Line(posArr);

graph1.getShapes().add(l1);

//Create string variables with text containing html tags

String s = "" +

" How to Steer Clear of money scams</ "

+ "";

//Create text paragraphs containing HTML text

com.aspose.pdf.HtmlFragment heading_text = new com.aspose.pdf.HtmlFragment(s);

page.getParagraphs().add(heading_text);

com.aspose.pdf.FloatingBox box = new com.aspose.pdf.FloatingBox();

//Add four columns in the section

box.getColumnInfo().setColumnCount(2);

//Set the spacing between the columns

box.getColumnInfo().setColumnSpacing("5");

box.getColumnInfo().setColumnWidths("105 105");

TextFragment text1 = new TextFragment("By A Googler (The Official Google Blog)");

text1.getTextState().setFontSize(8);

text1.getTextState().setLineSpacing(2);

box.getParagraphs().add(text1);

text1.getTextState().setFontSize(10);

text1.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);

// Create a graphs object to draw a line

com.aspose.pdf.drawing.Graph graph2 = new com.aspose.pdf.drawing.Graph(50, 10);

// specify the coordinates for the line

float[] posArr2 = new float[] { 1, 10, 100, 10 };

com.aspose.pdf.drawing.Line l2 = new com.aspose.pdf.drawing.Line(posArr2);

graph2.getShapes().add(l2);

// Add the line to paragraphs collection of section object

box.getParagraphs().add(graph2);

TextFragment text2 = new TextFragment("Sed augue tortor, sodales id, luctus et, pulvinar ut, eros. Suspendisse vel dolor. Sed quam. Curabitur ut massa vitae eros euismod aliquam. Pellentesque sit amet elit. Vestibulum interdum pellentesque augue. Cras mollis arcu sit amet purus. Donec augue. Nam mollis tortor a elit. Nulla viverra nisl vel mauris. Vivamus sapien. nascetur ridiculus mus. Nam justo lorem, aliquam luctus, sodales et, semper sed, enim Nam justo lorem, aliquam luctus, sodales et,nAenean posuere ante ut neque. Morbi sollicitudin congue felis. Praesent turpis diam, iaculis sed, pharetra non, mollis ac, mauris. Phasellus nisi ipsum, pretium vitae, tempor sed, molestie eu, dui. Duis lacus purus, tristique ut, iaculis cursus, tincidunt vitae, risus. Sed commodo. *** sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam justo lorem, aliquam luctus, sodales et, semper sed, enim Nam justo lorem, aliquam luctus, sodales et, semper sed, enim Nam justo lorem, aliquam luctus, sodales et, semper sed, enim nAenean posuere ante ut neque. Morbi sollicitudin congue felis. Praesent turpis diam, iaculis sed, pharetra non, mollis ac, mauris. Phasellus nisi ipsum, pretium vitae, tempor sed, molestie eu, dui. Duis lacus purus, tristique ut, iaculis cursus, tincidunt vitae, risus. Sed commodo. *** sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed urna. . Duis convallis ultrices nisi. Maecenas non ligula. Nunc nibh est, tincidunt in, placerat sit amet, vestibulum a, nulla. Praesent porttitor turpis eleifend ante. Morbi sodales.nAenean posuere ante ut neque. Morbi sollicitudin congue felis. Praesent turpis diam, iaculis sed, pharetra non, mollis ac, mauris. Phasellus nisi ipsum, pretium vitae, tempor sed, molestie eu, dui. Duis lacus purus, tristique ut, iaculis cursus, tincidunt vitae, risus. Sed commodo. *** sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed urna. . Duis convallis ultrices nisi. Maecenas non ligula. Nunc nibh est, tincidunt in, placerat sit amet, vestibulum a, nulla. Praesent porttitor turpis eleifend ante. Morbi sodales.");

box.getParagraphs().add(text2);

page.getParagraphs().add(box);

//Save the Pdf

doc.save("c:/pdftest/Two_COlumnDocument.pdf");

Hello codewarior is not online. Last active: Dec 28, 2015, 11:08 AMNayyer,


Thanks so much for your sample codes, I’m currently tying and modifying it to create the expected PDF format for my application.

Hi Junmil,


Thanks for sharing the feedback.

Please continue using our API’s and in the event of any further query, please feel free to contact.