2012-05-15 2 views
1

Android에서 iText (here)를 사용하여 텍스트를 PDF로 변환하려고했지만 "파일을 찾을 수 없음"예외가 발생합니다. 코드는 다음과 같습니다.iText를 사용하여 Android에서 텍스트를 PDF로 변환

try 
     { 

      PdfWriter.getInstance(document, new FileOutputStream("hello.pdf")); 
      document.open(); 
      document.add(new Paragraph("Hello World")); 
      document.close(); 
      Log.d("OK", "done"); 
     } 
     catch (FileNotFoundException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (DocumentException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

도와 주시겠습니까? 이것은 내 경우에는 완벽하게 작동합니다 감사합니다

+0

파일 (파일 위치)? 프로젝트 폴더 또는 시스템 드라이브에 있습니까? –

+0

실제로 위 코드를 정확히 사용했습니다. 그래서 OS가 프로그램 폴더에 새 파일을 만들어야한다고 생각합니다. –

+0

https://code.google.com/p/droidtext/를 사용하시기 바랍니다. 이것은 안드로이드에 더 좋습니다. – Rusfearuth

답변

6

,

try 
    { 
     Document document = new Document(); 
     PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory() + "/hello.pdf")); 
     document.open(); 
     document.add(new Paragraph("Hello World")); 
     document.close(); 
     Log.d("OK", "done"); 
    } 
    catch (FileNotFoundException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (DocumentException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

그리고 매니페스트 파일에

,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

hello.pdf의 위치는 "/mnt/sdcard/hello.pdf"입니다. – user370305

+0

감사합니다.하지만 SD 카드가없는 경우에는 어떻게해야합니까? –

+0

Environment.getExternalStorageState()를 확인하기 만하면 파일을 만들기 전에 "/ mnt/sdcard"와 같은 내부 SD 마운트 지점의 경로를 반환합니다. – user370305

1

나를 위해이 코드가 작동 ...이

이 {

을 시도하십시오
 String path = Environment.getExternalStorageDirectory()+"/hello/"; 
     File file = new File(path+"hello.pdf"); 
     if(!file.exists()){ 
      file.getParentFile().mkdirs(); 
      try { 
       file.createNewFile(); 

      } 
      catch (IOException e) 
      { 
       // TODO Auto-generated catch block e.printStackTrace(); } 
      } 
     } 

     Document document = new Document(); 
     PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory() 
       +File.separator 
       +"hello" //folder name 
       +File.separator 
       +"hello.pdf")); 
     document.open(); 
     document.add(new Paragraph("Hello World "+txt.getText())); 
     document.add(new Paragraph("Hello World" +txt.getText())); 
     document.add(new Paragraph("Hello World" +txt.getText())); 
     document.add(new Paragraph("Hello World "+txt.getText())); 
     document.close(); 
     Log.d("OK", "done"); 
관련 문제