Advertisement :
   Log In    OR    Register  
  Topics :  
RMI Example

Home >>> Your Questions on Java >>> PDF Java example >>> PDFBox example
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
Java IO Basics
Description : Understanding Input Output Streams
More...


Written By : Sameer
Title :
Java Comparator and compareTo
Description : Question
More...


Written By : Sameer
Title :
Apache ServiceMix
Description : SOA and ESB
More...


Written By : kkk_f17
Title :
Question on String in Java
Description : How to improve performance
More...


Written By : guddu
Title :
Tomcat and LDAP
Description : Integration for Enterprise SSO
More...


Written By : Amit
Title :
SSO
Description : Single Sign On
More...


Written By : Amit
Title :
Swing or AWT
Description : Why to choose either
More...


Written By : Girish
Title :
ACL Tag Library
Description : Using Java in Web Application
More...

Tags/Keywords : PDF Java example, PDF Java, PDF Box, PDF Screen,Article, simple, example
Author : Amit
Date (Year/Month/Date): 2010-05-09 Recently I was doing an example /POC on using Apache PDFBOX, thereby writing a PDF viewer type of screen using Java's Swing API. In this process I was able to show a single page from an external PDF file/page on screen, but encountered a typical issue, that can be from using Swing API or/and PDFBox API, I am not sure how to resolve this yet. Following is the code that I was refering about: PDFViewer.java

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;

import org.apache.pdfbox.PDFReader;
import org.apache.pdfbox.pdfviewer.PDFPagePanel;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

public class PDFViewer
{
  public PDFViewer(String pdfFileName) {
    System.out.println("PDF file name to show is :"+pdfFileName);
    final PDDocument pdDoc;
  try {
    JFrame screen = new JFrame(pdfFileName);
    Dimension dm = Toolkit.getDefaultToolkit().getScreenSize();
    screen.setSize(dm);

    File file = new File(pdfFileName);
    pdDoc = PDDocument.load(file);
    PDFPagePanel pdfPagePanel = new PDFPagePanel();
    pdfPagePanel.setSize(1000,1000);
    int numOfPages = pdDoc.getNumberOfPages();
    PDPage pdPage = (PDPage) pdDoc.getPrintable(13);
    pdPage.findCropBox().setLowerLeftX(1);
    pdPage.setCropBox(new PDRectangle(1000f,1000f));
    pdPage.setMediaBox(new PDRectangle(1000f,1000f));
    pdPage.setArtBox(new PDRectangle(1000f,1000f));
    pdPage.setBleedBox(new PDRectangle(1000f,1000f));
    System.out.println(pdPage.getCropBox().getHeight());
    pdfPagePanel.setPage(pdPage);

    JScrollPane scrolPane = new JScrollPane(pdfPagePanel);
    scrolPane.setVerticalScrollBar(new JScrollBar());
    scrolPane.setWheelScrollingEnabled(true);

    JToolBar toolBar = new JToolBar();
    ImageIcon imageiconP = new ImageIcon("Previous.png");
    ImageIcon imageiconN = new ImageIcon("Next.png");
    JButton butP = new JButton(imageiconP);
    JButton butN = new JButton(imageiconN);	
    toolBar.add(butP);
    toolBar.add(butN);
    screen.getContentPane().setLayout(new BorderLayout());
    screen.getContentPane().add(toolBar, BorderLayout.NORTH);
    screen.getContentPane().add(scrolPane, BorderLayout.CENTER);
    screen.setVisible(true);
    screen.show();
    screen.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we) {
        try {
	 pdDoc.close();
        } catch (IOException e) {
	  e.printStackTrace();
        }
        System.exit(0);
      }
    });
    System.out.println("end");
  } catch (IOException e) {
 
    e.printStackTrace();
  } finally {
 }
}
  public static void main(String[] args) 
  {
    new PDFViewer(args[0]);
  }
}
This program is tested in my local dev environment, but as I have already mentioned, it is not showing the Page from the PDF document with a scroll bar to be able to navigate across this page, which is not working in this code. Actually I had tried using JScrollPane and many more methods on the PDPage from PDFBox, but failed to show a scroll bar in order to move or go through the complete page shown on screen. I have used PDFPagePanel from PDFBox to be embeded in a JFrame from SWING API. If anyone has some time and willing to help, please help me in achieving this very specific issue on hand. If you have any further queries for which you need clarification, please do let me know by using following comments section.

Advertisement :



	
Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
8,40
Enter bigger number from above :*
Home >>> Your Questions on Java >>> PDF Java example >>> PDFBox example
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
18,27
Enter bigger number from above : *

Please log in to add or reply to any matter<- requires login
Log in or Register
Copyright © 2008-2010, Interview-Questions-Tips-Forum, All Rights Reserved.
CONTACT    PRIVACY POLICY    DISCLAIMER
 

Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Other names may be trademarks of their respective owners

This web site's Terms of Use and Disclaimer :

This web site provides some of the information about various technologies, example code, tips, tutorials etc. Like any printed materials,
content of these pages may become out of date over a period of time. Therefore all visitor/users of this web site are requested/advised
to refer to the originating parties/sources for the latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating parties/sources. These examples with source code or
without source code, have not been thoroughly tested under all conditions. Interview-questions-tips-forum.net therefore, cannot guarantee
or imply reliability of these example source code or programs.

By accessing and using this website in any ways, including, without limitation, browsing the website pages, using any information, using
any content and/or downloading any materials, you agree to and are bound by the terms of use described in this page and Usage Terms and Conditions.
If you do not agree to all of the terms and conditions contained in the terms of use described in this page and
Usage Terms and Conditions, do not use this website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use on your employer's behalf.

All Trademarks are property of their respective owner. Appropriate measure is being taken for providing accurate and up-to-date
information but like any printed materials, these blog(s)/contents may eventually be outdated one day, so if you are using any of these
information, please refer original content/documentation from respective sources. And under no circumstances shall the Author of these
contents and/or this web site be liable for any loss, damage, expense incurred or suffered which is claimed to have occurred because of
usage of the contents of this web site. If you have any questions/queries/feedback/suggestions then please write to this web site owner
at contact.