none
Ghostscript.NET.Rasterizer set resolution (Dpi) is not working RRS feed

  • Question

  • I have this code to set the x and y dpi but it always get ignored. I tried different versions of NuGet GhostScript and I also tried different versions of ghostscript.exe still same issue 

    Here is my code:

    var xDpi = 300; //set the x DPI
                var yDpi = 300; //set the y DPI
                              
                using (var rasterizer = new GhostscriptRasterizer()) //create an instance for GhostscriptRasterizer
                {
    
                    rasterizer.Open(fileName); //opens the PDF file for rasterizing
                    int PdfPages = rasterizer.PageCount;
                    for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
                    {
                        //set the output image(png's) complete path
                        string outputPNGPath = Path.Combine(fileNameResultDirectory, "00" + pageNumber.ToString() + ".tif");
    
    
                        //converts the PDF pages to png's 
                        Image pdf2PNG = rasterizer.GetPage(xDpi, yDpi, pageNumber);
    
                        //save the png's
                        pdf2PNG.Save(outputPNGPath, ImageFormat.Tiff);
                    }
    
    
    
                }

    Also tried:

    rasterizer.CustomSwitches.Add("-r300x300");

    I don't know what else to do... thank you

    Monday, February 10, 2020 2:43 PM

All replies

  • Hi Guest1993,

    Thank you for posting here.

    You can try the following code to get it.

            private static void Test()
            {
                var localGhostscriptDll = Path.Combine(Environment.CurrentDirectory, "gsdll64.dll");
                var localDllInfo = new GhostscriptVersionInfo(localGhostscriptDll);
    
                int desired_x_dpi = 160;
                int desired_y_dpi = 160;
    
                string inputPdfPath = @"d:\d.pdf";
    
                string outputPath = @"d:\Test\";
    
                GhostscriptRasterizer _rasterizer = new GhostscriptRasterizer();
    
                _rasterizer.Open(inputPdfPath, localDllInfo, false);
    
                for (int pageNumber = 1; pageNumber <= _rasterizer.PageCount; pageNumber++)
                {
                    string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");
    
                    Image img = _rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
                    img.Save(pageFilePath, ImageFormat.Png);
                }
    
                _rasterizer.Close();
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, February 11, 2020 3:06 AM
  • Thank you for the reply.

    I get this error: 

    System.DllNotFoundException: 'Ghostscript native library could not be found.'

    on this line 

     _rasterizer.Open(inputFile, localDllInfo, false);

    So I added gsdll32.dll manually to this environment path ..\bin\debug

    because my cp require 32 not 64. However, now I get this error:

    Ghostscript.NET.GhostscriptException: 'Delegate of an exported function couldn't be created for symbol 'gsapi_revision''



    • Edited by Guest1993 Tuesday, February 11, 2020 2:33 PM
    Tuesday, February 11, 2020 2:16 PM
  • Hi Guest1993,

    I managed to reproduce the problem, but unfortunately I tried my best and still couldn't find a solution.

    I'm not familiar with this third party package, maybe you can ask this question in their website.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 12, 2020 6:23 AM
  • Thank you for your time . When you say "I managed to reproduce the problem,"  Does that mean the same code was working and you had to do/undo something to reproduce the issue. 
    Wednesday, February 12, 2020 2:14 PM
  • Hi Guest1993,

    Because the dll file I downloaded was gsdll64.dll, so I changed the target platform of my project to x64, the code worked well, but when I changed the dll file to gsdll32.dll, I got the same error as you.

    The following is the link where I downloaded gsdll64 and the execution results of my code above.

    gsdll64.dll

    Result:

    Note: The path of the above code is wrong, I have modified it.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, February 14, 2020 6:33 AM
  • For some reason I can only use 32. I am trying to see if installation has something to do with the error because they have a read me file in the 32 zip. Also, no response from their site.
    • Edited by Guest1993 Friday, February 14, 2020 4:37 PM
    Friday, February 14, 2020 2:57 PM
  • Hi Guest1993,

    I's sorry to hear that.

    But I am too new to this third party dll.

    Maybe you can consider using other products.

    how to convert pdf files to image

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 17, 2020 1:59 AM
  • Thank you for your help along. I used ImageMagick it works fine but extremely slow. This approach is the best. Someone suggested this but I don't know how to use and can't find code samples.
    Monday, February 17, 2020 2:17 PM
  • Hi Guest1993,

    In addition to ImageMagick, there are many ways to choose.

    Spire.PDF

    PDF to Image – Convert PDF pages to images

    As for pdfdocument, it's an API of UWP. It's a very complicated thing to use it in console app or Windows Forms app. If you want to use it, I think it's easier for you to create a uwp application.

    You can raise questions about uwp in stackoverflow or Microsoft Q&A.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 18, 2020 2:14 AM
  • Unfortunately purchase is not an option, but thank you for all your help.
    Tuesday, February 18, 2020 2:02 PM
  • Not sure if I missed anything in regards to 64 vs 32 bit but you can get the 32 bit version here.

    https://www.ghostscript.com/download/gsdnld.html

    Also make sure "copy local" is true


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, February 18, 2020 2:55 PM
    Moderator
  • Hi, It is true.
    • Edited by Guest1993 Tuesday, February 18, 2020 3:42 PM
    Tuesday, February 18, 2020 3:16 PM
  • Please see this.

    Also this works but same issue: 

    using Ghostscript.NET;
    using Ghostscript.NET.Rasterizer;
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    
    namespace ConvertPDFtoTIFFtest
    {
        class Program
        {
    
            public GhostscriptVersionInfo _lastInstalledVersion = null;
            public GhostscriptRasterizer _rasterizer = null;
            static void Main()
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();
                string inputPdfPath = @"C:\Users\n\Desktop\images\DropHere\CFOI 7161_Report.pdf";
                string outputPath = @"C:\Users\n\Desktop\images\DropHere\";
                int desired_x_dpi = 300;
                int desired_y_dpi = 300;
                GhostscriptVersionInfo _lastInstalledVersion = GhostscriptVersionInfo.GetLastInstalledVersion();
    
                GhostscriptRasterizer _rasterizer = new GhostscriptRasterizer();
       
                _rasterizer.Open(inputPdfPath, _lastInstalledVersion, false);
    
                for (int pageNumber = 1; pageNumber <= _rasterizer.PageCount; pageNumber++)
                {
                    string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");
    
                    Image img = _rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
                
                    img.Save(pageFilePath, ImageFormat.Png);
    
                    Console.WriteLine(pageFilePath);
                }
    }
    }
    }
    At this point I might just use the paid version. 

    • Edited by Guest1993 Tuesday, February 18, 2020 9:47 PM
    Tuesday, February 18, 2020 3:45 PM
  • Hi Guest,

    When I use gsdll64.dll to successfully convert a pdf to an image, it also takes a lot of time.

    I think this may be unavoidable. After all, converting df to an image is not as simple as ordinary input and output. Some paid products may improve efficiency, but it always takes a certain amount of time.

    Best Regards,

    Timon



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 19, 2020 8:02 AM
  • It significantly faster than ImageMaigck. One last thing from you please. When you run my original code, you have the dpi issue as well or is it just my environment.   
    Wednesday, February 19, 2020 3:07 PM
  • Hi Guest,

    When I use your code, I can't even make it work, it seems that I can't load the pdf file.

    I don't know what can I do without any error, so I may not be able to answer your question.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    6 hours 27 minutes ago