Search code examples
androiditextitextg

Create pdf in Android, setBorderColor


I use iText library to create pdf, in Java Project setBorderColor work, I use:

c1 = new PdfPCell(new Phrase("182-432-23-23",smallFontBold));
c1.setBorderColor(BaseColor.DARK_GRAY);

but in Android Project in this code I have error:

c1 = new PdfPCell(new Phrase("182-432-23-23",smallFontBold));
c1.setBorderColor(Color.GRAY);

error:

The method setBorderColor(Color) in the type Rectangle is not applicable for the arguments (int)

Solution

  • It seems that Color.GRAY is an int value, whereas the setBorderColor() method expects an instance of the BaseColor class. What type of object is Color? There is no such class in iText and java.awt.Color is forbidden on Android (as are all AWT classes).

    Try:

    c1.setBorderColor(BaseColor.GRAY);
    c1.setBorder(Rectangle.BOX);
    c1.setBorderWidth(1);
    

    I added the two extra lines just to make sure that there is a border; setting the color if no border needs to be drawn has no effect.

    Important: if you are using a version of iText that doesn't have a BaseColor class, then you are using the wrong version of iText. On Android, you need to use iTextG.