Search code examples
c#.netpdfadobe-illustrator

Export Illustrator Vector Image From PDF Using C#


I have a pdf that contains a vector image. I asked the client about it, and they said that they created the image in Illustrator and saved it as a pdf. Is there a way I can extract that image and convert it into a png? I've tried code from the following:

Extract image from PDF using itextsharp

http://www.vbforums.com/showthread.php?530736-2005-Extract-Images-from-a-PDF-file-using-iTextSharp

and a couple of other links that I can't find, but they all don't seem to work. My theory is that they are extracting embedded images like jpegs, bmps, pngs, etc., but what I am faced with is a direct export from illustrator.

Should I be using an illustrator sdk or is there a way for me to do it using itextsharp? Also, I need to convert it to a standard image format, like png, and send the stream to a calling app, so I'll need to be able to grab stream.


Solution

  • You will not be able to do this with iText, since it cannot render or rasterize vector graphics in PDF files.

    Option 1:
    If a GPL license works for you, you could rasterize your PDF file with Imagemagick+GNU Ghostscript, but AFAIK you will have to write the output into a file in this case.

    Command line sample:

    convert -density 300 -depth 8 c:\temp\mydoc.pdf c:\temp\myrasterimage.png
    

    There is also a .net wrapper in Codeplex that might work for you: ImageMagick.NET

    Option A:
    If a commercial library is an option for you, you could try with Amyuni PDF Creator .Net. You can either use the method IacDocument.ExportToJpg, which requires writing into a file, or you can use the method IacDocument.DrawCurrentPage, which can be useful for writing the output into a memory stream.

    Sample code for exporting one page using IacDocument.DrawCurrentPage into a memory stream:

    const int twipsPerInch = 1440;
    const int MM_ISOTROPIC = 7;
    private static MemoryStream RasterizePDF(string filePath, int pageIndex, int targetDPI)
    {
        Amyuni.PDFCreator.IacDocument doc = new Amyuni.PDFCreator.IacDocument();
        doc.SetLicenseKey("Evaluation", "07EFC00...77C23E29");
        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);     
        doc.Open(fs, "");
        //Get the width and height of the target page
        Amyuni.PDFCreator.IacPageFormat format = doc.GetPage(pageIndex).GetPageFormat();
        doc.CurrentPageNumber = pageIndex;
    
        //Create Image
        Bitmap img = new Bitmap((int)(format.Width * targetDPI / twipsPerInch), (int)(format.Length * targetDPI / twipsPerInch), PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(img);
        //set image object background to white
        g.Clear(Color.White);
        //Get a device context for the grahics object
        IntPtr hdc = g.GetHdc();
        SetMapMode(hdc, MM_ISOTROPIC);
        // set scaling factor
        SetWindowExtEx(hdc, twipsPerInch, twipsPerInch, 0);
        SetViewportExtEx(hdc, targetDPI, targetDPI, 0);
        //draw the contents of the PDF document on to the graphic context
        doc.DrawCurrentPage(hdc, false);
        //clean up
        g.ReleaseHdc(hdc);
        g.Dispose();
        // Save the bitmap as png into the resulting stream
        MemoryStream resultStrm = new MemoryStream();
        img.Save(resultStrm, ImageFormat.Png);
        //Prepare the stream to be read later on
        resultStrm.Position = 0;
    }
    
    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern int SetMapMode(IntPtr hdc, int MapMode);
    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern int SetWindowExtEx(IntPtr hdc, int nXExtent, int nYExtent, int not_used);
    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern int SetViewportExtEx(IntPtr hdc, int nXExtent, int nYExtent, int not_used);
    

    Disclaimer: I currently work as a developer of the library