# Lab-6808: Working with PDF and Java

Expected Duration: 120 minutes
Contacts: Duane Nickull

Introduction | Exercise 0 | Exercise 1 | Exercise 2 | Exercise 3 | Exercise 4 | Exercise 5 | Exercise 6 | Exercise 7 | Exercise 8 | Summary
### Exercise 2: Let’s find out some attributes about our PDF document ### PDF has many versions. While in most cases it is not important, when writing code there are often requirements to understand the version of the PDF. Since you can programmatically turn an older version into a more current version, we would first want to get the version. Getting the Version and size (number of Pages) of a PDF document. In this section we will return to the Java API docs to explore some of the core things that are possible with PDF and Java. The following lines of code will be added: Step 1: Keeping your last project open (Exercise 1), add the following lines of code at the end of the PDFExtract method. (note - if you did not keep your last project open, you can simply open up the projects `PDFNumberPages.java and PDFVersion.java`

      try {

        System.out.println("Document version is " + doc.getVersion());

        } catch (Exception e) {

        System.out.println("Error getting document version: " + e);

        }
This simply uses the getVersion() method to grab the version of the document we have open. Step 2: Now use the following method to get the number of pages of the doc. Add this to the PDFOpen project if you have it open or where the TODO" appears in the PDFNumberPages.java class.

        try {

          System.out.println("Number of pages is " + doc.getNumberOfPages());

          } catch (Exception e) {

            System.out.println("Error getting number of Pages " + e );

          }
Now try to run it again. Your completed code should look like this:

        import java.io.InputStream;

          import java.io.FileInputStream;

          import java.io.FileOutputStream;

          import java.io.BufferedWriter;

          import java.io.FileWriter;

          import java.io.FileNotFoundException;

          import java.io.IOException;

          // this is from xpaaj.jar - check licenses before using.

          // LiveCycle ES has newer JAVA libraries for manipulating PDF

      import com.adobe.pdf.*;
        public class JavaOne2009_1 {

          public static void main(String[] args)

                  throws FileNotFoundException, IOException

            {

                  // get pdf filename

                  String inPdfName;

                  if(args.length != 1 ) {

                    System.out.println("\nCommand line format: java PDFExtractData pdf-file");

                    return;

                  } else {

                        // message

                      System.out.println("\nPDF data extraction using Adobe PDF Libraries.");

                        inPdfName = new String(args[0]);

                        PDFExtract(inPdfName);

                  }

            }
               /**

             *      TODO: Add Method PDFExtract to open and extract data from a PDF file

             */

          public static void PDFExtract(String inPdfName)

            throws FileNotFoundException, IOException

            {

            // open PDF

            System.out.println("\nOpen PDF Document ... ");

            PDFDocument doc = null;

            FileInputStream inPdfFile = new FileInputStream(inPdfName);
              try {

                  doc = PDFFactory.openDocument(inPdfFile);

            } catch (IOException e) {

                        System.out.println("Error opening PDF file :" + inPdfName);

                        System.out.println(e);

            }
              if(doc == null)

                  System.out.println("Cannot open PDF file : " + inPdfName);

            else

                  System.out.println( inPdfName + " was successfully opened.");

      try {

          System.out.println("Document version is " + doc.getVersion());

          } catch (Exception e) {

          System.out.println("Error getting document version: " + e);

          }
        try {

          System.out.println("Number of pages is " +   doc.getNumberOfPages());

          } catch (Exception e) {

                  System.out.println("Error getting number of Pages " + e );

          }

          }

          }
    /******* End of file *********************************/
You should see the following in the eclipse console window: `PDF data extraction using Adobe PDF Libraries.` `Open PDF Document ...` `/Users/adobe/Desktop/eclipse/workspace/JavaOne2009-1/src/DuanesWorld.pdf was successfully opened.` `Document version is %PDF-1.6` `Number of pages is 1` If you did, congtratulations! Proceed to exercise 3 Back to top Next exercise