Question

iTextPDF in android sets a border in the images in the PDF which I don't want. How to solve this issue. NB:- I searched a lot for a solution,I couldn't find any.

com.itextpdf.text.Document pdfDocument  =   new com.itextpdf.text.Document();
    pdfDocument.setPageSize(PageSize.A4);
    pdfDocument.setMargins(72f,72f,72f,72f);                                                                        

    File extStore           =   Environment.getExternalStorageDirectory();

    try {
        PdfWriter.getInstance(pdfDocument,new FileOutputStream(extStore.getAbsolutePath()+"/MyNewPDF.pdf"));
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (DocumentException e1) {
        e1.printStackTrace();
    }

    pdfDocument.open();

    BaseColor colorBlack    =   new BaseColor(0, 0, 0);                                                             

    /*Setting the Image Header*/
    Drawable myImage                =       getResources().getDrawable(R.drawable.pdf_logo);
    Bitmap bitmap                   =       ((BitmapDrawable)myImage).getBitmap();
    ByteArrayOutputStream stream    =       new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();

    Image imageHeader = null;

    try {
        imageHeader = Image.getInstance(bitmapdata);
    } catch (BadElementException e1) {
        e1.printStackTrace();
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    imageHeader.setAlignment(Element.ALIGN_RIGHT);                                                                  
    Font font = new Font(Font.FontFamily.UNDEFINED,15,Font.ITALIC);
    try {
        pdfDocument.add(imageHeader);
        pdfDocument.add(Chunk.NEWLINE);
        /*Setting the heading line*/
        pdfDocument.add(new Paragraph("Document Name: Qwert Document ID: Dgj",font));
    } catch (DocumentException e1) {
        e1.printStackTrace();
    }  


    /*Black Line*/
    Chunk linebreak         =   new Chunk(new LineSeparator(1f, 100f, colorBlack, Element.ALIGN_CENTER, -1));
    try {
        pdfDocument.add(linebreak);
        pdfDocument.add( Chunk.NEWLINE );
        pdfDocument.add( Chunk.NEWLINE );
        pdfDocument.add( Chunk.NEWLINE );
        pdfDocument.add( Chunk.NEWLINE );
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    /*Main Image*/
    Bitmap bitMap                   =   mBitmapArray.get(mPager.getCurrentItem()).mBitmap;
    ByteArrayOutputStream streamNew =   new ByteArrayOutputStream();
    bitMap.compress(Bitmap.CompressFormat.PNG, 100, streamNew);
    byte[] byteArray = streamNew.toByteArray();

    Image imageMain = null;
    try {
        imageMain = Image.getInstance(byteArray);
    } catch (BadElementException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    imageMain.setAlignment(Element.ALIGN_CENTER);
    try {
        pdfDocument.add(imageMain);
        pdfDocument.add( Chunk.NEWLINE );
        pdfDocument.add( Chunk.NEWLINE );
        pdfDocument.add( Chunk.NEWLINE );
    } catch (DocumentException e) {
    }


    Chunk linebreak2        =   new Chunk(new LineSeparator(2f, 100f, colorBlack, Element.ALIGN_CENTER, -1));
    try {
        /*Black Line*/
        pdfDocument.add(linebreak2);
        /*Last Line*/
        pdfDocument.add(new Paragraph("Sent by Traveler ID"));
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    pdfDocument.close();

Thanking you in advance

Was it helpful?

Solution

I solved the issue by using another library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top