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
 boolean canUserPrint(java.io.InputStream inputStream)
          Shows the Encrypted pdf inputStream can print or not.
 boolean canUserPrint(java.lang.String filePath)
          Shows the Encrypted pdf file can print or not.
 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 getCurrentHighlightIndex()
          Gets current ID of textToFind for GUI Search.
 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.
 int getSearchEndPage()
          Gets the search end page for GUI Search.
 int getSearchStartPage()
          Gets the search start page for GUI Search.
 int getTextMatchedCount()
          Gets the total matched number for GUI Search.
 void HighlightTextArea(int textMatchedIndex)
          Highlights the matched text area.
 void invalidate()
          Invalidates the container.
 boolean isEncryptedFile(java.io.InputStream inputStream)
          Shows the Pdf file stream is Encrypted or not.
 boolean isEncryptedFile(java.lang.String filePath)
          Shows the Pdf file is Encrypted or not.
 boolean isOwnerPassword(java.io.InputStream inputStream, java.lang.String ownerPassword)
          Shows the password is the owner password of the Encrypted Pdf file inputStream or not.
 boolean isOwnerPassword(java.lang.String filePath, java.lang.String ownerPassword)
          Shows the password is the owner password of the Encrypted Pdf file or not.
 boolean isPrintSuccess()
          Shows the Pdf page print successful or not.
 boolean isUserPassword(java.io.InputStream inputStream, java.lang.String userPassword)
          Shows the password is the user password password of the Encrypted Pdf file or not.If the user is the user(not the owner),he has limited access to the pdf file ,including printing and so on.
 boolean isUserPassword(java.lang.String filePath, java.lang.String userPassword)
          Shows the password is the user password of the Encrypted Pdf file or not.
 void openPdfFile(java.io.InputStream inputStream)
          Opens a Pdf file stream.
 void openPdfFile(java.io.InputStream inputStream, java.lang.String password)
          Opens an encrypted Pdf file stream.
 void openPdfFile(java.lang.String filePath)
          Opens a Pdf file, but does not actually decode the pages of the Pdf file.
 void openPdfFile(java.lang.String filePath, java.lang.String password)
          Opens an encrypted Pdf document.
 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 searchText(java.lang.String textToSearch)
          Searches text from startPage to endPage.
 void searchText(java.lang.String textToSearch, boolean isCaseSensitive)
          Searches text from startPage to endPage.
 void searchText(java.lang.String textToSearch, boolean isCaseSensitive, java.awt.Color HightLightColor)
          Searches text to get location of textToSearch within the rectangle from startPage to endPage.And shows itemFoundCount in searchCount TextField.
 void searchText(java.lang.String textToSearch, java.awt.Color hightlightColor)
          Searches text from startPage to endPage.
 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.
 void setSearchEndPage(int endPage)
          Sets the search end page for GUI Search.
 void setSearchStartPage(int startPage)
          Sets the search start page for GUI Search.
 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,
                              java.lang.Exception
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 - InputStream the pdf stream to be opened.
Throws:
PdfViewerException
java.io.IOException
java.lang.Exception

openPdfFile

public final void openPdfFile(java.io.InputStream inputStream,
                              java.lang.String password)
                       throws PdfViewerException,
                              java.lang.Exception
Opens an encrypted 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 - InputStream the encrypted pdf stream to be opened.
password - String the password of the file.
Throws:
PdfViewerException
java.lang.Exception

openPdfFile

public final void openPdfFile(java.lang.String filePath,
                              java.lang.String password)
                       throws PdfViewerException,
                              java.lang.Exception
Opens an encrypted Pdf document. 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 file path of the encrypted Pdf document to be opened.
password - String the password of the file.
Throws:
PdfViewerException
java.lang.Exception

setSearchStartPage

public void setSearchStartPage(int startPage)
Sets the search start page for GUI Search.

Parameters:
startPage - int the start page of pdf file to be searched in GUI.

getSearchStartPage

public int getSearchStartPage()
Gets the search start page for GUI Search.

Returns:
the start page of pdf file to be searched in GUI.

setSearchEndPage

public void setSearchEndPage(int endPage)
Sets the search end page for GUI Search.

Parameters:
endPage - int the end page of pdf file to be searched in GUI.

getSearchEndPage

public int getSearchEndPage()
Gets the search end page for GUI Search.

Returns:
the end page of pdf file to be searched in GUI.

searchText

public void searchText(java.lang.String textToSearch)
                throws PdfViewerException,
                       java.lang.Exception
Searches text from startPage to endPage. The isCaseSensitive parameter default value is false and the default highlight text color is Color.PINK.

Parameters:
textToSearch - String text to be searched.
Throws:
PdfViewerException
java.lang.Exception

searchText

public void searchText(java.lang.String textToSearch,
                       boolean isCaseSensitive)
                throws PdfViewerException,
                       java.lang.Exception
Searches text from startPage to endPage. The isCaseSensitive parameter default value is false.

Parameters:
textToSearch - String text to be searched.
isCaseSensitive - boolean shows the text comparison case senitive or not. true - case sensitive comparison when searching. false - not case sensitive comparison when searching.
Throws:
PdfViewerException
java.lang.Exception

searchText

