logo
Login

Sponsored Links

Sponsored

How to create PDF file using Java API?

Create PDF using Java APIThis snippet shows how to created a empty PDF file using the PDFBox Apache Framework.

package com.livrona.snippets.pdf;

import java.io.IOException;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

/**
* This snippet shows how to create a blank PDF and write the contents to a
* file.
*/

public class CreateBlankPDF {

/**
* Create the Blank PDF
*
* @param file
* @throws IOException
* @throws COSVisitorException
*/

public void create(String file) throws IOException, COSVisitorException {
PDDocument document = null;
try {
// create the PDF document object
document = new PDDocument();

// create an empty page, add it to the document
PDPage blankPage = new PDPage();
document.addPage(blankPage);

// save the document to the file stream.
document.save(file);
} finally {
if (document != null) {
document.close();
}
}
}

/**
* This will create a blank document.
*
* @param args
* The command line arguments.
*
* @throws IOException
* If there is an error writing the document data.
* @throws COSVisitorException
* If there is an error generating the data.
*/

public static void main(String[] args) throws IOException,
COSVisitorException {
CreateBlankPDF creator = new CreateBlankPDF();
if (args.length != 1) {
creator.usage();
} else {

creator.create(args[0]);
}
}

/**
* This will print out a message telling how to use this example.
*/

private void usage() {
System.err.println("usage: " + this.getClass().getName()
+ " <output-pdf-file>");
}
}



Like Developerfeed? Follows us via: RSS Feed Follow me on Twitter! Follow me on Facebook! Follow me on Youtube!

Socialize & Share Now :

Subscribe to DeveloperFeedDid you like this article? Did it Help you Solve your Problem? You can get the all the latest articles published at DeveloperFeed in your email inbox by entering your email address below. Your address will only be used for mailing you the articles, and each one will include a link so you can unsubscribe at any time.

Enter your email address:

BiGF00T's picture
 #

Hi,

thanks, I have used this as a starting point. Writing hello world became very soon too boring and I implemented some methods around PDFBox which helped me create my documents.

I have published them here in the hope that they will save someone else as much time as they did for me:

http://blog.sina.com.cn/s/blog_958470e80100xfm1.html

They include centered text, table (which I copied from another page), automatic page generation, text in columns (like using tabstops), draw a horizontal line.

Thanks for your small introduction,

BiGF00T

 

Subscribe & Follow

RSS Feed Follow me on Twitter! Follow me on Facebook! Follow me on Youtube!
Feed via Email:

Guide Navigation

Sponsored Links

Facebook

Sponsored Apps

Job Change Alert for Linkedin


Contact2CRM for Salesforce


Advertise on DeveloperFeed

About

Manpreet is an Architect & Software developer currently focusing on developing mobile apps on the iOS (iPhone/iPad) Platform. He’s the founder of a small iPhone development studio
called Livrona.

Subscribe to Developer Feed iPhone Blog

Tutorials

Tags

Who's online

There are currently 1 user and 7 guests online.

Online users