题
我正在使用Java和RCP,并且正在尝试用Acrobat向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)
问候,海瑟姆
解决方案
看看这些免费的PDF渲染器...
一些链接...
http://www.icepdf.org/ (现在 http://www.iceseott.org/java/projects/iepdf/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)
更新
ICEPDF是一种开源Java PDF引擎,可以在任何Java应用程序或Web服务器上渲染,转换或提取PDF内容。
对于基本功能,您必须包括 icepdf-core.jar
和 icepdf-viewer.jar
在您的课程中。根据要求,您还可以添加SVG支持。
取自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);
}
}
上面的代码可帮助您在秋千组件上显示PDF。您可以在SWT环境中做同样的事情(看看 SwingViewBuilder
..有点艰难,但会看起来和感觉)或使用 org.eclipse.swt.awt.SWT_AWT
(很容易...但是会带有Swing + SWT的外观和感觉)...尽管两种方法都可以解决您的目的。还要在许可文件夹中检查适用的许可证。
希望这会有所帮助。
其他提示
这是基于Eclipse SWT和JPOD渲染器的另一个免费,小型且功能强大的PDF查看器 - jpview. 。它具有强大的渲染和低记忆使用量。