Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert pdf to tiff(Each page will be an image file). I couldn't find any examples doing this using the library.
Here is what I have so far.
string inputPdf = @"C:\Users\Desktop\pdfconverttes\name.pdf";
           string outputPng = @"C:\Users\Desktop\pdfconverttes\name.tif";
           using (MagickImage images = new MagickImage())
           {
               images.Read(inputPdf);
               using (MagickImage  vertical = images.AppendVertically())//this works with imagemagick not graphicsmagick.
               {
                   vertical.Format = MagickFormat.Tiff;
                   vertical.Density = new Density(300);
                   vertical.Write(outputPng);
               }
           }

This image creates one image that has all pdf pages in it.

What I have tried:

What I tried to create multiple images:
using ImageMagick;
 MagickReadSettings settings = new MagickReadSettings();
            // Settings the density to 300 dpi will create an image with a better quality
            settings.Density = new Density(300);

            using (MagickImageCollection images = new MagickImageCollection())
            {
                // Add all the pages of the pdf file to the collection
                images.Read(@"C:\Users\Desktop\pdfconverttes\name.pdf", settings);

                int page = 1;
                foreach (MagickImage image in images)
                {
                    // Write page to file that contains the page number
                    image.Write(@"C: \Users\Desktop\pdfconverttes\name.png" + page + ".png");
                    // Writing to a specific format works the same as for a single image
                    image.Format = MagickFormat.Ptif;
                    image.Write(@"C: \Users\Desktop\pdfconverttes\name.tif" + page + ".tif");
                    page++;
                }
            }

Unhandled Exception: ImageMagick.MagickCoderErrorException: WriteBlob Failed
This is using ImageMagick.
Side Question: Is there a way to avoid purchasing GhostScript to use ImageMagick
Posted
Comments
Richard MacCutchan 31-Jan-20 11:36am    
image.Write(@"C: \Users\Desktop\pdfconverttes\name.png" + page + ".png");
That space after C: may be a problem. Also, you cannot write to a user's disk in ASP.NET.
AskalotLearnalot 31-Jan-20 13:30pm    
wow, thank you..can you post as answer. "Also, you cannot write to a user's disk in ASP.NET" do you mean bad practice.
Afzaal Ahmad Zeeshan 31-Jan-20 14:57pm    
It means that your ASP.NET web application does not have access to the user's machine (including their hard drives). The best thing to do is to allow the users to download a file, which browser will allow them to save on their device.

Otherwise, a web application cannot access the user's machine due to sandboxing applied by browsers for security and privacy reasons.
Richard MacCutchan 31-Jan-20 15:41pm    
I mean it is prevented by browser security. Think about it: if a website could store or modify files on the user's system then they could do untold damage. Also, since the code you are using is C# that only runs on the server, so it will fail for other reasons.

1 solution

Remove the space from both paths after the c: as suggested by @RichardMacCutchan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900