2012-03-21 14 views
1

私はAndroid上にiTextで働いていると、これは基本的に私のシナリオです:AndroidのiTextのPDF作成

それが作成した文字列を使用し 内の文字列を取得するためにXMLを解析 XMLデータを取得するためのHTTP呼び出しを実行しますPDF

私はXMLから引き出している文字列は、Web上の別のPDFから物理的に来るコード化されたデータです。

私はデコード処理を行って、デコードされた文字列を取得しました。これで、適切に保存できるようにするために、APIのいくつかの形式に書き込む必要があります。

私は現在、iTextのに働いていると私は、段落を追加しようとする - それは私が期待まさに行い、文字列を書き込みます(そのタイトルや作者などのようなタグを含む)

単純に方法はありますこの文字列をPDFドキュメントに書き込んで適切に保存しますか?

(この問題について私ができる限り最善の問題を言い換えるためにこれについて先に質問を削除しました)

答えて

2

が、これはusinf droidText libaryことにより、簡単なプログラムです

try { 
     Document document = new Document(); 
     File root = new File(Environment.getExternalStorageDirectory(), 
       "foldername"); 
     String sdcardhtmlpath = root.getPath().toString() + "/filename.pdf"; 
     PdfWriter.getInstance(document, 
       new FileOutputStream(sdcardhtmlpath)); 
     document.open(); 
     Paragraph paragraph = new Paragraph("your string");//add ur string here 
     paragraph.setAlignment(Element.ALIGN_LEFT); 
     document.add(paragraph); 
     document.close(); 
    } catch (Exception e) { 
     Log.v("PDFcreation", e.toString()); 
    } 
+1

niceeee work .... –

0
public void createPDF() 
    { 
     Document doc = new Document(); 


     try { 
       String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "//pdf"; 

       File dir = new File(path); 
        if(!dir.exists()) 
         dir.mkdirs(); 

       Log.d("PDFCreator", "PDF Path: " + path); 


       File file = new File(dir, "sample1.pdf"); 
       FileOutputStream fOut = new FileOutputStream(file); 

       PdfWriter.getInstance(doc, fOut); 

       //open the document 
       doc.open(); 

       Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using iText library"); 
       Font paraFont= new Font(Font.COURIER); 
       p1.setAlignment(Paragraph.ALIGN_CENTER); 
       p1.setFont(paraFont); 

       //add paragraph to document  
       doc.add(p1); 

       Paragraph p2 = new Paragraph("This is an example of a simple paragraph"); 
       Font paraFont2= new Font(Font.COURIER,14.0f,Color.GREEN); 
       p2.setAlignment(Paragraph.ALIGN_CENTER); 
       p2.setFont(paraFont2); 

       doc.add(p2); 

       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream); 
       Image myImg = Image.getInstance(stream.toByteArray()); 
       myImg.setAlignment(Image.MIDDLE); 

       //add image to document 
       doc.add(myImg); 

       //set footer 
       Phrase footerText = new Phrase("This is an example of a footer"); 
       HeaderFooter pdfFooter = new HeaderFooter(footerText, false); 
       doc.setFooter(pdfFooter); 



     } catch (DocumentException de) { 
       Log.e("PDFCreator", "DocumentException:" + de); 
     } catch (IOException e) { 
       Log.e("PDFCreator", "ioException:" + e); 
     } 
     finally 
     { 
       doc.close(); 
     } 

...この方法を試してみてください。これを試してみてください。

0
try{ 
     File obj=new File(Environment.getExternalStorageDirectory(),"pdf"); 
     if(!obj.exists()) 
     { 
      obj.mkdirs(); 
     } 
     String name=obj.getAbsolutePath()+"/mypdf.pdf"; 
     FileOutputStream fop=new FileOutputStream(name); 
     Document doc=new Document(); 

     PdfWriter.getInstance(doc, fop); 
     doc.open(); 
     Font font=new Font(FontFamily.TIMES_ROMAN, 20, Font.STRIKETHRU, BaseColor.GREEN); 
     Paragraph p=new Paragraph("sample pdf",font); 
     p.setAlignment(Element.ALIGN_LEFT); 

     doc.add(p); 
     doc.close(); 
     } 
     catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
関連する問題