Adding png image to PDF using Flate compression.

428 views
Skip to first unread message

Support

unread,
Apr 9, 2009, 3:44:35 PM4/9/09
to PDFTron PDFNet SDK
Q: How can I add a png image to a pdf without getting a jpeg encoding
(DCTDecode).
I want to have in the pdf this image in the same quality (lossless) as
it is in the file (This (and transparency) is the reason why I did not
generate a jpg file).
This is very important for us, as we are dealing with small text
within the image and with jpeg compression this looks not the best.

I tried already the following code (I added this code to the addimage
sample deployed with pdfnet):

img = Image.Create(doc,
@"sig_transparent.png");
element = bld.CreateImage(img, new Matrix2D
(100, 0, 0, 100, 50, 50));
writer.WritePlacedElement(element);
// results in jpeg compression (dctdecode) (as
expected)

Obj flate_hint = objset.CreateName("[/
Flate]");
//Obj flate_hint = objset.CreateName("Flate");
img = Image.Create(doc,
@"sig_transparent.png", flate_hint);
element = bld.CreateImage(img, new Matrix2D
(100, 0, 0, 100, 150, 50));
writer.WritePlacedElement(element);
// results in jpeg compression (dctdecode) ?!
not as expected (tried with “Flate” and “[/Flate]”)

Obj raw_hint = objset.CreateName("RAW");
img = Image.Create(doc,
@"sig_transparent.png", raw_hint);
element = bld.CreateImage(img, new Matrix2D
(100, 0, 0, 100, 250, 50));
writer.WritePlacedElement(element);
// results without jpeg compression (dctdecode)
for the image data - but as expected data are not compressed.
// alpha values are with dct compression
// --> quality of image is ok
// --> but size of image in pdf file is not
acceptable -> i want the rgb stream with flatedecode compressed (if
this is a valid option)

// without transcoding
bmp = new System.Drawing.Bitmap
(@"sig_transparent.png");
StdFile pngfile = new StdFile
(@"sig_transparent.png", StdFile.OpenMode.e_read_mode);
FilterReader reader_png = new FilterReader
(pngfile);

img = Image.Create(doc, reader_png,
bmp.Width, bmp.Height, 8,
ColorSpace.CreateDeviceRGB(),
Image.InputFilter.e_flate); // the format
of the input data stream
pngfile.Close();

element = bld.CreateImage(img, new Matrix2D
(100, 0, 0, 100, 50, 150));
writer.WritePlacedElement(element);
// results in invalid image


------
A: You can use the following hint to compress the image using Flate
(i.e. PNG) compression:

Obj flate_hint = objset.CreateArray();
flate_hint.PushBackName("Flate");
flate_hint.PushBackName("Level");
flate_hint.PushBackNumber(9); // Maximum compression
...

Image img = Image.Create(doc, @"sig_transparent.png", flate_hint);


This hint corresponds to [/Flate /Level 9] hint referred in PDFNet API
Reference
http://www.pdftron.com/pdfnet/html/classpdftron_1_1PDF_1_1Image.html#c98b19bbffd54c4870a9df1e47697568.

Support

unread,
Apr 15, 2009, 3:26:49 PM4/15/09
to PDFTron PDFNet SDK
Thank you, this works perfect for me!

> ------
> A: You can use the following hint to compress the image usingFlate
> (i.e. PNG) compression:
>
> Obj flate_hint = objset.CreateArray();
> flate_hint.PushBackName("Flate");
> flate_hint.PushBackName("Level");
> flate_hint.PushBackNumber(9); // Maximum compression
> ...
>
> Image img = Image.Create(doc, @"sig_transparent.png", flate_hint);
>
> This hint corresponds to [/Flate/Level 9] hint referred in PDFNet API
> Referencehttp://www.pdftron.com/pdfnet/html/classpdftron_1_1PDF_1_1Image.html#....
Reply all
Reply to author
Forward
0 new messages