Free Spire.PDF for Java is a class library that allows you to create and process PDF documents directly from your Java programs without having to install any additional software. PDF files can be easily created using this library. Simply create a PdfDocument object, create as many PdfPageBase objects as necessary, add strings, images, tables, lists and any other elements to the pages and then save the document.
In this post, I’ll give you an example of how to create a simple PDF file containing title, paragraph, image, numbered list and table, and how to convert PDF to other file formats using this free API with Java.
A page generated by Spire.PDF consists of margins and content area. The content in PDF is added to a specific coordinate of the content area. The coodinate system follows the following rules.
The origin of the coordinate system (0, 0) represents the top-left corner of the content area.
The x-axis extends to the right and the y-axis extends downward.
Creating Custom Functions
Spire.PDF itself provides useful classes and methods, such as PdfPageBase class, drawString method and drawImage method, which work with the page settings, writing text and adding images according to the specific coordinates. In order to make the code easier to read, I pre-defined five custom functions below to perform different tasks.
When drawing a title on PDF, you may probably need to align text. The drawTitle function created based on drawString is such a function that allows to horizontally align text, for instance, to center.
The drawParagraph function is different from drawTitle, though they’re taking similar parameters. This function is created based on PdfTextWidget.draw method and returns an object of PdfLayoutResult class which contains the bounds information of the element being drawing. By doing so, you’ll be able to know where the current paragraph ends and where to start drawing the next piece of content.
The drawImage function is a simply overloading of the original method. This function won’t returns a PdfLayoutResult object. In order to know where the content below begins, you'll need to get the image height through PdfImage object and then calculate the Y coordinate.
When using the drawTable function to draw tables on a PDF page, you need to specify the table data in String array. This function also returns a PdfLayoutResult object that helps us get the position and size of the table.
The last function drawSortedList is quite similar with drawTable, except that it takes some unique parameters like list content, order marker and list indent.
Using the Code
Invoke these custom functions and you’ll be able to draw elements at the appropriate positions of a PDF page.
Instead of saving the newly generated PDF (PdfDocument object) to a PDF file, you can also save it to other file formats like Doc, Docx, XPS, SVG and HTML directly through saveToFile method. In the code snippets below, I’d like to show you how to load an existing PDF document and do the file format conversion.
Apart from the file formats mentioned above, Spire.PDF also supports to convert PDF to popular image formats such as PNG, JPEG, EMF and TIFF. The code is a bit tricky, but far from complicated.
Other Useful Features
Below is a list of some other useful features supported by Free Spire.PDF.
With the easy-to-use APIs provided by Free Spire.PDF, we can quickly create PDF from scratch and convert PDF to other file formats without spending a lot of learning time. This is undoubtedly a good news for developers. No matter whether your project needs a PDF component or not, no matter whether you are a senior or a rookie programmer, you are welcome to download the library and gain some wonderful experience in processing PDF documents.