使用C#从PDF导出Illustrator矢量图像

时间:2013-05-17 18:07:54

标签: c# .net pdf adobe-illustrator

我有一个包含矢量图像的pdf。我向客户询问了这个问题,他们说他们在Illustrator中创建了图像并将其保存为pdf格式。有没有办法可以提取该图像并将其转换为png?我尝试过以下代码:

Extract image from PDF using itextsharp

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

和其他几个我找不到的链接,但它们似乎都不起作用。我的理论是他们正在提取嵌入式图像,如jpegs,bmps,pngs等,但我所面对的是插画师的直接导出。

我应该使用插画家sdk还是有办法让我用itextsharp来做?此外,我需要将其转换为标准图像格式,如png,并将流发送到调用应用程序,所以我需要能够抓取流。

2 个答案:

答案 0 :(得分:0)

您将无法使用iText执行此操作,因为它无法渲染或栅格化PDF文件中的矢量图形。

选项1:
如果GPL许可证适合您,您可以使用Imagemagick + GNU Ghostscript光栅化您的PDF文件,但是在这种情况下您必须将输出写入文件。

命令行示例:

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

Codeplex中还有一个可能适合您的.net包装:ImageMagick.NET

选项A:
如果商业图书馆是您的选择,您可以尝试使用Amyuni PDF Creator .Net。您可以使用需要写入文件的方法IacDocument.ExportToJpg,也可以使用方法IacDocument.DrawCurrentPage,这对于将输出写入内存流非常有用。

使用IacDocument.DrawCurrentPage将一个页面导出到内存流中的示例代码:

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

免责声明:我目前是图书馆的开发人员

答案 1 :(得分:0)

AI的现代版本使用PDF作为导出格式。它是一种增强形式的PDF,包含Illustrator的重要元数据,但最终它是PDF。

是的,大多数PDF软件包旨在提取位图,因为它们存在于原子块中。如果你的嵌入图像是矢量,那么它就会以大多数人无法理解的格式被删除。

Illustrator可能已使用自己的元数据来分隔图像。如果是这种情况,则很难提取。但是它可能使用了像Form XObject这样的PDF模拟。如果我正在设计Illustrator,我可能会同时做这两件事。

所以它可能提取虽然可能有点棘手。如果不能看到该文件,就更难说了。

如果您想将您的插图文件邮寄给ABCpdf,我们一定会看到我们的建议。 : - )

相关问题