Giter Site home page Giter Site logo

Comments (6)

mKenfenheuer avatar mKenfenheuer commented on August 26, 2024 13

Thanks for your help, this pointed me into the right direction.

Your code worked well, but not a 100% fit for my case (i had also transparent font smoothing).

I Just had to create a white image and "print" the document onto it.

the code below now works for me:

public static byte[] ConvertPdfToJpeg(this byte[] data, int rwidth = 1080, int rheight = 1920)
        {
            using (var library = DocLib.Instance)
            {
                using (var docReader = library.GetDocReader(data, rwidth, rheight))
                {
                    using (var pageReader = docReader.GetPageReader(0))
                    {
                        var rawBytes = pageReader.GetImage();
                        var width = pageReader.GetPageWidth();
                        var height = pageReader.GetPageHeight();
                        using (var doc = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                        using (var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                        {
                            bmp.AddBytes(rawBytes);
                            for (int y = 0; y < bmp.Height; y++)
                            {
                                bmp.SetPixel(bmp.Width - 1, y, bmp.GetPixel(bmp.Width - 2, y));
                            }
                            Graphics g = Graphics.FromImage(doc);
                            g.FillRegion(Brushes.White, new Region(new Rectangle(0, 0, width, height)));
                            g.DrawImage(bmp, new Point(0, 0));
                            g.Save();
                            using (var stream = new MemoryStream())
                            {
                                doc.Save(stream, ImageFormat.Jpeg);
                                return stream.ToArray();
                            }
                        }
                    }
                }
            }
        }

from docnet.

jaredbaszler avatar jaredbaszler commented on August 26, 2024 1

@mKenfenheuer

Thanks for your help, this pointed me into the right direction.

Your code worked well, but not a 100% fit for my case (i had also transparent font smoothing).

I Just had to create a white image and "print" the document onto it.

the code below now works for me:

public static byte[] ConvertPdfToJpeg(this byte[] data, int rwidth = 1080, int rheight = 1920)
        {
            using (var library = DocLib.Instance)
            {
                using (var docReader = library.GetDocReader(data, rwidth, rheight))
                {
                    using (var pageReader = docReader.GetPageReader(0))
                    {
                        var rawBytes = pageReader.GetImage();
                        var width = pageReader.GetPageWidth();
                        var height = pageReader.GetPageHeight();
                        using (var doc = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                        using (var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                        {
                            bmp.AddBytes(rawBytes);
                            for (int y = 0; y < bmp.Height; y++)
                            {
                                bmp.SetPixel(bmp.Width - 1, y, bmp.GetPixel(bmp.Width - 2, y));
                            }
                            Graphics g = Graphics.FromImage(doc);
                            g.FillRegion(Brushes.White, new Region(new Rectangle(0, 0, width, height)));
                            g.DrawImage(bmp, new Point(0, 0));
                            g.Save();
                            using (var stream = new MemoryStream())
                            {
                                doc.Save(stream, ImageFormat.Jpeg);
                                return stream.ToArray();
                            }
                        }
                    }
                }
            }
        }

@mKenfenheuer - thanks a bunch. I had exactly the same problem as you had experienced. You saved me a BUNCH of time. Thanks for the code example!!!

from docnet.

inlineHamed avatar inlineHamed commented on August 26, 2024 1

I think the library should treat like this as default.

from docnet.

Modest-as avatar Modest-as commented on August 26, 2024

This is not an issue, the library will generate whatever is presented in the PDF file, if file has no background specified it will treat that as transparent, you can always specify background layer to whichever color you want depending on your image processing library, just replace empty pixels with whatever Format32bppArgb value you want

from docnet.

mKenfenheuer avatar mKenfenheuer commented on August 26, 2024

@Modest-as

do you have an example on how to do this?

from docnet.

Modest-as avatar Modest-as commented on August 26, 2024

Sure, something like this:

var rawBytes = pageReader.GetImage();

for (var i = 0; i < rawBytes.Length / 4; i++)
{
    var j = i * 4;
    var alpha = rawBytes[j];
    var red = rawBytes[j + 1];
    var green = rawBytes[j + 2];
    var blue = rawBytes[j + 3];

    if (alpha != 0 || red != 0 || green != 0 || blue != 0) continue;

    rawBytes[j] = byte.MaxValue;
    rawBytes[j + 1] = byte.MaxValue;
    rawBytes[j + 2] = byte.MaxValue;
    rawBytes[j + 3] = byte.MaxValue;
}

from docnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.