【问题标题】:Java PDF ViewerJava PDF 查看器
【发布时间】:2011-05-25 04:29:09
【问题描述】:

我正在使用 java 和 RCP,我正在尝试在我的视图中显示带有 Acrobat 的 pdf 文档。 我不需要改变它们。 这段代码我有这个错误。知道如何解决这个问题吗? P.s.:它同时工作得很好。

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)

问候, 海瑟姆

【问题讨论】:

  • 你使用的是哪个库??

标签: java pdf rcp pdfrenderer


【解决方案1】:

看看这些免费的 pdf 渲染器...

一些链接...

  1. http://www.icepdf.org/(现在http://www.icesoft.org/java/projects/ICEpdf/overview.jsf - Apache 2 开源)

  2. http://www.jpedal.org/support_siEclipse.php(现为https://www.idrsolutions.com/jpedal/ - 商业)

  3. https://java.net/projects/pdf-renderer(仍然可用https://github.com/yarick123/pdf-renderer - LGPL-2.1)

更新

根据http://www.icepdf.org/

ICEpdf 是一个开源的 Java PDF 可以渲染、转换或 在任何 Java 中提取 PDF 内容 应用程序或 Web 服务器上。

对于基本功能,您必须在类路径中包含 icepdf-core.jaricepdf-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);
    }
}

以上代码可帮助您在 swing 组件上显示 PDF。你可以在 SWT 环境中做同样的事情(看看SwingViewBuilder .. 有点难,但是 SWT 的外观和感觉)或使用 org.eclipse.swt.awt.SWT_AWT (有点简单......但会有 swing + swt 的外观和感觉)......虽然这两种方法都会解决你的目的。还要检查许可证文件夹中的适用许可证。

希望这会有所帮助。

【讨论】:

  • 是的,我看到了它们,但我找不到打开现有 PDF 文件并在视图中显示它们的方法。
  • @Favonius:我正在使用 org.nightlabs.eclipse.ui.pdfviewer
  • @Haythem:根据 nightlab 页面,他们在内部使用 pdf-renderer.dev.java.net 进行渲染。 PDF 文件中是否有出现问题的图像?众所周知,sun pdf-renderer 存在嵌入 tiff 文件的问题。看看这个 javalobby 帖子javalobby.org/java/forums/t104502.html
  • @Haythem:同样来自pdf-renderer.dev.java.net,这是唯一已知的版本。我建议您查看jpedal.org/support_siEclipse.php 阅读器来源也可以在他们的页面上找到。
  • @Favoonius:我应该使用像 iText 这样的开源项目。您是否有示例如何使用 iText 或开源在视图上显示现有的 pdf 文档?
【解决方案2】:

这是另一个基于 Eclipse SWT 和 jPod Renderer 的免费、小巧且功能强大的 PDF 查看器 — JPview。它具有强大的渲染和低内存使用率。

【讨论】:

    猜你喜欢
    • 2013-02-06
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多