PDFBox Example | Create Empty PDF file using java PDFBox

by Azeem Tariq

Posted 2 years ago

Last Updated 11 months ago

1811 views

 

 

Hi in the previous tutorial  we set up our development environment for creating pdf files using PDFBox library and in this tutorial i will show you how you can use PDFBox and create very simple and empty PDF file. If you guys did not check my previous post then please check it and follow the steps to proceed in this tutorial

Java PDFBox Example Code

import java.io.IOException;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

/**
 *
 * @author Azeem
 * @Email [email protected]
 */


public class Main {


    public static void main(String[] args) {
        
        System.out.println("Create Simple PDF file with blank Page");
        
        String fileName = "EmptyPdf.pdf"; // name of our file
        
        try{
        
        PDDocument doc = new PDDocument(); // creating instance of pdfDoc
        
        doc.addPage(new PDPage()); // adding page in pdf doc file
        
        doc.save(fileName); // saving as pdf file with name perm 
        
        doc.close(); // cleaning memory 
        
        System.out.println("your file created in : "+ System.getProperty("user.dir"));
        
        
        }
        catch(IOException | COSVisitorException e){
        System.out.println(e.getMessage());
        }
        
    }
    
}

 

Conclusion

This is very simple tutorial to create pdf file but if you still face problem then please check out video tutorial :) if you don't like my tutorial then tell me if you like it then tell your friends :)