I want to create a pdf with partially transparent polygons as the ellipse in the below image using libharu. The polygons come as RGBA, but libharu only has these fill methods:
HPDF_Page_SetCMYKFill()
HPDF_Page_SetGrayFill()
HPDF_Page_SetRGBFill()
There is no "A" channel for rgb. (There is a transparency mask for the whole image, but as I understand, this only makes a range of colors invisible in the whole image, which is not what I want.)
I am new to libharu, and not an expert in color schemes or PDF, so solutions on all these levels might work...
The following code works as a general solution as proven by Duke (the Original Poster).
The generic means to add variability in a PDF file is, first to declare a set of optional "Graphics States" and transparency was added in version 5 (%PDF-1.4) as an Extended Graphics State (/ExtGState) commonly assigned to /GS0 /GS1 /GS2.... but the declarative naming scheme is optional.
Thus in the PDF internal document resources you add for example <</ExtGState<</GS0 0.25/GS1 0.5 /GS...>>>> as required. Then whilst building a page those states can be introduced (usually seen as
q /GS0 gs
for any following objects until reset when required (usually Q for previous) for the subsequent page objects.Every PDF builder will have some similar methodology and during discussion the following was proposed as the method for a minimal example using LibHaru. using
HPDF_CreateExtGState(pdf_document_)
andHPDF_Page_SetExtGState(pdf_page_, gstate)
The result will be a Page Definition with
/ExtGState
labled /E1/E1 points to the Extended Graphic State, in this case just a change in color alpha but could have other extended attributes
then later in page contents, that named state can be applied at that time as if /ca 0.45 was included
I have condensed the true PDF result here to show them in less lines. So object 7 in notepad is laid out like below, and it is easy to edit from 0.45 to 0.75 opacity to experiment as seen on the above right in a real time preview (not Acrobat).
NOTE: I use the term opacity for the alpha channel since 0 with infinite floats to 1 is a range from fully transparent (0) to fully opaque (1).
This can mean in RGBA terms that [0 0 0 0] is no color at all (totally translucent) while [1 1 1 1] is full color (totally solid white). But more confusing it means as a single "color" transparency is [# # # 0] to [# # # 1] thus on a scale of 256 bytes in images, all are shown as in effect a mono black softmask when stored and extracted independently, but that's a different very broad topic!