All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpedal.examples.PageCount Maven / Gradle / Ivy

The newest version!
/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/java-pdf-library-support/
 *
 * (C) Copyright 1997-2013, IDRsolutions and Contributors.
 *
 * 	This file is part of JPedal
 *
     This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 *
 * ---------------
 * PageCount.java
 * ---------------
 */

/**
 * This example opens a pdf file and gets the number of pages it contains
 */
package org.jpedal.examples;

import java.io.File;

import org.jpedal.PdfDecoder;

public class PageCount {

	/** user dir in which program can write */
	private String user_dir = System.getProperty("user.dir");

	/** sample file which can be setup - substitute your own. */
	private static String test_file = "/mnt/win_d/sample.pdf";

	public PageCount() {
	}

	// ////////////////////////////////////////////////////////////////////////
	/** example method to open a file and return the number of pages */
	public PageCount(String file_name) {
		String separator = System.getProperty("file.separator");

		// check output dir has separator
		if (this.user_dir.endsWith(separator) == false) this.user_dir = this.user_dir + separator;

		/**
		 * set up PdfDecoder object telling it whether to display messages and where to find its lookup tables
		 */
		PdfDecoder decode_pdf;

		// PdfDecoder returns a PdfException if there is a problem
		try {
			decode_pdf = new PdfDecoder(false); // false as no GUI display needed

			/**
			 * open the file (and read metadata including pages in file)
			 */
			System.out.println("Opening file :" + file_name);
			decode_pdf.openPdfFile(file_name);

			/** get page number */
			System.out.println("Page count=" + decode_pdf.getPageCount());

			/** close the pdf file */
			decode_pdf.closePdfFile();

		}
		catch (Exception e) {
			System.err.println("5.Exception " + e + " in pdf code");

		}
	}

	// ////////////////////////////////////////////////////////////////////////
	/**
	 * main routine which checks for any files passed and runs the demo
	 */
	public static void main(String[] args) {
		System.out.println("Simple demo to extract number of pages");

		// set to default
		String file_name = test_file;

		// check user has passed us a filename and use default if none
		if (args.length != 1) System.out.println("Default test file used");
		else {
			file_name = args[0];
			System.out.println("File :" + file_name);
		}

		// check file exists
		File pdf_file = new File(file_name);

		// if file exists, open and get number of pages
		if (pdf_file.exists() == false) System.out.println("File " + file_name + " not found");
		else {
			new PageCount(file_name);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy