This 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>");
}
}
|
|
Related Links
- How to create PDF file using Java API?
- How to add Text to PDF?
- Find Files in Directory based on Extension
- How to construct DOM and convert to XML?
- Apache DBCP Pooling Datasource Example
- Apache DBCP BasicDatSource Setup
- What is a Niche?
- No Such Object (LdapException)
- How to encode URL?
- Programmatically Toggle Function Keys
- mvohra's blog
- Printer-friendly version
- Add new comment
- 4587 reads



How to construct DOM and convert to XML?
Did 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.


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