Monday, 23 November 2020

Create a PDF document in Java using a free API

Usually, we can get a PDF document only by converting other document formats. Here, I will introduce a method to directly create a PDF document with a head, paragraph and image, which is that using Java code to do it programmatically.

It is worth noting that I used a free API called Free Spire.PDF for Java in this tutorial. It enables Java applications to create, read, convert and save PDF documents without installing Adobe Acrobat.

Before running codes, please download the Free Spire.PDF for Java package from the link, unzip it and find Spire.Pdf.jar in the “lib” folder. Finally just manually add the jar file to your project.

Of course, if you’re creating a Maven project, just add the following configuration into the pom.xml file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Using the code

After finishing the steps above, the next thing you need to do is following the code snippets in order to

generate a PDF document.

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 CreateAPdf {
public static void main(String[] args) {
//create a PdfDocument object
PdfDocument doc = new PdfDocument();

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

//create two solid brushes
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));

//create two fonts
PdfFont font1 = new PdfFont(PdfFontFamily.Helvetica, 15f, PdfFontStyle.Bold);
PdfFont font2 = new PdfFont(PdfFontFamily.Helvetica, 12f);

//initialize x, y coordinates
float x = 0;
float y = 0;

//title
String title = "Java-Overview";

//align text to center via PdfTextAlignment class
PdfTextAlignment alignment = PdfTextAlignment.Center;

//draw title on the center of the page
drawTitle(page, title, font1, brush1, (float) page.getClientSize().getWidth() / 2, y, alignment);
y = y + 30;

//paragraph text
String paragraph = "Java is a class-based, object-oriented programming language that is
designed to have as few implementation dependencies as possible. It is a general-purpose 
programming language intended to let application developers write once, run anywhere, meaning 
that compiled Java code can run on all platforms that support Java without the need for 
recompilation. Java applications are typically compiled to bytecode that can run on any 
Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java
is similar to C and C++, but has fewer low-level facilities than either of them. The Java 
runtime provides dynamic capabilities (such as reflection and runtime code modification)that are
typically not available in traditional compiled languages. As of 2019, Java was one of the most 
popular programming languages in use according to GitHub, particularly for client-server web applications, with a reported 9 million developers.";
        //draw paragraph on the page
PdfLayoutResult layoutResult = drawParagraph(page, paragraph, font2, brush2, x, y);
y = y + (float) layoutResult.getBounds().getHeight() + 10;

//load an image file
PdfImage image = PdfImage.fromImage("C:\\Users\\Test1\\Desktop\\Image.png");

//draw image on the page
drawImage(page, image, x, y);
y = y + (float) image.getPhysicalDimension().getHeight() + 10;

//save to file
doc.saveToFile("output/CreatePdf.pdf");
}
public static void drawTitle(PdfPageBase page, String text, PdfFont font, PdfBrush brush,
float x, float y, PdfTextAlignment alignment) {

//set the text alignment via PdfStringFormat class
PdfStringFormat format = new PdfStringFormat();
format.setAlignment(alignment);

//draw title on the page
page.getCanvas().drawString(text, font, brush, x, y, format);
}
public static PdfLayoutResult drawParagraph(PdfPageBase page, String text, PdfFont font, PdfBrush
brush, float x, float y) {

//create a PdfTextWidget object
PdfTextWidget widget = new PdfTextWidget(text, font, brush);

//set the PdfLayoutType to Paginate to make the content paginated automatically
PdfTextLayout layout = new PdfTextLayout();
layout.setLayout(PdfLayoutType.Paginate);

//create a rectangle where the paragraph will be placed
Rectangle2D.Float rect = new Rectangle2D.Float(0, y, (float) page.getClientSize().getWidth(),
(float) page.getClientSize().getHeight());

//draw paragraph on the page
PdfLayoutResult layoutResult = widget.draw(page, rect, layout);
return layoutResult;
}
public static void drawImage(PdfPageBase page, PdfImage image, float x, float y) {

//draw image on the page
page.getCanvas().drawImage(image, x, y);
}
}

Output




No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...