2013-05-29 201 views
1

我必須在項目中實現PDF查看器庫我正在關注https://github.com/bhavyahmehta/Pdf-Reader---Writer/blob/master/Pdf_%20Reader_Writer/README.md 在這個例子中,他們正在創建PDF和在查看器中加載。 其實我明白如何庫的工作原理下面是代碼下面的片段從庫中創建和加載PDF:PDF閱讀器通過PDF閱讀器---在Android中的作家

  @Override 
    protected Void doInBackground(Void... params) 
    { 
    String pdfcontent = generateHelloWorldPDF(); //generating pdf content 
    outputToFile("PdfSample.pdf",pdfcontent,"ISO-8859-1"); //passing pdf name and content 
     return null; 
    } 

      private String generateHelloWorldPDF() 
      { //function returns pdf content with pages 
     PDFWriter mPDFWriter = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT); 


    try { 
     // all image formates are supported 
     Bitmap i1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
     mPDFWriter.addImage(40, 60, i1, Transformation.DEGREES_315_ROTATION); 
     mPDFWriter.addImage(72, 72, i1); 
     mPDFWriter.addImage(200, 400, 135, 75, i1); 
     mPDFWriter.addImage(150, 300, 130, 70, i1); 
     mPDFWriter.addImageKeepRatio(100, 200, 50, 25, i1); 
     mPDFWriter.addImageKeepRatio(50, 100, 30, 25, i1, Transformation.DEGREES_270_ROTATION); 
     mPDFWriter.addImageKeepRatio(25, 50, 30, 25, i1); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.TIMES_ROMAN); 
    mPDFWriter.addRawContent("row content \n"); 
    mPDFWriter.addText(70, 50, 12, "hello world"); 
    mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING); 
    mPDFWriter.addRawContent("row content rg\n"); 
    mPDFWriter.addText(30, 90, 10, "� CRL", Transformation.DEGREES_270_ROTATION); 

    mPDFWriter.newPage(); 
    mPDFWriter.addRawContent("[] 0 d\n"); 
    mPDFWriter.addRawContent("1 w\n"); 
    mPDFWriter.addRawContent("0 0 1 RG\n"); 
    mPDFWriter.addRawContent("0 1 0 rg\n"); 
    mPDFWriter.addRectangle(40, 50, 280, 50); 
    mPDFWriter.addText(85, 75, 18, "Android Docs"); 

    mPDFWriter.newPage(); 
    mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER_BOLD); 
    // mPDFWriter.addText(150, 150, 14, "http://coderesearchlabs.com"); 
    mPDFWriter.addLine(150, 140, 300, 140); 

    String s = mPDFWriter.asString(); 
    return s; 
} 


     private void outputToFile(String fileName, String pdfContent, String encoding)  { //function which pushes the file to below path 
    //System.out.println("content" +fileName); 
    if(isSDPresent) 
    { 

     try { 
       File dir = new File(Environment.getExternalStorageDirectory() + "/pdf_Writer_Reader/"); 
       if (!dir.exists()) 
       { 
        dir.mkdirs(); 
       } 

       newFile = new File(dir,fileName); 
       if(!newFile.exists()) 
       { 
        newFile.createNewFile(); 
       } 

       try 
       { 
        FileOutputStream pdfFile = new FileOutputStream(newFile); 
        pdfFile.write(pdfContent.getBytes(encoding));    
        pdfFile.close(); 
       } 
       catch(FileNotFoundException e) { 
       Log.e("Ex==",""+e.toString()); 
       } 
     } 
     catch(IOException e) 
     { 
     Log.e("Ex==",""+e.toString()); 
     } 
    } 

任何人可以就如何從SD卡的幫助加載PDF將感謝建議。

回答

0

您在GitHub上提到的項目基於MuPDF庫。在同一個GitHub的項目,你有一個活動閱讀PDF的例子 - MuPDFActivity讀取PDF中的活動的一部分是在開始 -

private MuPDFCore openFile(String path) 
{ 
    int lastSlashPos = path.lastIndexOf('/'); 
    mFileName = new String(lastSlashPos == -1? path : path.substring(lastSlashPos+1)); 
    System.out.println("Trying to open "+path); 
    try 
    { 
     core = new MuPDFCore(path); 
     // New file: drop the old outline data 
     OutlineActivityData.set(null); 
    } 
    catch (Exception e) 
    { 
     System.out.println(e); 
     return null; 
    } 
    return core; 
} 

使用你叫

public static native void drawPage(Bitmap bitmap, int pageW, int pageH, 
int patchX, int patchY, 
int patchW, int patchH); 
的MuPDFCore實例

將特定頁面寫入位圖。要了解更多信息,請檢查具有工作示例的項目以顯示pdf。

+0

所以我必須通過路徑在ChoosePDFActivity背景的方法OpenFile方法 – user1048958

+0

您可以使用'中openFile()'方法在任何活動 – asloob

+0

那麼drawPage的用途是什麼,我們應該如何使用..? – user1048958

0

//註釋你中openFile(字符串路徑)和替換功能下面的代碼...

private MuPDFCore openFile() 
{ 
    String storageState = Environment.getExternalStorageState(); 
    File path, file; 
    MuPDFCore core; 

    if (Environment.MEDIA_MOUNTED.equals(storageState)) 
    { 
     System.out.println("Media mounted read/write"); 
    } 
    else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(storageState)) 
    { 
     System.out.println("Media mounted read only"); 
    } 
    else 
    { 
     System.out.println("No media at all! Bale!\n"); 
     return null; 
    } 
    path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 
    file = new File(path, "TheAccidentalBully.pdf"); 
    System.out.println("Trying to open "+file.toString()); 
    try 
    { 
     core = new MuPDFCore(file.toString()); 
    } 
    catch (Exception e) 
    { 
     System.out.println(e); 
     return null; 
    } 
    return core; 
} 

     and onCreate 

      if (core == null) { 
     Intent intent = getIntent(); 
     if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
      core = openFile(); 
      SearchTaskResult.set(null); 
     } 
+0

你可以編輯我的答案,而不是添加一個新的。 – asloob

+0

@asloob我嘗試了asloob,但它不允許我編輯... – user1048958

+0

@asloob它可以突出顯示文本和觸摸顯示該pdf上的文本的含義,我們正在查看... – user1048958