DEV Community

carlwils
carlwils

Posted on

Create a Multi-Column PDF in Java

Multi-column PDFs are commonly used in magazines, newspapers, research articles, etc. To get a multi-column PDF, you can first create a multi-column Word document and then convert it to PDF. But in this article, you will learn how to directly create a two-column PDF in Java applications using Free Spire.PDF for Java.

Introduction of the above mentioned API:
https://www.e-iceblue.com/Introduce/free-pdf-for-java.html

Install the Free API (2 Methods)

Method 1: Download the free API and unzip it. Then add the Spire.Pdf.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Create a Two-Column PDF from Scratch

The detailed steps and complet sample code are as follows:

  1. Create a PdfDocument object.
  2. Add a new page in the PDF using PdfDocument.getPages().add() method.
  3. Add a line and set its format in the PDF using PdfPageBase.getCanvas().drawLine() method.
  4. Add text in the PDF at two separate rectangle areas using PdfPageBase.getCanvas().drawString() method.
  5. Save the document to PDF using PdfDocument.saveToFile() method.
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class TwoColumnPDF {
    public static void main(String[] args) throws Exception {

        //Creates a pdf document
        PdfDocument doc = new PdfDocument();

        //Add a new page
        PdfPageBase page = doc.getPages().add();

        //Set location and width
        float x = 0;
        float y = 15;
        float width = 600;

        //Create pen
        PdfPen pen = new PdfPen(new PdfRGBColor(Color.black), 1f);

        //Draw line on the PDF page
        page.getCanvas().drawLine(pen, x, y, x + width, y);

        //Define paragraph text
        String s1 = "Karijini is also a place of important cultural significance to indigenous people, "
                + "with certain locations in the park recognised as women's-only places and others that are strictly for men. "
                + "\"There are places, for instance, women would go if they wanted to get pregnant and "
                + "places they would visit if they wanted to have twins,\" explained anthropologist Dr Amanda Harris, "
                + "who has worked across the Pilbara with traditional owners.";

        String s2 = "Fern Pool, located in a terrarium-like environment at the genesis of Karijijni's Dale's Gorge, "
                + "is an important women's place to the local Banjima people, "
                + "yet unlike other indigenous destinations that prohibit interference, such as Uluru's climbing ban and "
                + "a prohibition on photography at areas within the Kunku-Breakaways near Coober Pedy, "
                + "Karijini traditional owners welcome all guests, asking only that visitors be respectful and walk lightly.";

        //Get width and height of page
        double pageWidth = page.getClientSize().getWidth();
        double pageHeight = page.getClientSize().getHeight();

        //Create solid brush objects
        PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));

        //Create true type font objects
        PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,14));

        //Set the text alignment via PdfStringFormat class
        PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);

        //Draw text
        page.getCanvas().drawString(s1, font, brush, new Rectangle2D.Double(0, 20, pageWidth / 2 - 8f, pageHeight), format);
        page.getCanvas().drawString(s2, font, brush, new Rectangle2D.Double(pageWidth / 2 + 8f, 20, pageWidth / 2 - 8f, pageHeight), format);

        //Save the document
        doc.saveToFile("createTwoColumnPDF.pdf", FileFormat.PDF);
    }
}
Enter fullscreen mode Exit fullscreen mode

MultiColumnPDF

Discussion (0)