Convert a single page of multipaged document to PNG in .NET

Hi,

I just found out that saving a single page of a multi-page document as png fails since the update to Conversion .Net 20.1 with the following error:

Saving complete multi page document to image is not supported. Please save by page.

This code for thumbnail generation was working previously:

    using (var conv = new Converter(sInputFile))
    {
        var options = new ImageConvertOptions()
        {
            Format = ImageFileType.Png,
            //convert only the first page for thumbnail
            PageNumber = 1,
            PagesCount = 1,
            //low dpi for thumbnail
            VerticalResolution = 30,
            HorizontalResolution = 30
        };
        conv.Convert(sOutputFile, options);
    }

Are we forced to use “SavePageStream” now, or how does processing single pages now work?

I saw that there was a bug listed with the 20.1 release:
CONVERSIONNET-3625 Bug PageNumber and PagesCount not respected when converting to image.
Could this be the same issue?

If it is, I apologize for posting a duplicate and want to add that not only PageNumber and PagesCount are ignored, but also the Vertical- and HorizontalResolution property (when using SavePageStream instead).

1 Like

@Clemens_Pestuka,

Using code below, we’re not able to reproduce this issue at our end (you have to use SavePageStream):

 string outputFileTemplate = Path.Combine(@"D:/Samples/", "converted-page-{0}.png");
 GroupDocs.Conversion.Contracts.SavePageStream getPageStream = page => new FileStream(string.Format(outputFileTemplate, page), FileMode.Create);
 using (Converter converter = new Converter(@"D:/Samples/target.pdf"))
 {
        var options = new ImageConvertOptions()
         {
              Format = ImageFileType.Png,
               //convert only the first page for thumbnail
               PageNumber = 1,
               PagesCount = 1,
              //low dpi for thumbnail
              VerticalResolution = 30,
              HorizontalResolution = 30
          };
          converter.Convert(getPageStream, options);
  }

Have a look at output image.png (13.8 KB). Vertical and horizontal resolutions are successfully applied. Have a look at this article.

.

@atirtahir3
Thank you for the reply. I agree that the code you sent works fine, I tried that too.

What worries me is:

(you have to use SavePageStream )

This was not needed in 19.12.1 and the code I sent was working in that version, but with 20.1 it just broke :frowning: .
Do I really need to use SavePageStream now, although I will only have 1 single page (due to PageNumber and PagesCount)?
I did not see this as a breaking change, are such things usually reported?

@Clemens_Pestuka,

We are investigating this. Your investigation ticket ID is CONVERSIONNET-3726. As there is any further update, you’ll be notified.

1 Like

@atirtahir3

When testing another issue I also realized that this issue only seems to happen with a certain docx file.
Portrait_Landscape.zip (14.1 KB)

For this document, a second empty page is created, although pageCount is 1.
Conversion output.zip (5.9 KB)

So it looks like nothing changed and we don’t need to use SavePageStream now.
Just for this one document I got the error

Saving complete multi page document to image is not supported. Please save by page.

because there was actually more than one page to save.
So the actual bug that needs to be fixed, is the second empty page that should not be created.

1 Like

@Clemens_Pestuka,

We’ll further investigate this issue and let you know as there’s any update.

1 Like

@Clemens_Pestuka - just curious - did you find a workaround for the initial issue you reported? I am encountering a similar issue, and am also not using the SavePageStream as was suggested in the recommended solution.

If you have a workaround, would you be able to share what you did?

Thanks in advance,
-Brian

1 Like

@bpieslak

Hi Brian, I did not really find or look for a workaround but just using SavePageStream and discarding the second file should be one.
It turned out that an empty page was generated at conversion (maybe some new-lines shifted) and that was the reason why saving to a single file did not work (there were two pages now). So the issue only occurred with a certain file.

If the same thing happens for you too, maybe sharing your document could help them fix the issue :slight_smile:

1 Like

@bpieslak,

Let us know if @Clemens_Pestuka suggestion works for you. Otherwise, you can share problematic document with us. Also specify following details:

  • API version that you are using
  • Sample code to reproduce the issue

Was this ever resolved? I’m getting same issue when trying pull first page from Excel to JPG. If I change output type to TIFF, I get 4 pages when I’m asking only for 1. This only happens with some Excels and not all.

ImageConvertOptions optionsImage = new ImageConvertOptions
{
Format = ImageFileType.Tif,
PageNumber = 1,
PagesCount = 1,
Width = 800
};

That code gets me 4 pages in tiff in some Excels. Clearly a bug.

Groupdocs.Conversion 23.3.1

I cannot share Excel as it is confidential.

1 Like

@NPozdniakov

Could you please make a sample Excel file using that issue could be reproduced?

Found one from public Enron case. Here is example of Excel and output it creates when asked for 1 page.

https://bit.ly/3LnNU0u

I only need page 1 in JPEG

@NPozdniakov

Thanks for the details. We are further investigating this issue. Your investigation ticket ID is CONVERSIONNET-6035.

@Clemens_Pestuka @NPozdniakov
The key point is to provide SpreadsheetLoadOptions with property OnePagePerSheet = true
Here is a sample code snippet, which provides one image for the complete sheet.

using (Converter converter = new GroupDocs.Conversion.Converter("ALEWIS_0000000021.0001.XLS", type => new SpreadsheetLoadOptions
       {
           OnePagePerSheet = true
       }))
{
    ImageConvertOptions options = new ImageConvertOptions { Format = GroupDocs.Conversion.FileTypes.ImageFileType.Jpg };
    options.PageNumber = 1;
    options.PagesCount = 1;
    converter.Convert((i,type) => new FileStream($"result-page{i}.{type.Extension}", FileMode.Create, FileAccess.ReadWrite), options);
}
1 Like

Thank you. I will try that.

@NPozdniakov

You are welcome.

hmm… that doesn’t really work for me. Text on page is too small for most since we do OnePagePerSheet. I just want page 1 without any scaling. It should work exactly like for Doc file.

Do you have any other suggestions?

Thanks

1 Like

@NPozdniakov

We will conduct a thorough investigation once again and provide you with an update shortly.

@NPozdniakov

If OnePagePerSheet is applied - then the complete sheet is exported as one image. There is no scale, just the image, big in width and height and may need zoom to 100% to be viewed properly.
Otherwise, if OnePagePerSheet is set to no, the worksheet is exported as 18 images. It is the same as it is exported from MS Excel.
Could you please export your sample with Excel and show us the result?

Should the PageCount/PageNumber option restrict to just 1 page (so you don’t get 18)? That is how it works for Word type documents.

1 Like