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.

Fri Mar 21, 2014 7:42 am

Hi,
I am quite new and am testing to extract a image from the pdfdocument in .net
But I am wondering how to specify the size of the destination image.
For example I want the image to be 800x600 pixels.

What I want to achieve is loading a pdf and save all pages as images to disk.

Code: Select all
Dim doc As New PdfDocument()
doc.LoadFromFile("C:\manual.pdf")

Dim Im As System.Drawing.Image = doc.SaveAsImage(i, 96, 96)

ndswbs@nds.eu
 
Posts: 8
Joined: Wed Mar 12, 2014 7:13 am

Fri Mar 21, 2014 8:23 am

Hello,

Thanks for your inquiry.
Please kindly refer to the following code.
Code: Select all
Dim document As New PdfDocument()
document.LoadFromFile("..\..\sample.pdf")

Dim image As Image = Nothing
Dim height As Integer = 800
Dim width As Integer = 600
For i As Integer = 0 To document.Pages.Count - 1
   image = document.SaveAsImage(i)
   Dim bitmap As New Bitmap(width, height)
   Dim g As Graphics = Graphics.FromImage(bitmap)
   g.DrawImage(image, New Rectangle(0, 0, width, height), New Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel)
   bitmap.Save([String].Format("D:\Images\image-{0}.png", i), ImageFormat.Png)
Next


Welcome to write to us again for further problems.

Best wishes,
Amy
User avatar

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

Mon Mar 24, 2014 7:05 am

Hi, thanks for your help

What is happening then in the next line?

image = document.SaveAsImage(i)

Because there is no destination width/height specified.
I want the output to be 1) consuming low cpu usage, 2) low memory usage, and 3) best quality of output in a manual output format.

In my situation the best would be to create output directly based on a manual width/height. This would skip another process step which will cost more cpu and memory.

ndswbs@nds.eu
 
Posts: 8
Joined: Wed Mar 12, 2014 7:13 am

Mon Mar 24, 2014 8:29 am

Hello,

Thanks for your feedback. Sorry for the inconvenience.
According to your requirement description, we have added the new feature likes SaveAsImage(i, imageHeight, imageWidth); into our schedule. We will inform you when it is supported in new version.

Please feel free to contact us if you have any problems.

Best wishes,
Amy
E-iceblue support team
User avatar

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

Mon Mar 24, 2014 8:50 am

I am trying now to get the spire.pdf.wpf working.

but this one does not have the saveasimage function?

ndswbs@nds.eu
 
Posts: 8
Joined: Wed Mar 12, 2014 7:13 am

Mon Mar 24, 2014 9:20 am

Hello,

Spire.pdf.wpf doesn't have SaveAsImage() function at present. Sorry for the inconvenience.
We have added it into our schedule. We will inform you when it is supported.

Best wishes,
Amy
E-iceblue support team
User avatar

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

Thu Mar 27, 2014 2:57 am

Hello,

Thanks for your waiting.
Sorry that the feature SaveAsImage(i, imageHeight, imageWidth); needs some time to be added. Now we provide you a solution to get a high quality of output and cost low CPU and memory.
Please try the method document.SaveAsImage(i,PdfImageType.Metafile) to save every page to a metafile. And please try to free unmanaged resource to reduce CPU and memory.

Share sample code:
Code: Select all
Dim document As New PdfDocument()
document.LoadFromFile("..\..\sample.pdf")

Dim image As Image = Nothing

Dim height As Integer = 800
Dim width As Integer = 600
For i As Integer = 0 To document.Pages.Count - 1
   Using image = document.SaveAsImage(i,PdfImageType.Metafile)
      Using bitmap As New Bitmap(width, height)
         Using g As Graphics = Graphics.FromImage(bitmap)
            g.DrawImage(image, New Rectangle(0, 0, width, height), New Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel)
            bitmap.Save([String].Format("D:\Images\image-{0}.png", i), ImageFormat.Png)
         End Using
      End Using
   End Using
Next

Spire.PDF for WPF has added SaveAsImage(); function.
We will inform your immediately as soon as the new version is released.

Welcome to write to us if you have any problems.

Best wishes,
Amy
E-iceblue support team
User avatar

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

Mon Mar 31, 2014 5:44 am

We will inform your immediately as soon as the new version is released.


Do you know when this will be released?
I am currently investigating the product to use and need to make my conclusion within 2 weeks.

ndswbs@nds.eu
 
