Java PDF 查看器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4437910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java PDF Viewer
提问by Haythem
I am using java and RCP and I am trying to show pdf Document with Acrobat on my views. I don't need to change them. I have this error with this code. Any idea how to resolve this problem?. P.s.: it works good same times.
我正在使用 java 和 RCP,我试图在我的视图中使用 Acrobat 显示 pdf 文档。我不需要改变它们。这段代码我有这个错误。知道如何解决这个问题吗?Ps:它同时运行良好。
PDFFile pdfFile;
pdfFile = PdfFileLoader.loadPdf(file, new NullProgressMonitor());
PdfDocument pdfDocument = new OneDimensionalPdfDocument(pdfFile, new NullProgressMonitor());
pdfViewer.setPdfDocument(pdfDocument);
Error from PdfDocument pdfDocument = new OneDimensionalPdfDocument(pdfFile, new NullProgressMonitor()) : Unsupport CMap format: 6
java.nio.BufferUnderflowException
at java.nio.Buffer.nextGetIndex(Unknown Source)
at java.nio.HeapByteBuffer.getShort(Unknown Source)
at com.sun.pdfview.font.ttf.HmtxTable.setData(HmtxTable.java:79)
at com.sun.pdfview.font.ttf.TrueTypeTable.createTable(TrueTypeTable.java:113)
at com.sun.pdfview.font.ttf.TrueTypeFont.getTable(TrueTypeFont.java:106)
at com.sun.pdfview.font.TTFFont.getOutline(TTFFont.java:129)
at com.sun.pdfview.font.TTFFont.getOutline(TTFFont.java:89)
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.PDFParser.iterate(PDFParser.java:742)
at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:88)
at java.lang.Thread.run(Unknown Source)
Regards, Haythem
问候, 海瑟姆
采纳答案by Favonius
Have a look at these free pdf renderer ...
看看这些免费的 pdf 渲染器...
Some links ...
一些链接...
http://www.icepdf.org/(now at http://www.icesoft.org/java/projects/ICEpdf/overview.jsf- Apache 2 Open Source)
http://www.jpedal.org/support_siEclipse.php(now at https://www.idrsolutions.com/jpedal/- commercial)
https://java.net/projects/pdf-renderer(still available https://github.com/yarick123/pdf-renderer- LGPL-2.1)
http://www.icepdf.org/(现在位于http://www.icesoft.org/java/projects/ICEpdf/overview.jsf- Apache 2 开源)
http://www.jpedal.org/support_siEclipse.php(现在在https://www.idrsolutions.com/jpedal/- 商业)
https://java.net/projects/pdf-renderer(仍然可用https://github.com/yarick123/pdf-renderer- LGPL-2.1)
UPDATE
更新
As per http://www.icepdf.org/,
ICEpdf is an open source Java PDF engine that can render, convert, or extract PDF content within any Java application or on a Web server.
ICEpdf 是一个开源 Java PDF 引擎,可以在任何 Java 应用程序或 Web 服务器上呈现、转换或提取 PDF 内容。
For basic functionality you have to include icepdf-core.jar
and icepdf-viewer.jar
in your class path. Depending upon the requirement you can also add the SVG support.
对于基本功能,您必须在类路径中包含icepdf-core.jar
和icepdf-viewer.jar
。根据要求,您还可以添加 SVG 支持。
Taken from iceface sample folder:
取自 iceface 示例文件夹:
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import javax.swing.*;
/**
* The <code>ViewerComponentExample</code> class is an example of how to use
* <code>SwingController</code> and <code>SwingViewBuilder</code>
* to build a PDF viewer component. A file specified at the command line is
* opened in a JFrame which contains the viewer component.
*
* @since 2.0
*/
public class ViewerComponentExample {
public static void main(String[] args) {
// Get a file from the command line to open
String filePath = args[0];
// build a component controller
SwingController controller = new SwingController();
SwingViewBuilder factory = new SwingViewBuilder(controller);
JPanel viewerComponentPanel = factory.buildViewerPanel();
// add interactive mouse link annotation support via callback
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
JFrame applicationFrame = new JFrame();
applicationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applicationFrame.getContentPane().add(viewerComponentPanel);
// Now that the GUI is all in place, we can try openning a PDF
controller.openDocument(filePath);
// show the component
applicationFrame.pack();
applicationFrame.setVisible(true);
}
}
The above code helps you in displaying a PDF on a swing component. You can do the same in the SWT environment (have a look at SwingViewBuilder
.. kind of hard, but will SWT look and feel ) or use org.eclipse.swt.awt.SWT_AWT
(kind of easy... but will have swing + swt look and feel)... though both approach will solve your purpose. Also check the applicable licenses in the license folder.
上面的代码可帮助您在摆动组件上显示 PDF。你可以在 SWT 环境中做同样的事情(看看SwingViewBuilder
.. 有点难,但会 SWT 的外观和感觉)或使用org.eclipse.swt.awt.SWT_AWT
(有点容易......但会有swing + swt 的外观和感觉)......虽然这两种方法都将解决您的目的。还要检查许可证文件夹中的适用许可证。
Hope this will help.
希望这会有所帮助。