2013-05-29 8 views
1

프로젝트에서 PDF 뷰어 라이브러리를 구현해야합니다. 다음을 작성합니다. https://github.com/bhavyahmehta/Pdf-Reader---Writer/blob/master/Pdf_%20Reader_Writer/README.md 이 예제에서는 PDF를 만들고 뷰어에서로드하는 중입니다. 는 사실은 내가 여기 어떻게 라이브러리 작품을 이해 라이브러리 생성 및 로딩 PDF에 대한 코드의 다음 조각입니다 :Pdf-Reader를 통한 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()); 
     } 
    } 

이 사람이 감사 할 것이다 도움의 sdcard에서 PDF를로드하는 방법을 제안 할 수는.

답변

0

GitHub에서 언급 한 프로젝트는 MuPDF 라이브러리를 기반으로합니다. 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 인스턴스를 사용하여 같은 GitHub의 프로젝트에서 당신은 활동에서 PDF 읽기의 예를 가지고

비트 맵에 특정 페이지를 작성합니다. pdf를 표시 할 수있는 샘플이있는 프로젝트를 더 확인하십시오.

+0

난 당신이'openFile()'방법을 사용할 수 있습니다 – user1048958

+0

ChoosePDFActivity 배경 방법에 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