Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a PNG with transparency to a PDF?

I need to add a PNG with transparency to a PDF, but honestly, I'm kinda lost.

Do I need to get the tRNS chunk of a PNG and add to my SMask? What to do when the PNG doesn't have a tRNS chunk?

I know that already exists this question but it doesn't have an answer and I didn't understand the most voted one.

Any help will be appreciated!

EDIT:

I'm trying to first mask one color, I take the IDAT chunk of the image and add to my stream and use the mask color on the /Mask tag:

6 0 obj
<<
/Type /XObject
/Subtype /Image
/Name /Im1
/Width 60
/Height 23
/BitsPerComponent 8
/Length 484
/ColorSpace /DeviceRGB
/Filter /FlateDecode
/Mask [0 0 0 0 0 0]
>>
stream
    % IDAT CHUNK %
endstream
endobj

But it doesn't open in Adobe, it shows the message "Insufficient Data For An Image" and on Foxit shows the image "twisted" and in the wrong color.

I'm using this image and this is my PDF.

I only want to mask out one color and I obtained that color using FreeImage.

PS: I didn't know if I should create a new question or update this one, sorry if this is the wrong protocol.

like image 770
Penachia Avatar asked Nov 07 '22 11:11

Penachia


1 Answers

Generally Acrobat displays that error when the image stream does not contain enough data. Based on the image dimension, bit depth, and color space representation you should be able to calculate how much is needed and if what you are supplying in the stream is less than that you would see such an error.

I think the problem is the PNG is an Indexed-color, so each pixel consists of an index into a palette. You specify the /ColorSpace as DeviceRGB where the BPC is 8 so that's 24 bits per pixel, you should be using an /Indexed colorspace with a base colorspace that's DeviceRGB instead.

I believe you are specifying the mask correctly, however I would start without worrying about the mask just to make sure you can get the base image right first when opening it in your PDF viewer for instance.

like image 106
JosephA Avatar answered Nov 15 '22 10:11

JosephA