如何在android中的pdf文件创建表中插入图像

时间:2017-02-16 05:19:33

标签: android android-layout android-studio itext pdf-generation

我正在从android中的用户输入创建PDF生成应用程序。我想在表格字段内添加图像。如何在表格列中添加图像?请解释一下。

public class MainActivity extends Activity {

    EditText et1,et2,et3,et4;
    Button Save;
    String edit1,edit2,edit3,edit4;
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell1, cell2,cell3,cell4, cell5,cell6,cell7, cell8,cell9,cell10;
    File cacheDir;
    final Context context = this;
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Thiru");



            else
                cacheDir=context.getCacheDir();
            if(!cacheDir.exists())
                cacheDir.mkdirs();

        et1=(EditText)findViewById(R.id.editText1);
        et2=(EditText)findViewById(R.id.editText2);
        et3=(EditText)findViewById(R.id.editText3);
        et4=(EditText)findViewById(R.id.editText4);
        img = (ImageView) findViewById(R.id.imageView);

        Save=(Button)findViewById(R.id.button1);
        Save.setOnClickListener(reportClickListener);


    }

OnClickListener reportClickListener= new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            edit1=et1.getText().toString();
            edit2=et2.getText().toString();
            edit3=et3.getText().toString();
            edit4=et4.getText().toString();

            String FILE = Environment.getExternalStorageDirectory().toString() + "/Thiru/" + "report.pdf";

            // Create New Blank Document
            Document document = new Document(PageSize.A4);

            // Create Pdf Writer for Writting into New Created Document
            try {
                PdfWriter.getInstance(document, new FileOutputStream(FILE));
                // Open Document for Writting into document
                document.open();
                // User Define Method


                addTitlePage(document);
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Close Document after writting all content
            document.close();

            Toast.makeText(MainActivity.this, "PDF File is Created."+FILE, Toast.LENGTH_LONG).show();
        }
    };

PdfPTable table = new PdfPTable(3);

// t.setBorderColor(BaseColor.GRAY);
// t.setPadding(4);
// t.setSpacing(4);
// t.setBorderWidth(1);

            PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Table Header 2"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Table Header 3"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);
            table.setHeaderRows(1);

            table.addCell("1.0");
            table.addCell("1.1");
            table.addCell("1.2");
            table.addCell("2.1");
            table.addCell("2.2");
            table.addCell("2.3");
            table.addCell(image);

            document.add(table);

document.newPage();
            //Toast.makeText(this, "PDF File is Created.", Toast.LENGTH_LONG).show();
        }


}

我已经知道如何从用户输入创建PDF文件。我需要的是如何在表格单元格中添加图像。如何添加图片?我想知道如何在PDF文件中添加URL中的图像。

2 个答案:

答案 0 :(得分:0)

以下是您可以在Image中添加徽标或标题Document的代码。您必须根据您的要求进行更改。

这里是snippet代码

public void addLogo(Document document) throws DocumentException {
    try { // Get user Settings GeneralSettings getUserSettings =

        Rectangle rectDoc = document.getPageSize();
        float width = rectDoc.getWidth();
        float height = rectDoc.getHeight();
        float imageStartX = width - document.rightMargin() - 315f;
        float imageStartY = height - document.topMargin() - 80f;

        System.gc();

        InputStream ims = getAssets().open("image.jpg"); // image from assets folder
        Bitmap bmp = BitmapFactory.decodeStream(ims);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);

        byte[] byteArray = stream.toByteArray();
        // PdfImage img = new PdfImage(arg0, arg1, arg2)

        // Converting byte array into image Image img =
        Image img = Image.getInstance(byteArray); // img.scalePercent(50);
        img.setAlignment(Image.TEXTWRAP);
        img.scaleAbsolute(200f, 50f);
        img.setAbsolutePosition(imageStartX, imageStartY); // Adding Image


        PdfPCell cellImage= new PdfPCell(img, true);  
        mTable.addCell(cellImage); //add image cell in table
        document.add(mTable);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

答案 1 :(得分:0)

您可以从网址导入图片: Add image to PDF from a URL?

您可以在表格单元格中添加图片: http://developers.itextpdf.com/examples/tables-itext5/adding-images-table

您可以混合使用并更改table.addCell(image);,如下所示

String imageUrl = "https://s.yimg.com/os/mit/ape/w/d8f6e02/dark/partly_cloudy_day.png";
Image imageFromWeb = Image.getInstance(new URL(imageUrl));
PdfPCell cellImageInTable = new PdfPCell(imageFromWeb , true);
table.addCell(cellImageInTable);