【问题标题】:The method getInstance(byte[]) is undefined for the type Document.. Android方法 getInstance(byte[]) 未定义类型 Document.. Android
【发布时间】:2014-03-24 12:02:33
【问题描述】:

我正在使用 droidText 库生成 pdf

我有以下代码

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


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

            File dir = new File(path);
                if(!dir.exists()){
                     System.out.println("directory not exists");
                     dir.mkdirs();
             }else{
                 System.out.println("directory exirsts");
             }
             System.out.println("path="+path);

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


            File file = new File(dir, "sample.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 DroidText");
            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);

//

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




             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setDataAndType(Uri.fromFile(file),"application/pdf");
             intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
             startActivity(intent);

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

}  

它可以工作.. 但是当我使用以下几行向它添加 Image 时,它​​说“方法 getInstance(byte[]) 未定义类型 Document”

   ByteArrayOutputStream stream = new ByteArrayOutputStream();
             Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.ic_launcher);
             bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
             Image myImg = Image.getInstance(stream.toByteArray());
             //myImg.setAlignment(Image.MIDDLE);                 
             add image to document
             doc.add(myImg);

帮我在文档中添加图片

【问题讨论】:

标签: android pdf


【解决方案1】:

根据Document.java,该类没有 getInstance 方法。 你想要Image.getInstance 按照这个StackOverflow question

例如

Image myImg = Image.getInstance(stream.toByteArray());
doc.add(myImg);

这不是一个重复的问题,但确实有一个答案可以回答您“帮助我在文档中添加图像”的陈述

【讨论】:

  • 请更新您的原始问题,包括您所做的以及您所做的结果。你说它不起作用但你没有描述它是否仍然有相同的错误信息或者错误信息消失了,但图像仍然没有出现,等等。
猜你喜欢
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多