Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active October 8, 2021 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/6bdeaa06a31c5d5a2532c34e9c23d110 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/6bdeaa06a31c5d5a2532c34e9c23d110 to your computer and use it in GitHub Desktop.
Create Multi-Column PDF Documents using Java
// Create a new document
Document doc = new Document();
// 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);
// Add a new page and get its reference
Page page = doc.getPages().add();
// Create a new graph
com.aspose.pdf.drawing.Graph graph1 = new com.aspose.pdf.drawing.Graph(500, 2);
// Add the graph to paraphraphs collection
page.getParagraphs().add(graph1);
// Add a line to the graph
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 variable with text containing HTML content
String s = "<span style=\"font-family: \"Times New Roman\", Times, serif;\" font-size=\"14pt\" \">"
+"<strong> How to Steer Clear of money scams</<strong> </span>";
// Create text fragment and initialize it with HTML text
HtmlFragment heading_text = new HtmlFragment(s);
page.getParagraphs().add(heading_text);
// Create a floating box
FloatingBox box = new FloatingBox();
// Add columns in the section
box.getColumnInfo().setColumnCount(2);
// Set the spacing between the columns
box.getColumnInfo().setColumnSpacing("5");
// Set column width
box.getColumnInfo().setColumnWidths("105 105");
// Create a new text fragment
TextFragment text1 = new TextFragment("By A Googler (The Official Google Blog)");
text1.getTextState().setFontSize (8);
text1.getTextState().setLineSpacing (2);
text1.getTextState().setFontSize (10);
text1.getTextState().setFontStyle (FontStyles.Italic);
// Add text to paragraph
box.getParagraphs().add(text1);
// Create a graph 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 };
// Create a line
com.aspose.pdf.drawing.Line l2 = new com.aspose.pdf.drawing.Line(posArr2);
// Add line to graph
graph2.getShapes().add(l2);
// Add the line to paragraphs collection of section object
box.getParagraphs().add(graph2);
// Create a new text fragment to set content of the document
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.");
// Add text to the floating box
box.getParagraphs().add(text2);
// Add floating box to the page
page.getParagraphs().add(box);
// Save PDF file
doc.save("multicolumn-pdf.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment