2010-12-14 49 views
5

Tôi đang sử dụng java và RCP và tôi đang cố gắng hiển thị Tài liệu pdf với Acrobat trên quan điểm của tôi. Tôi không cần phải thay đổi chúng. Tôi gặp lỗi này với mã này. Bất kỳ ý tưởng làm thế nào để giải quyết vấn đề này ?. P.s .: nó hoạt động tốt cùng một lần.Trình xem PDF Java

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) 

Kính trọng, Haythem

+0

Bạn đang sử dụng thư viện nào ?? – Favonius

Trả lời

12

Có một cái nhìn tại các renderer pdf miễn phí ...

Một số liên kết ...

  1. http://www.icepdf.org/

  2. http://www.jpedal.org/support_siEclipse.php

  3. https://java.net/projects/pdf-renderer

CẬP NHẬT

Theo http://www.icepdf.org/,

ICEpdf là một mã nguồn mở Java PDF động cơ có thể làm cho, chuyển đổi, hoặc nội dung PDF chiết xuất trong mọi ứng dụng Java hoặc trên máy chủ Web.

Đối với chức năng cơ bản, bạn phải bao gồm icepdf-core.jaricepdf-viewer.jar trong đường dẫn lớp học của mình. Tùy thuộc vào yêu cầu bạn cũng có thể thêm hỗ trợ SVG.

Taken từ thư mục mẫu 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); 
    } 
} 

Đoạn mã trên sẽ giúp bạn trong việc hiển thị một PDF trên một thành phần swing. Bạn có thể làm tương tự trong môi trường SWT (có một cái nhìn tại SwingViewBuilder .. loại cứng, nhưng sẽ SWT nhìn và cảm nhận) hoặc sử dụng org.eclipse.swt.awt.SWT_AWT (loại dễ dàng ... nhưng sẽ có swing + swt nhìn và cảm nhận) .. mặc dù cả hai cách tiếp cận sẽ giải quyết mục đích của bạn. Đồng thời kiểm tra giấy phép hiện hành trong thư mục giấy phép.

Hy vọng điều này sẽ hữu ích.

+0

Có, tôi đã thấy chúng nhưng tôi không thể tìm thấy cách để mở các tệp PDF hiện có và hiển thị chúng trên một chế độ xem. – Haythem

+0

@Haythem: Vậy bạn đang sử dụng thư viện nào? – Favonius

+0

@Favonius: Tôi đang sử dụng org.nightlabs.eclipse.ui.pdfviewer – Haythem

4

Dưới đây là một Trình xem PDF miễn phí, nhỏ và mạnh mẽ khác dựa trên Eclipse SWT và jPod Renderer - JPview. Nó có dựng hình mạnh mẽ và sử dụng bộ nhớ thấp.

Các vấn đề liên quan