Daniel-Glucksman / JBig2Decoder.NET

A .Net Implementation of the JPedal JBig2 Decoder
MIT License
10 stars 4 forks source link

Better Readme and Usages and `Could not load file or assembly 'PresentationCore...` error #10

Open sahin52 opened 2 years ago

sahin52 commented 2 years ago

I am having trouble to get images with .Net5.0. Here is the code I am using:

using PdfSharp.Pdf;
using PdfSharp.Pdf.Advanced;
using System.IO;
using PdfSharp.Pdf.IO;
using System.Drawing;
using JBig2Decoder;
using System.Collections.Generic;
//....

public class Service{
      public void OcrPdfs()
        {
            const string filename = "20211214.pdf";

            PdfDocument document = PdfReader.Open(filename);

            int imageCount = 0;
            int i = 0;
            // Iterate pages
            foreach (PdfPage page in document.Pages)
            {
                i++;
                // 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;
                        // Iterate references to external objects
                        foreach (PdfItem item in items)
                        {
                            PdfReference reference = item as PdfReference;
                            if (reference != null)
                            {
                                PdfDictionary xObject = reference.Value as PdfDictionary;
                                var cals = xObject.Elements.Values;
                                // 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;
                case "/JBIG2Decode":
                    JDec(image, ref count);
                        break;
                case "/JPXDecode":
                    break;
                default:
                    break;
            }
        }
      private static void JDec(PdfDictionary image, ref int count)
        {
            var decoder = new JBIG2StreamDecoder();
            var stream = decoder.decodeJBIG2(image.Stream.Value); // error line
            var ms = new MemoryStream(stream);
            var img = Bitmap.FromStream(ms);

            FileStream fs = new FileStream(String.Format("Image{0}.jpeg", count++), FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(stream);
            bw.Close();
        }
}

I am getting error on the specified line (error line): Could not load file or assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

The pdf file is: https://www.resmigazete.gov.tr/eskiler/2021/12/20211214.pdf . What is the reason?
And could you please add some more usages.