sungaila / PDFtoImage

A .NET library to render PDF files into images.
https://www.sungaila.de/PDFtoImage/
MIT License
144 stars 14 forks source link

Convert PDF to PNG problem #88

Open COMADD opened 1 week ago

COMADD commented 1 week ago

PDFtoImage version

4.0.2

OS

Windows

OS version

Windows 11 23H2

Architecture

x64

Framework

.NET (Core)

App framework

WinUI3 .Net7.0

Detailed bug report

Hello,

I'm trying to use the PDFtoImage package to convert a single page PDF file to a PNG image via the Conversion.SavePng() function.

The conversion works since a PNG file is created, but this image is truncated, at the bottom, on N pixels which are transparent!

I should point out that the PDF contains a square page and not a rectangular A4 type page.

The generated PNG image is square too, but a part, at the bottom, is missing.

I tried using different RenderOptions options but couldn't generate a correct PNG image.

Example PDF and PNG files for this problem are available via the links: https://1drv.ms/i/s!AqhmCsNj4U4CyjugqM2Iv5hn297s?e=KKiqNd https://1drv.ms/b/s!AqhmCsNj4U4CyjpmZPIBqUG1UR4t?e=sea6MF

Best regards

sungaila commented 1 week ago

Hi @COMADD, thanks for opening this issue!

I'm not sure what you mean by "but this image is truncated, at the bottom, on N pixels which are transparent". Looking at your sample PNG file it looks corrupted at the bottom of the picture.

Does this happen when you use the WebConverter (https://www.sungaila.de/PDFtoImage/)? Can you give me a code snippet of your SavePng call?

COMADD commented 1 week ago

Hello,

I don’t know what is WebConverter !

My C# code is :

public static async Task ConvertPdfToPngAsync(StorageFile pdf, StorageFile png) { try { IRandomAccessStream randomAccessStream = await pdf.OpenAsync(FileAccessMode.Read); Stream pdf_stream = randomAccessStream.AsStreamForRead();

    using (IRandomAccessStream png_stream = await png.OpenAsync(FileAccessMode.ReadWrite))
    {
        RenderOptions options = new RenderOptions(83,
            600,
            600,
            false,
            false,
            true,
            PdfRotation.Rotate0,
            PdfAntiAliasing.All,
            null,
            null,
            false,
            false);
        Conversion.SavePng(png_stream.AsStreamForWrite(),
                                     pdf_stream,
                                     false,
                                     null,
                                     0,
                                     options);
    }
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
    throw;
}

}

I test different versions of RenderOption, changing DPI, width, height, and WithAspectRatio but cannot have a correct PNG image.

De : David Sungaila @.> Envoyé : mercredi 3 juillet 2024 21:20 À : sungaila/PDFtoImage @.> Cc : COMADD @.>; Mention @.> Objet : Re: [sungaila/PDFtoImage] Convert PDF to PNG problem (Issue #88)

Hi @COMADDhttps://github.com/COMADD, thanks for opening this issue!

I'm not sure what you mean by "but this image is truncated, at the bottom, on N pixels which are transparent". Looking at your sample PNG file it looks corrupted at the bottom of the picture.

Does this happen when you use the WebConverter (https://www.sungaila.de/PDFtoImage/)? Can you give me a code snippet of your SavePng call?

— Reply to this email directly, view it on GitHubhttps://github.com/sungaila/PDFtoImage/issues/88#issuecomment-2207024389, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BIVU7VFTH623UMY532QTV2TZKRFEXAVCNFSM6AAAAABKKE22FOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBXGAZDIMZYHE. You are receiving this because you were mentioned.Message ID: @.***>

sungaila commented 1 week ago

I've tested the conversion with your given options and get this result:

image https://github.com/sungaila/PDFtoImage/assets/12021771/ea8ccbbf-3e81-4a7d-87e0-346e0bace51e

Maybe the png_stream isn't closed properly. Try this:

Conversion.SavePng(png_stream.AsStreamForWrite(),
                                         pdf_stream,
                                         true,
                                         null,
                                         0,
                                         options);
COMADD commented 6 days ago

I test Conversion.SavePng() with leaveOpen=true the generated PNG is better, but there remains a narrow margin at the bottom!

The result link : https://1drv.ms/i/s!AqhmCsNj4U4CyjzZGISa_GotD_TM?e=mAldWy

I verify the code and ConvertPdfToPngAsync() is correctly awaited.

The reworked code is : public static async Task ConvertPdfToPngAsync(StorageFile pdf, StorageFile png) { try { using (IRandomAccessStream randomAccessStream = await pdf.OpenAsync(FileAccessMode.Read)) { Stream pdf_stream = randomAccessStream.AsStreamForRead();

         using (IRandomAccessStream png_stream = await png.OpenAsync(FileAccessMode.ReadWrite))
         {
             RenderOptions options = new RenderOptions(
                 83,
                 600,
                 600,
                 false,
                 false,
                 true,
                 PdfRotation.Rotate0,
                 PdfAntiAliasing.All,
                 null,
                 null,
                 false,
                 false);
             Conversion.SavePng(
                 png_stream.AsStreamForWrite(),
                 pdf_stream,
                 true,
                 null,
                 0,
                 options);
         }
     }
 }
 catch (Exception ex)
 {
     Debug.WriteLine(ex.Message);
     throw;
 }

}

De : David Sungaila @.> Envoyé : jeudi 4 juillet 2024 12:44 À : sungaila/PDFtoImage @.> Cc : COMADD @.>; Mention @.> Objet : Re: [sungaila/PDFtoImage] Convert PDF to PNG problem (Issue #88)

I've tested the conversion with your given options and get this result: https://github.com/sungaila/PDFtoImage/assets/12021771/ea8ccbbf-3e81-4a7d-87e0-346e0bace51e https://github.com/sungaila/PDFtoImage/assets/12021771/ea8ccbbf-3e81-4a7d-87e0-346e0bace51e Maybe the png_stream isn't closed properly. Try this: Conversion.SavePng(png_stream.AsStreamForWrite(), pdf_stream, true, null, 0, options); — Reply to this email directly, https://github.com/sungaila/PDFtoImage/issues/88#issuecomment-2208665852, or https://github.com/notifications/unsubscribe-auth/BIVU7VBFYQPBY5QTYAZRMJ3ZKURNFAVCNFSM6AAAAABKKE22FOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBYGY3DKOBVGI. You are receiving this because you were mentioned.Message ID: @.***>