相关疑难解决方法(0)

使用iTextSharp提取图像

我一直在使用此代码取得巨大成功,以提取PDF的每个页面中找到的第一个图像.但是,由于未知原因,它现在无法使用某些新PDF.我已经使用了其他工具(Datalogics等),这些工具可以很好地利用这些新的PDF来提取图像.但是,如果我可以使用iTextSharp,我不想购买Datalogics或任何工具.任何人都可以告诉我为什么这段代码没有找到PDF中的图像?

Knowns:我的PDF每页只有1张图片,没有别的.

using iTextSharp.text;
using iTextSharp.text.pdf;
...
public static void ExtractImagesFromPDF(string sourcePdf, string outputPath)
{
    // NOTE:  This will only get the first image it finds per page.
    PdfReader pdf = new PdfReader(sourcePdf);
    RandomAccessFileOrArray raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);

    try
    {
        for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
        {
            PdfDictionary pg = pdf.GetPageN(pageNumber);
            PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));

            PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
            if (xobj != null)
            {
                foreach (PdfName name in xobj.Keys)
                {
                    PdfObject obj = xobj.Get(name);
                    if (obj.IsIndirect())
                    { …
Run Code Online (Sandbox Code Playgroud)

itextsharp

21
推荐指数
4
解决办法
7万
查看次数

标签 统计

itextsharp ×1