Vaibhav's Blog Space

Project PDF-Renderer

Monday Jun 16, 2008

Today I have raised the observer permission for project PDF-Renderer. Awesome project which help Java Developer to work on PDF format. A complete viewer and render. API's are strong and I have just check this code from site itself. This code put the first page of your PDF file inside PagePanel. No doubt PDF is one of the open format used worldwide across all OS. In such a case, support from Java is something like adding more flavor in sweet.


import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PagePanel;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.*;

/**
 * An example of using the PagePanel class to show PDFs. For more advanced
 * usage including navigation and zooming, look ad the 
 * com.sun.pdfview.PDFViewer class.
 *
 * -AT-author joshua.marinacci@sun-DOT-com
 */
public class Main {

    public static void setup() throws IOException {
    
        //set up the frame and panel
        JFrame frame = new JFrame("PDF Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PagePanel panel = new PagePanel();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

        //load a pdf from a byte buffer
        File file = new File("Amityform.pdf");
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
            0, channel.size());
        PDFFile pdffile = new PDFFile(buf);

        // show the first page
        PDFPage page = pdffile.getPage(0);
        panel.showPage(page);
        
    }

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {⁞
                    Main.setup();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

Just download the jar file from project site. And then :

javac -cp PDFRenderer.jar Main.java

java -cp PDFRenderer.jar;. Main

It is pretty fast as well, because IO operation has been done by NIO and channels are superb.  Thanks guys for making such a great project.

[29] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg
Comments:

Hi, I downloaded pdfview sourcecode manually from https://pdf-renderer.dev.java.net, but I can't find the class PagePanel, Can you send email and tell me where is it?

Posted by sunxing on August 21, 2008 at 01:15 PM IST #

Oh, page panel is there at this location:

com.sun.pdfview.PagePanel;

I will check again, may be some API got changed. :)

Posted by Vaibhav Choudhary on August 21, 2008 at 02:05 PM IST #

PDF Renderer jar files are working for some file ,but latest version or complicated pdf file are not even displaying,exception were found in the console.i have tried a lot but no result were found for these exceptions.

Posted by D.Shankar on August 25, 2008 at 12:18 PM IST #

This is what developer of this project told me -

PagePanel is in the demos viewer hierarchy at:
https://pdf-renderer.dev.java.net/source/browse/pdf-r
enderer/demos/viewer/com/sun/pdfview/PagePanel.java

Posted by Vaibhav Choudhary on August 25, 2008 at 12:33 PM IST #

I am not able to find library of com.sun.pdfview.*

Posted by Tapasya on September 08, 2008 at 04:04 PM IST #

I am not able to find library of com.sun.pdfview.PDFFile AND com.sun.pdfview.PDFPage

Posted by Tapasya on September 08, 2008 at 04:13 PM IST #

Yes Tapasya ! There are some issues with the current example given. I have put that into the ears of concerned person. Rest, I would love if you join the project.

Posted by Vaibhav on September 09, 2008 at 12:36 AM IST #

Hi Vaibhav,
I am experiencing problem in randering the PDF file in JPanel.
I tried this sample it is working fine. But when i rander it in JPanel it dosn't work.
I am able to see number of pages in PDF in the console but on the panel
it shows "no page selected" .
Please suggest me the solution.
Thnx in advance.

Posted by Vikram Thakur on September 12, 2008 at 05:53 PM IST #

Ya sure sir. How i can join u'r project

Posted by Tapasya on September 13, 2008 at 06:01 PM IST #

Hi Vaibhav,

I am using this PDFRenderer.jar to display PDFs in our application. Simple PDFs are getting displayed properly, but the complex ones like the latest W4 form are not getting displayed properly and it is throwing the following exception on fonts!!

java.lang.RuntimeException: Bad command (15)
at com.sun.pdfview.font.Type1Font.parse(Type1Font.java:656)
at com.sun.pdfview.font.Type1Font.parseGlyph(Type1Font.java:789)
at com.sun.pdfview.font.Type1Font.getOutline(Type1Font.java:818)
at com.sun.pdfview.font.Type1Font.getOutline(Type1Font.java:846)
at com.sun.pdfview.font.OutlineFont.getGlyph(OutlineFont.java:118)
at com.sun.pdfview.font.PDFFont.getCachedGlyph(PDFFont.java:307)
at com.sun.pdfview.font.PDFFontEncoding.getGlyphFromEncoding(PDFFontEncoding.java:132)
at com.sun.pdfview.font.PDFFontEncoding.getGlyphs(PDFFontEncoding.java:98)
at com.sun.pdfview.font.PDFFont.getGlyphs(PDFFont.java:273)
at com.sun.pdfview.PDFTextFormat.doText(PDFTextFormat.java:283)
at com.sun.pdfview.PDFTextFormat.doText(PDFTextFormat.java:315)
at com.sun.pdfview.PDFParser.iterate(PDFParser.java:758)
at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:88)
at java.lang.Thread.run(Thread.java:595)

Do you have any solution for this?

Thanks

Posted by Balaji on September 25, 2008 at 02:35 AM IST #

