Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sahin52 opened this issue Dec 16, 2021 · 0 comments

Comments

@sahin52
Copy link

sahin52 commented Dec 16, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant