Various types of publications such as magazines, newspapers, research articles, etc. use multi-column page layouts. While generating such documents programmatically, you may need to create multi-column PDFs. For such cases, this article covers how to create a multi-column PDF in Java.
Java Library to Create Multi-Column PDF
To create a multi-column PDF, we will use Aspose.PDF for Java. It is a feature-rich PDF manipulation library that lets you create simple as well as complex PDF files from scratch.
You can either download the library’s JAR or install it within your Java application using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.12</version>
</dependency>
Create a Multi-Column PDF in Java
Aspose.PDF for Java makes it quite easier for you to create a multi-column PDF file. The following are the steps to achieve this.
- First, create a new Document object.
- Then, set the page margins using Document.getPageInfo().getMargin().setLeft() and Document.getPageInfo().getMargin().setRight() methods.
- Add a new page in the PDF using Document.getPages().add() method and get its reference into a Page object.
- Create a new Graph object and add it to the paragraph collection using Page.getParagraphs().add() method.
- Create a new Line and add it to the Graph.getShapes() collection.
- Add heading text to the page using Page.getParagraphs().add() method.
- Create a new FloatingBox and specify the number of columns, column spacing, and column width.
- Create another Graph object.
- Add a new Line to the Graph.getShapes() collection.
- Add Graph to the FloatingBox.getParagraphs() collection.
- Create a new TextFragment and add it to FloatingBox.getParagraphs() collection.
- Add FloatingBox to the page using Page.getParagraphs().add() method.
- Finally, save the PDF file using Document.save(string) method.
Java Code to Create Multi-Column PDF
The following code sample shows how to create a multi-column PDF in Java.
The following is the screenshot of the multi-column PDF generated by the above code sample.
Free Java PDF Library
You can get a free temporary license to create multi-column PDF files without any limitations.
Explore Java PDF Library
You can learn more about the Java PDF library using the documentation. In case you would have any questions, feel free to ask via our forum.
Conclusion
Multi-column layout in PDF documents is used for various types of publications. In this post, you have learned how to create multi-column PDF files from scratch using Java. The step-by-step guide and code sample have shown how to easily generate a multi-column PDF in a Java application.