Android iText PDF创建

时间:2012-03-21 21:54:59

标签: java android itext

我在Android上使用iText,这基本上就是我的情景:

对GET XML数据运行HTTP调用 解析XML以获取其中的字符串 使用字符串创建PDF

我从XML中提取的字符串是实际来自网络上另一个PDF的PDF格式数据。

我已经完成了解码过程,因此它是一个已解码的字符串...现在我只需要让它写入某种形式的API,以便它能够正确保存。

我目前正在使用iText,当我尝试添加段落时 - 它完全符合我的预期并写入字符串(包括标题,如标题和作者等)

有没有办法简单地将此字符串写入PDF文档并正确保存?

(我之前删除了一个关于此问题的问题,以便尽可能地重新解释这个问题。)

3 个答案:

答案 0 :(得分:2)

试试这种方式......

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 :(得分: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();
         }

这是使用droidText库的简单程序。试试这个有用。

答案 2 :(得分: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();
        }