Hi Vaibhav,

We are using PDFRenderer.jar in our project for displaying PDF files.
I am using below code for getting PDFPage in my pdfPanel(JPanel) which is showing data in UI. Please refer below code for more info.:

public void refreshPage()
{
try
{
ByteBuffer buf = ByteBuffer.wrap(inputPdf);
PDFFile newfile = new PDFFile(buf);
PDFPage page = newfile.getPage(currentPage);
awtImage = (Image) page.getImage((int) (page.getWidth())
,(int) (page.getHeight()), null, null);
Image scaleImage = awtImage.getScaledInstance((int) (awtImage.getWidth(null) * pdfScalRatio),
(int) (awtImage.getHeight(null) * pdfScalRatio), Image.SCALE_DEFAULT);
ImageIcon imgforLabel = new ImageIcon(scaleImage);
pdfPanel.setPreferredSize(new Dimension(imgforLabel.getIconWidth(),imgforLabel.getIconHeight()));
pageLabel.setOpaque(false);
pageLabel.setBorder(null);
pageLabel.setIcon(imgforLabel);
pageLabel.setBounds(0,0,scaleImage.getWidth(null), scaleImage.getHeight(null));
pdfPanel.add(pageLabel);
pdfPanel.repaint();
pdfPanel.validate();

} catch (Exception er) {
er.printStackTrace();
}
}

Issue:
My issue this is working fine in some of the PDF file and not working in some of the PDF files, but after some event say MousePress, MouseClick it is coming automatically. So can you please help me out to slove this issue.

Please mail me if you want more info.

Posted by Vishal Patel on October 17, 2008 at 07:02 PM IST #

Hi Vaibhav,
Is there any feature for full text searching similar to Acrobet Reader?

Posted by Vikram Thakur on December 04, 2008 at 11:51 AM IST #

Hi Vaibhav,

Do you know how this API can be used to display a byte array containing the pdf file content? I have in mind the retrieval of a pdf file from a database in blob format, moving in into a byte array and then display it.

Thanks and regards,
Ferenc

Posted by Ferenc Peter on December 10, 2008 at 09:31 PM IST #

How to show the chinese charactor using PDF Render framework?
I got exception:
java.io.IOException: Unknown CMap: UniCNS-UCS2-H
at com.sun.pdfview.font.PDFCMap.getCMap(PDFCMap.java:67)
at com.sun.pdfview.font.PDFFontEncoding.<init>(PDFFontEncoding.java:73)
at com.sun.pdfview.font.PDFFont.getFont(PDFFont.java:131)
at com.sun.pdfview.PDFParser.getFontFrom(PDFParser.java:1150)
at com.sun.pdfview.PDFParser.iterate(PDFParser.java:723)
at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101)
at java.lang.Thread.run(Thread.java:619)

Posted by James on February 22, 2009 at 07:56 PM IST #

Hello!

I am experiencing the same problem when trying to a PDF file with embedded fonts in it. Can anyone help me out in setting this fonts?
Below are exceptions:
java.nio.InvalidMarkException
at java.nio.Buffer.reset(Unknown Source)
at com.sun.pdfview.font.ttf.TrueTypeFont.parseDirectories(TrueTypeFont.java:317)
at com.sun.pdfview.font.ttf.TrueTypeFont.parseFont(TrueTypeFont.java:70)
at com.sun.pdfview.font.TTFFont.<init>(TTFFont.java:76)
at com.sun.pdfview.font.CIDFontType2.<init>(CIDFontType2.java:62)
at com.sun.pdfview.font.PDFFont.getFont(PDFFont.java:170)
at com.sun.pdfview.font.Type0Font.<init>(Type0Font.java:51)
at com.sun.pdfview.font.PDFFont.getFont(PDFFont.java:141)
at com.sun.pdfview.PDFParser.getFontFrom(PDFParser.java:1150)
at com.sun.pdfview.PDFParser.iterate(PDFParser.java:723)
at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101)
at java.lang.Thread.run(Unknown Source)

Thanks

Posted by ION on February 27, 2009 at 09:25 PM IST #

Hi Ferenc,

I did some thing very similar. We have a PDF file generated and stored in the DB as a blob. We just need to stream the byte array to the response outputstream, but set the response content type to application-pdf. The browser is smart enough to know and render the stream as a PDF.

Besides that, we had additional functionality requirement to present the pdf content as an image. For that we used the PDFRenderer library to take that byte stream from the db and convert it to images (each page an image)Hope this helps.

Posted by Saikrishna on March 04, 2009 at 04:17 PM IST #

hi,

i needof java pdf viewer for multiPage...
the above coding is restricted the table line display...

Posted by abdul basith m on March 31, 2009 at 02:46 PM IST #

hi,

i needof java pdf viewer for multiPage...
the above coding is restricted the table line display...

Posted by abdul basith m on March 31, 2009 at 02:48 PM IST #

Hi

I have a problem with com.sun.pdfview.PagePanel
when I insert the PagePanel into JDialog (JDialog working as modal mode)
the PagePanel doeasn't work :( I getting "no page selected"

