We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Load PDF and render it on Canvas/JPanel. A package is missing

I am working on a project to load files with different formats and display them on a Java component, such as Canvas, JPanel, or JavaFX panes. I can use Aspose.Words to load DOCX, XML, TXT, HTML with Document’s constructor and render them with the Document.renderToScale() or renderToSize() method.


I was trying to do the same for PDF with Aspose.Pdf. After checking the JavaDoc it seems PdfDocument can do this. However, I have two issues:

  1. I have not found any method that can render a PDF, like what Aspose.Words does. What class can do this?
  2. The aspose-pdf-jdk16.jar does not have the com.aspose.pdf.engine package. So PdfDocument class dose not exist and I could not even test if any PDF file can be loaded with PdfDocument class.

Best regards,
Martin

Hi Martin,


Thanks for your interest in our API’s.

Aspose.Pdf for Java is PDF file creation and manipulation API and I am afraid it does not support the feature to render/display PDF files. However I would recommend you to please try using ViewerApp of our sister company named GroupDocs. You can use this cloud application to display/render many file formats.

Besides this, you may consider converting PDF pages to Image format and then displaying those images in JPanel or other control. For more details, please visit Convert PDF pages to JPEG Image

Hi Nayyer,
Thanks for your reply.

The package “com.aspose.pdf.engine” is mssing from the jar file “aspose-pdf-jdk16.jar” which I downloaded recently.

This package contains the ‘PdfDocument’ class which is what I am trying to use.

I have tried Aspose.Words, Aspose.Cells, Aspose.Slides. All works well. If Aspose.Pdf works too, it’s very possible that our company will buy these products soon.

martinatidg:
Hi Nayyer,
Thanks for your reply.

The package “com.aspose.pdf.engine” is mssing from the jar file “aspose-pdf-jdk16.jar” which I downloaded recently.

This package contains the ‘PdfDocument’ class which is what I am trying to use.

I have tried Aspose.Words, Aspose.Cells, Aspose.Slides. All works well. If Aspose.Pdf works too, it’s very possible that our company will buy these products soon.

Hi Martin,

The com.aspose.pdf.engine package is for internal use and its respective classes are not publicly exposed. Also please note that PdfDocument class is not publicaly avaialble and it might have been obfuscated in latest release. In order to manipulate PDF files, please try using com.aspose.pdf.Document class.

Concerning to your original query where you have been able to “use Aspose.Words to load DOCX, XML, TXT, HTML with Document’s constructor and render them with the Document.renderToScale() or renderToSize() method.” so in order to deal with PDF files, you may consider converting PDF files to DOC format using Aspose.Pdf for Java and then manipulate the resultant DOC file with Aspose.Words for Java. Please take a look over following code snippet.

[Java]

//
load source PDF file
<o:p></o:p>

com.aspose.pdf.Document doc = new com.aspose.pdf.Document("c:/pdftest/Conversion_Output.pdf");

// save the output in DOC format

doc.save("c:/pdftest/Convertedfile.doc", com.aspose.pdf.SaveFormat.Doc);

Hi Nayyer,

Thank you for your reply.



It would make it quite slow to read and convert a PDF to Doc and then read and render the doc file.



It seems Aspose.Pdf lacks a function the other products have. That is to get the mage of a page or draw the image of a page to a Graphics/Graphics2D:





For example, we need a function for PDF that is similar to the functions found in other products as listed below:



Aspose.Slides: BufferredImage ISilde.getThumbnail(float, float);


Aspose.Cells: SheetRender.toImage(int, Graphics);


Aspose.Words: Document.renderToScale(int, Graphics, int, int, float);





Thank you very much.

Martin

Hi Martin,


As shared in my earlier post 560247, Aspose.Pdf for Java supports the feature to convert PDF pages to Image format. For further details, please visit Convert PDF pages to JPEG Image

As all the API’s have different document creation and manipulation routines, so Aspose.Pdf for Java contains specific classes for PDF to Image rendering. In case you encounter any issue, please share the resource PDF, so that we can test the scenario at our end.

Hi Nayyer,


Thank you very much. That’s a little different.




The post 560247 is to convert a PDF document to an JPEG image document.




However, as shown in other products examples in my last post, what we need is a Java Image/BufferedImage object for one specific page or a method that can render one specific page of PDF to a Java Graphics/Graphics2D object.




From the example of your post, it seems a page can be output to an OutputStream as shown below. If that’s an InputStream, I may create an Image object with it. Is there any method can do that?





jpegDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream);

martinatidg:
what we need is a Java Image/BufferedImage object for one specific page or a method that can render one specific page of PDF to a Java Graphics/Graphics2D object.
From the example of your post, it seems a page can be output to an OutputStream as shown below. If that's an InputStream, I may create an Image object with it. Is there any method can do that?

jpegDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream);
Hi Martin,

Thanks for sharing the details.

I am afraid the requested feature is currently not supported but for the sake of implementation, I have logged this requirement in our issue tracking system under New Features list as PDFNEWJAVA-34344. We will further investigate this requirement in details and will keep you updated on the status of a correction.

We apologize for your inconvenience.

Thank you very much!

Martin