PDFsharp提取复选框图像问题

时间:2019-02-19 19:26:23

标签: image checkbox extract pdfsharp

我需要从PDF文件中提取复选框。没有杂技场或类似的东西。需要使用PDFsharp库或其他一些免费使用的库。我不能使用iTextSharp,因为它不是免费用于商业用途。
我找到了一些用于从页面提取图像的代码,但是它带给我4个对象,其中两个是图像。我假设它们可能是页面上的复选框的勾号和未勾号的复选框,但是我需要获取页面中图像的所有引用,因为每个页面上都有大约15个复选框,并且我需要知道单击了哪个复选框和哪个复选框不是。唯一的方法是从页面中提取所有图像,并检查是否单击。我当前使用的代码是:

using (PdfDocument doc = PdfReader.Open(path))
    {
        foreach (PdfPage page in doc.Pages)
        {
            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;
                            // Is external object an image?
                            if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image")
                            {
                                // do something with your image here 
                                // only the first image is handled here
                                var bitmap = ExportImage(xObject);
                                bitmap.Save($@"C:\Users\tishk\Desktop\exported{imageCount++}.png", ImageFormat.Bmp);
                            }
                        }
                    }
                }
            }
        }
    }
}

private static Bitmap ExportImage(PdfDictionary image)
{
    string filter = image.Elements.GetName("/Filter");
    switch (filter)
    {
        case "/FlateDecode":
            return ExportAsPngImage(image);

        default:
            throw new ApplicationException(filter + " filter not implemented");
    }
}

private static Bitmap ExportAsPngImage(PdfDictionary image)
{
    int width = image.Elements.GetInteger(PdfImage.Keys.Width);
    int height = image.Elements.GetInteger(PdfImage.Keys.Height);
    int bitsPerComponent = image.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);

    var canUnfilter = image.Stream.TryUnfilter();
    var decoded = image.Stream.Value;

    Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
    Marshal.Copy(decoded, 0, bmpData.Scan0, decoded.Length);
    bmp.UnlockBits(bmpData);

    return bmp;
}

0 个答案:

没有答案