Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PdfPCell background color ignoring alpha channel of an rgba #276

Closed
alaingaudreau opened this issue Sep 29, 2019 · 5 comments
Closed

PdfPCell background color ignoring alpha channel of an rgba #276

alaingaudreau opened this issue Sep 29, 2019 · 5 comments

Comments

@alaingaudreau
Copy link

Is it expected behavior for PdfPCell to ignore the alpha channel value when you setBackgroundColor with a Color(r,g,b,a)?

I haven't seen anything in the docs/javadocs for this class regarding the alpha channel.

@razilein
Copy link
Contributor

razilein commented Oct 3, 2019

https://github.com/LibrePDF/OpenPDF/blob/master/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java#L2262

I've found this. The alpha channel is ignored, because it's value is not delivered at this point.

The alpha channel should be delivered to this method:
https://github.com/LibrePDF/OpenPDF/blob/master/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java#L2166

But I don't know how to set this. I've found that there is the parameter "S" which is for transparency.

See https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
on page 347 (Table 144)

Maybe someone else can help with this example:

    private static void cellWithRgb(Document doc) {
        PdfPCell redCell = new PdfPCell(new Paragraph("Red"));
        redCell.setBackgroundColor(new Color(255, 0, 0, 50));
        
        PdfPCell greenCell = new PdfPCell(new Paragraph("Green"));
        greenCell.setBackgroundColor(new Color(0, 255, 0, 100));
        
        PdfPCell blueCell = new PdfPCell(new Paragraph("Blue"));
        blueCell.setBackgroundColor(new Color(0, 204, 255, 255));

        final PdfPTable table = new PdfPTable(1);
        table.addCell(redCell);
        table.addCell(greenCell);
        table.addCell(blueCell);

        doc.add(table);
    }

@sixdouglas
Copy link
Contributor

sixdouglas commented Oct 8, 2019

Here you have an example of the ContentByte in use:

https://github.com/LibrePDF/OpenPDF/blob/master/pdf-toolbox/src/test/java/com/lowagie/examples/directcontent/colors/Transparency.java

With this sample you see how to write table using DirectContent:

https://github.com/LibrePDF/OpenPDF/blob/master/pdf-toolbox/src/test/java/com/lowagie/examples/directcontent/pageevents/EndPage.java

Maybe you can manage using this for the background color. Here is a sample of code.

Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("transparency-1.pdf"));
    document.open();
    Rectangle page = document.getPageSize();
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(0.5f);
    cb.setGState(gs1);
    PdfPTable head = new PdfPTable(3);
    for (int k = 1; k <= 6; ++k) {
        PdfPCell cell = new PdfPCell(new Phrase("head " + k));
        cell.setBackgroundColor(new Color(0, 204, 255));
        head.addCell(cell);
    }
    head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
    head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(), cb);
    cb.restoreState();
    cb.sanityCheck();
}
catch (Exception de) {
    de.printStackTrace();
}
document.close();

But as you can see the borders and the text inherit also of the transparency :/

@mkl-public
Copy link
Contributor

Related: https://stackoverflow.com/a/56954685/1729265

Support for transparent cell background colors has been added in iText 5.5.7, more exactly in git commit 09ccaa728830a404e92fc3040dd3626198576fef.
Depending on the exact version of iText you may try and use a PdfPCellEvent implementation instead of setting the background color with setBackgroundColor. In that implementation you'd have to first save the graphics state, then set a PdfGState with transparency, fill the cell rectangle with the color, and restore the graphics state again.

@sixdouglas
Copy link
Contributor

@asturio maybe we need to build a release to make these changes available to all

@testn
Copy link

testn commented Apr 15, 2020

so this one should be closed?

@asturio asturio closed this as completed Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants