Getting a CGImage out of a PDF file - c

I have a PDF file where every page is a (LZW) TIFF file. I know this because I created it. I want to be able to load it and save it as a bunch of TIFF files.
I can open the PDF file with CGPDFDocumentCreateWithURL, and get a page. I can even draw the page onto the screen.
What I WANT to do is draw the page into a bitmapContext, so that I can use CGBitmapContextCreateImage to get the image into a CGImageRef. However, in order to create a bitmap context, I need to know the size and resolution of the image. I can't seem to find out how to get either a CGPDFDocument or a CGPDFPage to tell me the resolution of the image object on that page.
Is there an easier way to do this that I'm not realizing?
thanks.

Ghostscript will work for you here :
gs -sDEVICE=tiff32nc -sOutputFile=foo-Page%d.tif foo.pdf
For 2 page document foo.pdf you should get :
foo-Page1.tif
foo-Page2.tif
From memory I think the output resolution from GS is that of the containing Page, not necessarily the resolution of the embedded file (unless these are the same to begin with).
If this is the case and you want to recover the image as it was originally res-wise, you can use iText (java) or iTextSharp(.net) to get to the image content stream (ie. Bytes) and write them out to disk in the format of your choice, after converting the content stream into a PdfImage iirc.
Hope the ghostscript option is applicable to save writing yet another utility...

Related

is base-64 just for jpeg or gif? how about a tiff? how can I convert string (which is part of XML and belong to a tiff file) to byte[]?

I wrote this in my application for displaying a TIFF Image :
byte[] b = convert.frombase64("ADFsf/s1ugdGHREHR/+/235gjhjhfcg/+kdhjgvkhfv/gcngcxsfdzsdf......=")
but It doesn't work. I received this message on loading the tiff :
run-time error '31037'
system error &H800401C2 (-2147221054)
I don't know exactly why?
When I save this tiff Image in this way :
file.writeallbytes("z.tiff",b);
I can open it , It means, It saved correctly.
now my problem is, I can`t display it in my application and image loading has got some problems.
thanks
Base-64 is certainly not only for JPEG or GIF. It can be used to represent any string of binary data (including plain text.) The base-64-encoded data you gave is malformed (as can be seen by writing the bytestream to a .tiff file), though.
My issue was done!
byte[] b = convert.frombase64("ADFsf/s1ugdGHREHR/+/235gjhjhfcg/+kdhjgvkhfv/gcngcxsfdzsdf......=")
method: convert.frombase64 -> can be used for any string of binary data
My problem was about loading a tiff file.
Firstly I must identify the pages in tiff image.
I split my tiff image to single pages by frame dimension method.

SSRS Data Driven Subscription that customizes DeviceInfo settings like PageWidth, PageHeight?

I want to set up data driven subscriptions to mass output png files. The problem is that adding a new Extension for png in rsreportserver.config under Configurations/Extensions/Render only gives one fixed size of png file.
Report A really ought to output a 6in x 3in png file and report B ought to output a 6in x 4in png file.
Yes, I could create multiple entries in rsreportserver.config but this is confusing for end users as they show up on all users' export dropdowns needlessly.
I proposed doing the mass image generation with an external program that generates a custom url for each png (DeviceInfo settings can be part of the url) and uses WebClient.DownloadFile() in a loop, but my supervisor is currently really locked into the idea of data driven subscriptions for whatever reason.
Per #iamdave's suggestion, just setting the overarching page dimensions in report designer does give a suitably sized png file via data driven subscription without having to hardcode png dimensions in rsreportserver.config.
The reason I didn't initially notice this was the reports in question were graphs that were only ever used as subreports on an encompassing megareport and had never really been run as individual standalone reports. When used as a subreport, page dimensions never came into play, so they were left at the default 8.5x11.

Saving image from Image object to isoStorage?

I've been googling around, and all the different answers has got me confused.
In my app I retrieve a contact picture, and put it in an Image object to be displayed to the user.
But I want to save the image to isostorage, and later retrieve it again.
How do I do this?
Can I use the Image.Source for anything?
You cannot get a filename or an ISO storage file.
The only use for ISO storage is to store a file of the bitmap and open a stream to that file to recreate a bitmap later.
The bitmap can then be assigned to the Image.Source.

how to override image in media library in WP7

I want to save image to media library in windows phone 7. I`m using this example http://msdn.microsoft.com/en-us/library/ff769549(v=VS.92).aspx . It works fine, the only problem that i have is that after image modification i call save procedure with the same file name, exactly like in example
MediaLibrary library = new MediaLibrary();
Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
myFileStream.Close();
but modification is saved to another file, even thought i use the same file name when i call SavePicture (and i want to override the image file). What am i doing wrong?
Reading between the lines a little you are seeing a new picture appear in the phone saved pictures collection where you were expecting an existing one to be replaced?
You should note that the code you have referenced creates duplicate pictures. One is stored in the phones saved pictures collection and another is saved in isolated storage for the application.
Its not possible for an application to mutate an existing picture in the saved pictures collection even if that application is the original creator of the picture. When saved, a new picture is created in the saved pictures collection.
On the other hand the existing content of the file in the isolated storage is replaced with the new content.
You can't.
It's only possible to read and add images in/to the MediaLibrary.
It is not possible to edit or delete images.
This is by design.

iTextSharp to generate PDF from WPF FixedDocument

I have a simple WPF app that displays and prints some
reports with a FixedDocument.
How can generate PDF's from that, with a free and open solution,
such as iTextSharp?
A WPF FixedDocument, also known as an XPS document, is a definite improvement over PDF. It has many capabilities that PDF lacks. In most cases it is better to distribute your document as XPS rather than PDF, but sometimes it is necessary to convert from XPS to PDF, for example if you need to open the document on devices that have only PDF support. Unfortunately most free tools to convert from XPS to PDF, such as CutePDF and BullzipPDF, require installing a printer driver or are not open source.
A good open-source solution is to use the "gxps" tool that is part of GhostPDL. GhostPDL is part of the Ghostscript project and is open-source licensed under GPL2.
Download GhostPDL from http://ghostscript.com/releases/ghostpdl-8.71.tar.bz2 and compile it.
Copy the gxps.exe executable into your project as Content and call it from your code using Process.Start.
Your code might look like this:
string pdfPath = ... // Path to place PDF file
string xpsPath = Path.GetTempPath();
using(XpsDocument doc = new XpsDocument(xpsPath, FileAccess.Write))
XpsDocument.CreateXpsDocumentWriter(doc).Write(... content ...);
Process.Start("gxps.exe",
"-sDEVICE=pdfwrite -sOutputFile=" +
pdfPath +
"-dNOPAUSE " +
xpsPath).WaitForExit();
// Now the PDF file is found at pdfPath
A simple way, which is easy, but probably not the most efficient way is to render the Fixed document to an image and then embed the image in a PDF using iTextSharp.
I have done it this way before successfully. Initially I tried to convert the control primitives (shapes) to PDF equivalents, but this proved too hard.
If you can get it into an image from WPF then you can import it into iTextSharp like they do in this article. You can even avoid the filesystem all together if you write it to a MemoryStream and then use that instead of using a FileStream.
http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
IF you want to do it programatically, your Best bet would be the following path XPS (Fixed Document) -> Print to PS -> Use Ghostscript to read the PS and convert to PDF.
If you dont care about reading the PDF back in the code, you can print to any one of the free PDF printers to which you can pass the destination path. This way your target PDF file will still be searchable if you have any test in your report.

Resources