Embedded Images and ExportAsTiff(FilterWriter)

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();

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.

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);
// 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