Kittu
Kittu

Reputation: 61

android image not appearing in iText pdf

I am trying to convert an image into Pdf format through iText library in android platform. I was able to generate Pdf in the desired location but the image is not in the pdf i.e, a blank pdf is generated. Below is my code. Please correct me if you find any mistake. I included 'itextg-5.5.4' in the 'libs' folder and included as a library. No compile time errors in code.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bundle extras = data.getExtras();
    photo = extras.getParcelable("data");
    File outFile = new File(pdfFolder,imageName);//pdfFolder exists at a location and imageName=timestamp
     FileOutputStream fos = new FileOutputStream(outFile);
     photo.compress(Bitmap.CompressFormat.JPEG, 100, fos);

    Date date = new Date() ;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);

    File myFile = new File(pdfFolder , timeStamp + ".pdf");

    OutputStream output = new FileOutputStream(myFile);

    //Step 1
    Document document = new Document(PageSize.A4, 25, 25, 30, 30);;

    //Step 2
    PdfWriter writer=PdfWriter.getInstance(document, output);

    //Step 3
    document.open();

    //Step 4 Add content

    document.add(new Paragraph("Simple Image"));

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    Image image=Image.getInstance(stream.toByteArray());

    document.add(image);
    document.close();
    output.close();

Upvotes: 2

Views: 1028

Answers (2)

Kittu
Kittu

Reputation: 61

Below is the fully functional code:

File myFile;
public void createpdf() throws DocumentException, IOException {

        //Create time stamp to name the pdf

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        myFile = new File(pdfFolder, timeStamp + ".pdf");

        OutputStream output = new FileOutputStream(myFile);

        //Step 1
        Document document = new Document(PageSize.A4, 25, 25, 30, 30);
        ;

        //Step 2
        PdfWriter.getInstance(document, output);

        //Step 3
        document.open();

        //Step 4 Add content

        Paragraph preface = new Paragraph("TITLE_GOES_HERE");
        preface.setAlignment(Element.ALIGN_CENTER);

        document.add(preface);


    for (int i=0;i<photoNames.size();i++) {
    //'photoNames' is an ArrayList<String> holding names of images captured
        Image image = Image.getInstance(photoNames.get(i));

        int indentation = 0;

        float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
                - document.rightMargin() - indentation) / image.getWidth()) * 100;
        image.scalePercent(scaler);

        document.add(image);

    }
        //Step 5: Close the document
        document.close();
        output.close();

Upvotes: 1

Kittu
Kittu

Reputation: 61

i found a solution by myself as below. Replaced

 Image image=Image.getInstance(stream.toByteArray());

with

 Image image=Image.getInstance(outFile.toString());

Reason: Image.getInstance("ImageName"). I was using ByteArray which couldn't recognize the Image.

Upvotes: 1

Related Questions