public void searchText(java.lang.String textToSearch,
                       java.awt.Color hightlightColor)
                throws PdfViewerException,
                       java.lang.Exception
Searches text from startPage to endPage. The default highlight text color is Color.PINK.

Parameters:
textToSearch - String text to be searched.
hightlightColor - Color the color of searched text.
Throws:
PdfViewerException
java.lang.Exception

searchText

public void searchText(java.lang.String textToSearch,
                       boolean isCaseSensitive,
                       java.awt.Color HightLightColor)
                throws PdfViewerException,
                       java.lang.Exception
Searches text to get location of textToSearch within the rectangle from startPage to endPage.And shows itemFoundCount in searchCount TextField.

Parameters:
textToSearch - String text to be found.
isCaseSensitive - boolean if a case sensitive comparison happens or not when searching.
HightLightColor - Color color of textToSearch.
Throws:
PdfViewerException
java.lang.Exception

getTextMatchedCount

public int getTextMatchedCount()
Gets the total matched number for GUI Search.

Returns:
the total matched number.

getCurrentHighlightIndex

public int getCurrentHighlightIndex()
Gets current ID of textToFind for GUI Search.


HighlightTextArea

public void HighlightTextArea(int textMatchedIndex)
                       throws PdfViewerException,
                              java.lang.Exception
Highlights the matched text area.

Parameters:
textMatchedIndex - int the index of search result.
Throws:
PdfViewerException
java.lang.Exception

isEncryptedFile

public boolean isEncryptedFile(java.lang.String filePath)
                        throws java.io.IOException
Shows the Pdf file is Encrypted or not.

Parameters:
filePath - String the path of Pdf file.
Returns:
return the Pdf file Encrypted or not.
Throws:
IOException.
java.io.IOException

isEncryptedFile

public boolean isEncryptedFile(java.io.InputStream inputStream)
                        throws java.io.IOException
Shows the Pdf file stream is Encrypted or not.

Parameters:
inputStream - InputStream the stream of Pdf file.
Returns:
return the Pdf file stream Encrypted or not.
Throws:
IOException.
java.io.IOException

isOwnerPassword

public boolean isOwnerPassword(java.io.InputStream inputStream,
                               java.lang.String ownerPassword)
                        throws java.io.IOException,
                               com.aspose.pdf.kit.CryptographyException
Shows the password is the owner password of the Encrypted Pdf file inputStream or not.

Parameters:
inputStream - InputStream the stream of Pdf file.
ownerPassword - String the owner password of the Pdf file.If the user is the owner ,he has full access to the pdf file ,including viewing, printing and so on.
Returns:
return the Pdf file inputStream Encrypted or not.
Throws:
CryptographyException.
IOException.
java.io.IOException
com.aspose.pdf.kit.CryptographyException

isOwnerPassword

public boolean isOwnerPassword(java.lang.String filePath,
                               java.lang.String ownerPassword)
                        throws java.io.IOException,
                               com.aspose.pdf.kit.CryptographyException
Shows the password is the owner password of the Encrypted Pdf file or not.

Parameters:
filePath - String the path of the Pdf file.
ownerPassword - String the owner password of the Pdf file.If the user is the owner ,he has full access to the pdf file ,including printing and so on.
Returns:
return the Pdf file Encrypted or not.
Throws:
CryptographyException.
IOException.
java.io.IOException
com.aspose.pdf.kit.CryptographyException

isUserPassword

public boolean isUserPassword(java.lang.String filePath,
                              java.lang.String userPassword)
                       throws java.io.IOException,
                              com.aspose.pdf.kit.CryptographyException
Shows the password is the user password of the Encrypted Pdf file or not.

Parameters:
filePath - String the path of the Pdf file.
userPassword - String the user password of the Pdf file.If the user is the user(not the owner),he has limited access to the pdf file ,including printing and so on.
Returns:
return the Pdf file Encrypted or not.
Throws:
CryptographyException.
IOException.
java.io.IOException
com.aspose.pdf.kit.CryptographyException

isUserPassword

public boolean isUserPassword(java.io.InputStream inputStream,
                              java.lang.String userPassword)
                       throws java.io.IOException,
                              com.aspose.pdf.kit.CryptographyException
Shows the password is the user password password of the Encrypted Pdf file or not.If the user is the user(not the owner),he has limited access to the pdf file ,including printing and so on.

Parameters:
inputStream - InputStream the stream of Pdf file.
userPassword - String the user password of the Pdf file.
Returns:
return the Pdf file Encrypted or not.
Throws:
CryptographyException.
IOException.
java.io.IOException
com.aspose.pdf.kit.CryptographyException

canUserPrint

public boolean canUserPrint(java.lang.String filePath)
                     throws java.io.IOException
Shows the Encrypted pdf file can print or not.

Parameters:
filePath - String the path of the Pdf file.
Returns:
return the pdf file can print or not.
Throws:
IOException.
java.io.IOException

canUserPrint

public boolean canUserPrint(java.io.InputStream inputStream)
                     throws java.io.IOException
Shows the Encrypted pdf inputStream can print or not.

Parameters:
inputStream - InputStream the stream of Pdf file.
Returns:
return the pdf file can print or not.
Throws:
IOException.
java.io.IOException