Posts: 8
Joined: Wed Mar 12, 2014 7:13 am

Mon Mar 31, 2014 9:20 am

Hello,

Thanks for your inquiry.
Our test team found some issues when they tested the new version. Our dev team is fixing the issues.
Sorry that we can't give you an exact timeline now. But we will do our best to resolve them and release the new version soon.
We will inform you immediately as soon as the new version is released.

Best wishes,
Amy
E-iceblue support team
User avatar

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

Fri Apr 25, 2014 12:56 pm

I want to try to get the wpf variant working until the new feature is available.
So what I think I could do, is use the dpi setting for adjusting the size of the image created.

If I save the image as 96 dpi, I get a certain width/height
Where is this size related too? I looked at the pagesettings.size, but I saw it is stored in points.
Can someone help me to get this solved.


Code: Select all
    Dim doc As New PdfDocument()
    doc.LoadFromFile("C:\pdf.pdf ")

    Dim Sze = doc.PageSettings.Size
    For i As Integer = 1 To doc.Pages.Count

      Dim img = doc.SaveAsImage(i, CDbl(96), CDbl(96))
      G.Source = img
      Exit Sub
    Next


ndswbs@nds.eu
 
Posts: 8
Joined: Wed Mar 12, 2014 7:13 am

Tue Apr 29, 2014 3:46 am

Hello,

Sorry for the delay response for the weekends here.
The dpi is not related to the size of the image created.
There is new version of Spire.PDF has been released, Spire.PDF Pack Version:3.0(http://www.e-iceblue.com/Download/downl ... t-now.html). Welcome to test SaveAsImage(int pageIndex, double dpiX = 96, double dpiY = 96); function for WPF.
The function only is to save pdf as vector images with defined dpi. You need to code setting the size of the created images.
Code: Select all
  BitmapSource source = document.SaveAsImage(i, 300.0, 300.0);


Best wishes,
Amy
User avatar

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

Mon May 05, 2014 9:18 am

Hello,

Have you tested the new feature in Spire.PDF Pack Version:3.0? How did it work?
Thanks for your feedback.

Best wishes,
Amy
User avatar

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

Mon May 05, 2014 11:19 am

Hi Amy,

I have been testing last week.
When I create a image, lets say
Code: Select all
Dim img = doc.SaveAsImage(i, CDbl(96), CDbl(96))


and afterwards I check the pixelwidth and pixelheight of the image (img) they have a certain value.
My question is how is the size in pixels determined? I did not/am not able to specify a destination size.
The destination image (img) itself is pixel oriented, not vector.

That's why the best solution is to specify something like: doc.SaveAsImage(i, 1024, 768)
where the values are the image destination pixel width/height.

ndswbs@nds.eu
 
Posts: 8
Joined: Wed Mar 12, 2014 7:13 am

Tue May 06, 2014 8:09 am

Hello,

Thanks for your feedback.
The width of image is page.Bound.Width*dpi/96.0. The height of image is page.Bound.Height*dpi/96.0.
There is a method document.SaveAsDrawingImage(int pageIndex); can save as the vector image, but it has a bug now.
Our dev team is fixing it. Once it is resolved, we will inform you.

Best wishes,
Amy
User avatar

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

Fri May 16, 2014 2:27 am

Hello,

Thanks for waiting.
The bug has been fixed. Welcome to download and test Spire.PDF Pack(Hot Fix) Version:3.0.27(http://www.e-iceblue.com/Download/downl ... t-now.html).
Sample code:
Code: Select all
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(inputFile);
 for (int j = 1; j <= pdf.Pages.Count; j++)
            {
                DrawingImage drawingImage = pdf.SaveAsDrawingImage(j);
                var drawingVisual = new DrawingVisual();
                using (var drawingContext = drawingVisual.RenderOpen())
                {
                    int width = (int)drawingImage.Drawing.Bounds.Width;
                    int Height = (int)drawingImage.Drawing.Bounds.Height;
                    var bitmap = new RenderTargetBitmap(width, Height, 96, 96, PixelFormats.Pbgra32);
                    bitmap.Render(drawingVisual);
                    var encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmap));
                    string st = outputFile + "_" + j.ToString() + ".png";
                    using (var stream = new System.IO.FileStream(st, FileMode.Create))
                    { encoder.Save(stream); }

                }
                pdf.Close();
            }


Best wishes,
Amy
User avatar

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

Return to Spire.PDF

cron