Home  Products  Purchase  Downloads  Demos  Forums  Blogs  Ticket  Wiki  API  Corporate

com.aspose.pdf.kit
Class PdfViewer

java.lang.Object
  extended bycom.aspose.pdf.kit.PdfViewer
All Implemented Interfaces:
java.awt.print.Pageable, java.awt.print.Printable

public final class PdfViewer
extends java.lang.Object
implements java.awt.print.Printable, java.awt.print.Pageable

Represents the Pdf Viewer,it can be embedded in three-party software to view and print Pdf files, it has almost all Adobe Reader¡¯s functions. On the same way, it provides APIs to achieve the same functions. This component¡¯s features including:

1. viewing Pdf file in visual mode embedded to other software;

2. zooming the Pdf page;

3. printing the Pdf file, including: print all pages or a special page scale;

4. searching text in the Pdf file, including: search text in a special rectangle or full pdf page;

5. extracting text in the Pdf file, including: extract text in a special rectangle or full pdf page;

 
  It can be used like this:
  Pdf kit viewer lib, The SimplePdfViewer example has the view and print function of the lib.
  Examples for the lib are listed as follows:
   1.SimplePdfViewer
   2.BackgroundPrint
   3.ExtractTextInRectangle
   4.SearchTextInRectangle
   
  To use the lib you need the following step at least:
  
  [SampleCode]
  //create a PdfViewer class instance.
  PdfViewer pdfviewer = new PdfViewer();
  //open a Pdf file.
  pdfviewer.openPdfFile(filePath);
  //decode the pdf page.
  pdfviewer.decodePage(currentPage);
  //set the Pdf file page parameters, the first 1 = 100%, the second 1 is the Pdf page number.
  pdfviewer.setPdfPageParameters(1, 1);
  //initialize the Pdf show.
  displayScrollPane = pdfviewer.showPdf();
       
       
  //If you want to print the Pdf file, the follow code is an example. The SimplePdfViewer shows the usage in detail. 
  //sets the default print scale - false.
  pdfviewer.enableScalePrint(false);
  //gets a printjob object.
  PrinterJob printJob = PrinterJob.getPrinterJob();
  //gets the default page.
  PageFormat pf = printJob.defaultPage();
  //sets the Paper object for this PageFormat.
  pf.setPaper(paper);
  //allows user to edit settings and select printing
  printJob.setPrintable(pdfviewer, pf);
  //creates the printDialog.
  boolean printFile = printJob.printDialog();
  if (printFile)
  	{
  		printJob.print();
  	}
  //close the Pdf file.
  pdfviewer.closePdfFile();
  
 


Field Summary
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Fields inherited from interface java.awt.print.Pageable
UNKNOWN_NUMBER_OF_PAGES
 
Constructor Summary
PdfViewer()
          The constructor of the PdfViewer object.
PdfViewer(boolean isGraphic)
          The constructor of the PdfViewer object.
 
Method Summary
 void closePdfFile()
          Closes the current Pdf file.
 void decodePage(int pageNumber)
          Decodes a page of one Pdf file.
 void enableScalePrint(boolean scalePrint)
          Sets the print scale or not.
 int getCropBoxHeight(int pageNumber)
          Gets the cropBox height.
 int getCropBoxWidth(int pageNumber)
          Gets the cropBox width.
 int getMediaBoxHeight(int pageNumber)
          Gets the pdf height of mediaBox.
 int getMediaBoxWidth(int pageNumber)
          Gets the pdf width of mediaBox.
 int getMediaBoxX(int pageNumber)
          Gets the pdf mediaBox X value.
 int getMediaBoxY(int pageNumber)
          Gets the pdf file mediaBox Y value.
 int getNumberOfPages()
          Gets the print page number which implements the java.awt.print.Pageable interface.
 java.awt.print.PageFormat getPageFormat(int pageIndex)
          Gets the PageFormat object of the pageIndex page which implements java.awt.print.Pageable interface.
 int getPdfFilePageCount()
          Gets page count of the current Pdf file.
 int getPdfHeight()
          Gets the height of JScrollPane which is the Pdf file view area.
 int getPdfWidth()
          Gets the width of JScrollPane which is the Pdf file view area.
 java.awt.print.Printable getPrintable(int pageIndex)
          Gets the Printable object which implements the java.awt.print.Pageable interface.
 java.lang.String getPrintFailureMessage()
          Gets the print failure message string.
 float getPrintScaleFactor()
          Gets the scaling factor applied to the Pdf page.
 void invalidate()
          Invalidates the container.
 boolean isPrintSuccess()
          Shows the Pdf page print successful or not.
 void openPdfFile(java.io.InputStream inputStream)
          Opens a Pdf file stream.
 void openPdfFile(java.lang.String filePath)
          Opens a Pdf file, but does not actually decode the pages of the Pdf file.
 int print(java.awt.Graphics graphics, java.awt.print.PageFormat pageFormat, int pageIndex)
          Implements the standard Java printing functionality of java.awt.print.Printable.
 void setPageFormat(int pageNumber, java.awt.print.PageFormat pageFormat)
          Sets the page format for a special page.
 void setPdfPageParameters(float scale, int pageNumber)
          Initialises the initial scale factor of the specific Pdf page number.
 void setPrintPageRange(int startPage, int endPage)
          Sets the print page range.
 javax.swing.JScrollPane showPdf()
          Shows the Pdf file in a JScrollPane object which is the Pdf view area.
 void updateUI()
          Invokes this method to reset the UI which using the JPanel.updateUI().
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PdfViewer

public PdfViewer()
The constructor of the PdfViewer object.


PdfViewer

public PdfViewer(boolean isGraphic)
The constructor of the PdfViewer object.

Parameters:
isGraphic - boolean show the pdf file in JScrollPane or not. true - show the pdf file in JScrollPane. false - not show the pdf file in JScrollPane, it is for text extraction.
Method Detail

openPdfFile

public final void openPdfFile(java.lang.String filePath)
                       throws PdfViewerException,
                              java.lang.Exception
Opens a Pdf file, but does not actually decode the pages of the Pdf file. At last you must explicitly close the open file using the closePdfFile() to release all the memory.

Parameters:
filePath - String the path of Pdf file.
Throws:
PdfViewerException
java.lang.Exception

closePdfFile

public final void closePdfFile()
Closes the current Pdf file.


decodePage

public final void decodePage(int pageNumber)
                      throws PdfViewerSecurityException,
                             PdfViewerFontException,
                             PdfViewerException,
                             java.lang.Exception
Decodes a page of one Pdf file.

Parameters:
pageNumber - int The page number of one Pdf file which must be between 1 and getPdfFilePageCount().
Throws:
PdfViewerSecurityException
PdfViewerFontException
PdfViewerException
java.lang.Exception

showPdf

public javax.swing.JScrollPane showPdf()
Shows the Pdf file in a JScrollPane object which is the Pdf view area.

Returns:
return the Pdf view area.

setPdfPageParameters

public final void setPdfPageParameters(float scale,
                                       int pageNumber)
Initialises the initial scale factor of the specific Pdf page number.

Parameters:
scale - float the scale factor.
pageNumber - int the special page of the Pdf file.

getPdfFilePageCount

public final int getPdfFilePageCount()
Gets page count of the current Pdf file.

Returns:
return page count.

getCropBoxWidth

public int getCropBoxWidth(int pageNumber)
Gets the cropBox width. It is the value of the CropBox key. The crop box is the part of the document to be displayed. It is usually the same as the media box but may be smaller.

Parameters:
pageNumber - int the page number to be examined to get the CropBox Width.
Returns:
return the CropBox width.

getCropBoxHeight

public int getCropBoxHeight(int pageNumber)
Gets the cropBox height.

Parameters:
pageNumber - int the page number to be examined to get the CropBox height.
Returns:
return the CropBox height.

getPdfWidth

public final int getPdfWidth()
Gets the width of JScrollPane which is the Pdf file view area. It includes any scaling factor which has been set.

Returns:
return the width of JScrollPane.

getPdfHeight

public final int getPdfHeight()
Gets the height of JScrollPane which is the Pdf file view area. It includes any scaling factor which has been set.

Returns:
return the height of JScrollPane.

invalidate

public void invalidate()
Invalidates the container. The container and all parents above it are marked as needing to be laid out.


updateUI

public void updateUI()
Invokes this method to reset the UI which using the JPanel.updateUI().

See Also:
JComponent.updateUI()

enableScalePrint

