Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Jan 14, 2021 3:10 am

Hi!
This is my actual code to convert a PDF to an image.
Code: Select all
PdfDocument doc = new PdfDocument();
               doc.LoadFromBytes(plottingMapLevel.ImgData);
               doc.CompressionLevel = PdfCompressionLevel.None;
               Stream image = doc.SaveAsImage(0);
               MemoryStream ms = (MemoryStream)image;
               imgData = ms.ToArray();


Inside of my PDF there are big images but somehow when converting to an image the Height and width are smaller on the pdf converted image. How can I delete this compression?

r_lugo.cr
 
Posts: 3
Joined: Thu Jan 14, 2021 3:04 am

Thu Jan 14, 2021 7:50 am

Hello,

Thanks for your inquiry.
When using our Spire.PDF to convert PDF to images, the size of converted image is based on the PDF page size. And I debugged and found that the page size of your file is about 1123x685 (as shown below), so the size of the converted picture is the same. Hope you can understand.
page-size.png

If you have further questions, please feel free to contact us.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Jan 14, 2021 1:10 pm

Ok, got it. Thanks!

Is there a way to detect if the page have an image inside and extract it with their original dimensions and not the page dimensions?

r_lugo.cr
 
Posts: 3
Joined: Thu Jan 14, 2021 3:04 am

Fri Jan 15, 2021 1:57 am

Hi,

Please refer to the following code to extract the original image from the PDF.

Code: Select all
int count = 0;
    foreach (PdfPageBase page in doc.Pages)
    {
        foreach (Stream stream in page.ExtractImages())
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            FileStream fs = new FileStream("result" + count++ + ".png", FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }
    }


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2767
Joined: Wed Jun 27, 2012 8:50 am

Wed Jan 20, 2021 8:23 am

Hello,

Greetings from E-iceblue!
Did the code we provided work for you? Any feedback will be greatly appreciated!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF