Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用PDFSharp和/JBIG2过滤器从pdf中提取图像_C#_Image_Pdf_Pdfsharp_Jbig2 - Fatal编程技术网

C# 使用PDFSharp和/JBIG2过滤器从pdf中提取图像

C# 使用PDFSharp和/JBIG2过滤器从pdf中提取图像,c#,image,pdf,pdfsharp,jbig2,C#,Image,Pdf,Pdfsharp,Jbig2,我正在尝试使用PDFsharp从PDF文件中提取图像。我运行代码的测试文件显示过滤器类型为/JBIG2。如果可以使用PDFSharp,我想帮助您理解如何解码此图像并保存它 我用来提取图像然后保存的代码如下: const string filename = "../../../test.pdf"; PdfDocument document = PdfReader.Open(filename); int imageCount = 0; foreach (PdfPage pa

我正在尝试使用PDFsharp从PDF文件中提取图像。我运行代码的测试文件显示过滤器类型为/JBIG2。如果可以使用PDFSharp,我想帮助您理解如何解码此图像并保存它

我用来提取图像然后保存的代码如下:

const string filename = "../../../test.pdf";            
PdfDocument document = PdfReader.Open(filename);
int imageCount = 0;

foreach (PdfPage page in document.Pages) { // Iterate pages
  // Get resources dictionary
  PdfDictionary resources = page.Elements.GetDictionary("/Resources");

  if (resources != null) {
    // Get external objects dictionary
    PdfDictionary xObjects = resources.Elements.GetDictionary("/XObject");

    if (xObjects != null) {
      ICollection<PdfItem> items = xObjects.Elements.Values;

      foreach (PdfItem item in items) { // Iterate references to external objects
        PdfReference reference = item as PdfReference;

        if (reference != null) {
          PdfDictionary xObject = reference.Value as PdfDictionary;

          // Is external object an image?
          if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image") {
            ExportImage(xObject, ref imageCount);
          }
        }
      }
    }
  }
}

static void ExportImage(PdfDictionary image, ref int count) {
   string filter = image.Elements.GetName("/Filter");

   switch (filter) {
     case "/DCTDecode":
       ExportJpegImage(image, ref count);
       break;
     case "/FlateDecode":
       ExportAsPngImage(image, ref count);
       break;
   }  
}

static void ExportJpegImage(PdfDictionary image, ref int count) {
  // Fortunately, JPEG has native support in PDF and exporting an image is just writing the stream to a file.
  byte[] stream = image.Stream.Value;
  FileStream fs = new FileStream(
    String.Format("Image{0}.jpeg", count++), FileMode.Create, FileAccess.Write
  );
  BinaryWriter bw = new BinaryWriter(fs);
  bw.Write(stream);
  bw.Close();
}
const string filename=“../../../test.pdf”;
PdfDocument document=PdfReader.Open(文件名);
int imageCount=0;
foreach(document.Pages中的PdfPage页面){//迭代页面
//获取资源字典
PdfDictionary resources=page.Elements.GetDictionary(“/resources”);
if(资源!=null){
//获取外部对象字典
PdfDictionary xObjects=resources.Elements.GetDictionary(“/XObject”);
if(xObject!=null){
ICollection items=xObjects.Elements.Values;
foreach(PdfItem item in items){//迭代对外部对象的引用
PdfReference reference=作为PdfReference的项目;
如果(引用!=null){
PdfDictionary xObject=引用。值为PdfDictionary;
//外部物体是图像吗?
if(xObject!=null&&xObject.Elements.GetString(“/Subtype”)==“/Image”){
ExportImage(xObject,ref imageCount);
}
}
}
}
}
}
静态无效导出映像(PdfDictionary映像,ref int计数){
字符串过滤器=image.Elements.GetName(“/filter”);
开关(过滤器){
大小写“/dct解码”:
exportJPEG图像(图像,参考计数);
打破
案例“/格式代码”:
ExportAsPngImage(图像,参考计数);
打破
}  
}
静态无效输出JPEG图像(PdfDictionary图像,参考整数计数){
//幸运的是,JPEG在PDF中具有本机支持,导出图像只是将流写入文件。
字节[]流=image.stream.Value;
FileStream fs=newfilestream(
String.Format(“Image{0}.jpeg”,count++),FileMode.Create,FileAccess.Write
);
BinaryWriter bw=新的BinaryWriter(fs);
写入(流);
bw.Close();
}

在上面,我得到的过滤器类型是
/JBIG2
,我确实支持它。上面的代码来自

JBIG2在PDF中使用最广泛,但PDF之外的情况则不同。尽管.jbig2是一种光栅图像格式,但就图像查看器而言,对它的支持非常少。您最好像Acrobat一样将其导出为CCITT4压缩TIFF。

请发布您用于提取过程的代码,如果可能,请发布相关PDF(或其链接)。使用代码编辑主要摘要。这将是很难共享的文件,但我可以补充说,该文件是一个pdf生成时,我扫描了一份文件,并通过电子邮件发送给自己@要回答你的问题,我必须阅读Adobe的PDF参考手册,但我现在没有时间。如果你查阅参考资料,也许你可以自己回答你的问题。