public void enableScalePrint(boolean scalePrint)
Sets the print scale or not.

Parameters:
scalePrint - boolean true - using the pdf scaling factor to print. false - using the 100% scaling factor to print which is the raw pdf scale.

getPrintScaleFactor

public float getPrintScaleFactor()
Gets the scaling factor applied to the Pdf page.

Returns:
return the scaling factor.

isPrintSuccess

public boolean isPrintSuccess()
Shows the Pdf page print successful or not.

Returns:
return the printing successful or not.

getPrintFailureMessage

public java.lang.String getPrintFailureMessage()
Gets the print failure message string. It is useful for debugging.

Returns:
return the print failure message string.

print

public int print(java.awt.Graphics graphics,
                 java.awt.print.PageFormat pageFormat,
                 int pageIndex)
          throws java.awt.print.PrinterException
Implements the standard Java printing functionality of java.awt.print.Printable.

Specified by:
print in interface java.awt.print.Printable
Parameters:
graphics - Graphics object page rendered.
pageFormat - PageFormat object of the print PageFormat.
pageIndex - int current page index (less 1 so start at page 0).
Returns:
int Printable.PAGE_EXISTS or Printable.NO_SUCH_PAGE
Throws:
java.awt.print.PrinterException

getNumberOfPages

public int getNumberOfPages()
Gets the print page number which implements the java.awt.print.Pageable interface.

Specified by:
getNumberOfPages in interface java.awt.print.Pageable
Returns:
return the page number.
See Also:
Pageable.getNumberOfPages()

getPageFormat

public java.awt.print.PageFormat getPageFormat(int pageIndex)
                                        throws java.lang.IndexOutOfBoundsException
Gets the PageFormat object of the pageIndex page which implements java.awt.print.Pageable interface.

Specified by:
getPageFormat in interface java.awt.print.Pageable
Returns:
return the PageFormat object.
Throws:
java.lang.IndexOutOfBoundsException
See Also:
Pageable.getPageFormat(int)

setPageFormat

public void setPageFormat(int pageNumber,
                          java.awt.print.PageFormat pageFormat)
Sets the page format for a special page. If no pageFormat set, a default PageFormat object will be used.

Parameters:
pageNumber - int the special page.
pageFormat - PageFormat the PageFormat object.

getPrintable

public java.awt.print.Printable getPrintable(int pageIndex)
                                      throws java.lang.IndexOutOfBoundsException
Gets the Printable object which implements the java.awt.print.Pageable interface.

Specified by:
getPrintable in interface java.awt.print.Pageable
Parameters:
pageIndex - int the index of the page.
Returns:
return the Printable object.
Throws:
java.lang.IndexOutOfBoundsException
See Also:
Pageable.getPrintable(int)

setPrintPageRange

public void setPrintPageRange(int startPage,
                              int endPage)
Sets the print page range. Make sure 1 <= startPage <= endPage <= getPdfFilePageCount().

Parameters:
startPage - int the start page to print and larger than 1.
endPage - int the end page to print and larger than or equals startPage.

getMediaBoxX

public int getMediaBoxX(int pageNumber)
Gets the pdf mediaBox X value.

Parameters:
pageNumber - int the page number of the current pdf file.
Returns:
mediaBox X value of the page.

getMediaBoxY

public int getMediaBoxY(int pageNumber)
Gets the pdf file mediaBox Y value.

Parameters:
pageNumber - int the page number of the current pdf file.
Returns:
mediaBox Y value of the page.

getMediaBoxHeight

public int getMediaBoxHeight(int pageNumber)
Gets the pdf height of mediaBox.

Parameters:
pageNumber - int the page number of the current pdf file.
Returns:
the height of pdf file mediaBox.

getMediaBoxWidth

public int getMediaBoxWidth(int pageNumber)
Gets the pdf width of mediaBox.

Parameters:
pageNumber - int the page number of the current pdf file.
Returns:
the width of pdf file mediaBox.

openPdfFile

public final void openPdfFile(java.io.InputStream inputStream)
                       throws PdfViewerException,
                              java.io.IOException
Opens a Pdf file stream. But does not actually decode the pages of the Pdf file. At last you must explicitly close the open file using the closePdfFile() to release all the memory.

Parameters:
inputStream - The pdf stream to be opened.
Throws:
PdfViewerException
java.io.IOException