Insert image in android

0

I'm doing an apk that creates a pdf file with Java iText PDF 5.4.4.4 , I've already been able to create the file, but I can not insert an image.

This is the code I hope you can help me.

    package com.example.user.pruebaimagen;


import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Image;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {
    private Button b;
    private PdfPCell cell;
    private String textAnswer;
    private com.itextpdf.text.Image bgImage;
    ListView list;
    private String path;
    private File dir;
    private File file;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Document documento = new Document();
        b = (Button) findViewById(R.id.button1);

        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDForden/pdf";
        dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    createPDF();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
            }
        });


    }

    public void createPDF() throws FileNotFoundException, DocumentException {



        Document doc = new Document();
        try {

            Log.e("PDFCreator", "PDF Path: " + path);
            SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
            file = new File(dir, "Ordendeservicio.pdf");
            FileOutputStream fOut = new FileOutputStream(file);
            PdfWriter writer = PdfWriter.getInstance(doc, fOut);



            //open the document
            doc.open();

            Image myImage = Image.getInstance(getClass().getResource("UserSignature/Firma.png"));
            doc.add(myImage);

            PdfPTable pTable7 = new PdfPTable(1);
            pTable7.setWidthPercentage(100);
            cell = new PdfPCell();
            cell.setColspan(1);
            pTable7.addCell(cell);


            cell = new PdfPCell();
            cell.addElement(new Paragraph(" hola" ));
            pTable7.addCell(cell);
            cell = new PdfPCell();
            cell.addElement(new Paragraph(" hola tu" ));
            pTable7.addCell(cell);
            doc.add(pTable7);


            doc.close();
            Toast.makeText(getApplicationContext(), "PDF generado con exito", Toast.LENGTH_LONG).show();
        } catch (DocumentException ex) {
            Log.e("PDFCreator", "DocumentException:");// Atrapamos excepciones concernientes al documentoo.
        } catch (IOException ex) {
            Log.e("PDFCreator", "ioException:");// Atrapamos excepciones concernientes al I/O.
        }

}

}
    
asked by Scorm32 17.04.2018 в 02:08
source

1 answer

0

I use Java iText PDF in a project and to add an image I use this code:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.TuImagen);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        Image imagen = Image.getInstance(stream.toByteArray());
        imagen.scaleToFit(150, 150);
        document.add(imagen);

Where R.drawable.TuImage is the image you want to publish and that you have to add to your Resources- > res / drawable / TuImagen.jpg.

I hope it serves you.

Greetings.

    
answered by 22.04.2018 в 14:11