Does anyone has some solution ?

Posted by abrams on April 02, 2009 at 04:55 PM IST #

Ok thats me again

There is only one way to resolve the problem above - create an image

PDFPage pdfPage = getPDFfile().getPage(page);
rectangle = new Rectangle(0, 0, (int) pdfPage.getBBox().getWidth(), (int) pdfPage.getBBox().getHeight());
Image img = pdfPage.getImage(rectangle.width, rectangle.height, rectangle, null, true, true);
JLabel(new ImageIcon(img));

and now you can show label on your JDialog

Posted by abrams on April 06, 2009 at 03:34 PM IST #

Hey Vaibhav, this should solve your problem. :)
http://tecbites.blogspot.com/2008/02/java-pdfrenderer-and-importance-of.html

Posted by Akshat on April 30, 2009 at 10:03 PM IST #

Oops, it wasn't Vaibhav talking about PdfRenderer showing "no page". Well whoever it was, that link should come handy.

Posted by Akshat on April 30, 2009 at 10:24 PM IST #

How can I view pdf fill 100% the jpanel?
I need to view the document big.

Posted by Ayres on May 22, 2009 at 04:51 PM IST #

i am facing a strange issue using PDF Renderer with respect to font style, the all document which get’s printed using PDF Renderer, the text font style are geting changed to italic even thought its not, can any one help to solve this issue

Posted by rudrasagar on July 22, 2009 at 06:19 PM IST #

Hi Vaibhav,

Does anybody working on PDF-Renderer project?

I want to enhance scrolling and printing capabilities of PDF-Renderer, however task seems to be daunting, especially in absence of proper documentation.

I appreciate if you can point me to right direction/person/documentation.

Thanks.

Posted by mp on August 07, 2009 at 09:01 PM IST #

Hi,

I'm trying to pring pdf file which is a bag tag. It prints only half of it.

Here pdf file is long and it print only the 100% (when i view the file with 100% zoom it displays only half of the content).

My code for rendering is:

public int print ( final Graphics graphics, final PageFormat pageFormat, final int index ) throws PrinterException
{
final int pagenum = index + 1;
if ( ( pagenum >= 1 ) && ( pagenum <= pdfFile.getNumPages ( ) ) )
{
final Graphics2D graphics2D = ( Graphics2D ) graphics;
final PDFPage page = pdfFile.getPage ( pagenum );

// fit the PDFPage into the printing area
final Rectangle imageArea =
new Rectangle ( ( int ) pageFormat.getImageableX ( ), ( int ) pageFormat.getImageableY ( ),
( int ) pageFormat.getImageableWidth ( ), ( int ) pageFormat.getImageableHeight ( ) );

graphics2D.translate ( 0, 0 );
final PDFRenderer pgs = new PDFRenderer ( page, graphics2D, imageArea, null, null );

System.err.println ( "imageArea.getX " + imageArea.getX ( ) );
System.err.println ( "imageArea.getY" + imageArea.getY ( ));
System.err.println ( "imageArea.getWidth " + imageArea.getWidth ( ) );
System.err.println ( "imageArea.getHeight " + imageArea.getHeight ( ) );
try
{
page.waitForFinish ( );
pgs.run ( );
}
catch ( InterruptedException ie )
{
// nothing to do
/*
* if ( logService.isInfoEnabled ( ) ) { logService.info ( "ignoring InterruptedException : " +
* ie.getMessage ( ) ); }
*/
}
return PAGE_EXISTS;
}
else
{
return NO_SUCH_PAGE;
}
}

Also, I'm removing marging:
// to remove margins
final Paper paper = new Paper ( );
int width = ( int ) paper.getWidth ( );
int height = ( int ) paper.getHeight ( );
if ( "Bag Tag".equals ( printerType ) )
{
PDFPage page = pdfFile.getPage ( 0 );
width = ( int ) page.getBBox ( ).getWidth ( );// 155;
height = ( int ) page.getBBox ( ).getHeight ( );// 1510;
}
if ( logService.isInfoEnabled ( ) )
{
logService.info ( "paper widhth=" + width + " :: paper height=" + height );
}
paper.setImageableArea ( 0, 0, width, height );
pageFormat.setPaper ( paper );

Any idea about this?

Posted by Riyaz on September 12, 2009 at 05:57 PM IST #

Hi Vaibhav,

Can you help me in getting the source of PDFRenderer? Am trying to download the PDFRenderer source files from the link that you have provided and also from the SwingLabs site...but it seems like the site is down....

If you have the source files please leave a reply here...will pass you my mail id so that you can mail the src file...waiting for your precious reply...

Thanks in advance

Posted by Rajashekar on October 22, 2009 at 07:50 PM IST #

download site will be up soon.

Posted by Vaibhav on October 22, 2009 at 08:15 PM IST #

Hi vaibhav,

In my program I am importing com.sun.pdfview.PDFViewer and able to display the document, and was wondering how to select text and copy after displaying the document using PDFViewer

Thanks

Posted by Sandeep on November 08, 2009 at 01:28 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed