Exporting a Vector Image Illustrator from a PDF Using C #

I have a pdf that contains a vector image. I asked the client about this, and they said that they created the image in Illustrator and saved it as a pdf. Is there any way to extract this image and convert it to png? I tried the 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 cannot find, but all of them do not work. My theory is that they extract embedded images such as jpegs, bmps, pngs, etc., but what I encounter is direct export from illustrator.

Should I use sdk illustrator or is there a way to do this with itextsharp? Also, I need to convert it to a standard image format like png, and send the stream to the calling application, so I will need to capture the stream.

+4
source share
2 answers

You cannot do this with iText, since it cannot display or rasterize vector graphics in PDF files.

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

Command line example:

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

Codeplex also has a .net shell that might work for you: ImageMagick.NET

Option A:
If you have a commercial library for you, you can try Amyuni PDF Creator.Net . You can use the IacDocument.ExportToJpg method, which requires writing to a file, or you can use the IacDocument.DrawCurrentPage method, which can be useful for writing output to a memory stream.

Sample code to export a single page using IacDocument.DrawCurrentPage to 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 am currently working as a library developer

0
source

Modern versions of AI use PDF as an export format. This is an advanced PDF form containing important metadata for Illustrator, but it is ultimately a PDF.

Yes, most PDF packages are aimed at extracting bitmaps as they enter atomic lumps. If your embedded image is a vector, it has been discarded in a format that most will not understand.

Illustrator may have used his own metadata to delimit the image. If so, then it will be difficult to extract. However, he may have used an analogue of PDF, such as the XObject form. If I were designing Illustrator, I would probably do both.

So it’s possible to extract at least a little more complicated. It is no longer possible to say without being able to see the document.

If you want to send us your illustrator file in ABCpdf, we will definitely see what we can offer. :-)

0
source

Source: https://habr.com/ru/post/1481443/


All Articles