Embedded Images and ExportAsTiff(FilterWriter)

46 views
Skip to first unread message

Mark Mann

unread,
Mar 5, 2021, 12:34:08 PM3/5/21
to PDFTron SDK
I think I am a little dense here because I cannot seem to figure out how to properly use the FilterWriter in this situation.

I want to get the embedded image of a PDF directly into a byte array(I'll take any format, png, tiff, original, whatever) and I cannot seem to figure it out. 

I have the following:

                        var image = new pdftron.PDF.Image(element.GetXObject());
                        FilterWriter fw = new FilterWriter();
                        image.ExportAsTiff(fw);

Of course if I change ExportAsTiff to a file it works fine, but when I do it this way, anything I try to do with the FilterWriter(fw) causes a protected memory error. I'm sorry I can't figure this out as I know it is probably simple, but the FilterWriter vs. FilterReader just seem to consistently confuse me and ExportAsTiff only returns a FilterWriter.

Ryan

unread,
Mar 5, 2021, 4:58:00 PM3/5/21
to PDFTron SDK
No this is a great question. The main step missing, is to give FilterWriter a sink filter to write to. 

The following code creates a MemoryFilter as a sink, and then after it is filled with the TIFF data, we switch it to an source, and then for demonstration purposes we write to disk, but at this point you could give it a FilterReader and send the data to some other source.

pdftron.Filters.MemoryFilter mf = new pdftron.Filters.MemoryFilter(0, false); // zero, since we don't know how big the TIFF will be, and false = sink
FilterWriter fw = new FilterWriter(mf);
image.ExportAsTiff(fw);
// now the MemoryFilter contains the TIFF data
mf.SetAsInputFilter(); // switch the MemoryFilter to a source
mf.WriteToFile(path_to_disk, false); // for demo purpose write to disk

Mark Mann

unread,
Mar 18, 2021, 5:22:15 PM3/18/21
to PDFTron SDK
Ryan,

This worked perfectly, thank you. However I ran into problem number 2. This does not work with InlineImages. I found this link: https://www.pdftron.com/documentation/samples/dotnetcore/cs/ElementReaderAdvTest so I was able to turn the Inline Image into a byte array, which seems to look like what I am expecting, but when I try to load that byte array into a standard Windows GDI Bitmap I am getting errors. Do I need to do anything else to the array in order for it to become a Bitmap object in C#?

I saw in the above link there is a section of code which makes it appear an Element can turn into a bitmap, however I was unable to find this function/method in the actual intellisense API when using the SDK:

// System.Drawing.Bitmap bmp = image.GetBitmap(); // bmp.Save(output_path + "reader_img_extract_" + image_counter.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);

Ryan

unread,
Mar 19, 2021, 2:22:20 PM3/19/21
to PDFTron SDK
Could you provide the input PDF file, and the code you tried.


>I saw in the above link there is a section of code which makes it appear an Element can turn into a bitmap, however I was unable to find this function/method in the actual intellisense API when using the SDK:

Message has been deleted

Ryan

unread,
Mar 23, 2021, 2:36:47 PM3/23/21
to PDFTron SDK
Message from poster (attached file has been omitted)

I am happy to replace the code with anything, all I care about is ending up with a System.Drawing.Bitmap or png or tiff or anything I can load and view as an image.

                            Image2RGBA img_conv = new Image2RGBA(element, true);
                            FilterReader fr = new FilterReader(img_conv);
                            byte[] buff = new byte[img_conv.Size()];
                            fr.Read(buff);
                            imageStrings.Add(System.Convert.ToBase64String(buff)); //You can ignore this for the most part. I am just getting a Base64 string to send it to another function
                            fr.Dispose();
                            img_conv.Dispose();

I also looked at this code: https://www.pdftron.com/documentation/samples/dotnetcore/cs/ElementReaderAdvTest and tried to do the image.GetBitmap() line but the system does not recognize .GetBitmap.
 
PDF is also attached (omitted)

Ryan

unread,
Mar 25, 2021, 6:11:14 PM3/25/21
to PDFTron SDK
I processed the PDF you provided, and all the inline images came out as expected.

Attached is the code I used.

If that doesn't work, then please provide full details of exactly what didn't work and how exactly you expected it to work.

ImageExtractTest.cs
Reply all
Reply to author
Forward
0 new messages