This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Free Spire.PDF with Java.

Table of Contents


Overview

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.

Before Start

Download Free Spire.PDF for Java from this link and import the Spire.Pdf.jar file in your Java project.

Coordinate System

A page generated by Spire.PDF consists of margins and content area. The coordinate system exists only in the content area, allowing users to add content to the specified coordinates. The coordinate system in Spire.PDF 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.

  • drawTitle

  • When drawing a title on PDF, you 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.

  • drawParagraph

  • 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.

  • drawImage

  • 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.

  • drawTable

  • 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.

  • drawSortedList

  • 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.

    The resulting PDF file looks like the one below.


    Convert PDF to Other Formats

    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.

  • Add Header and Footer to PDF
  • Add Links to PDF
  • Add Text/Image Watermark to PDF
  • Add Attachments to PDF
  • Add Image/Dynamic Stamps to PDF
  • Apply Different Fonts in PDF
  • Create Fillable Form Fields in PDF
  • Create Grid in PDF
  • Draw Barcode in PDF
  • Draw Shapes in PDF
  • Extract Text/Image from PDF
  • Encrypt and Decrypt PDF
  • Merge/Split PDF
  • Print PDF

  • Conclusion

    With the easy-to-use APIs provided by Free Spire.PDF, you 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.  

    If you have any questions, feel free to comment below or contact us at support@e-iceblue.com.
     
    When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
    ScroogeXHTML - small and flexible RTF to HTML converter library
    https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!