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.
}
}
}