4

I want to get the same image quality as if I would use the export pdf to png from Adobe Acrobat.

But somehow this does not work for me. The dimensions that I get if I export the pdf to a png with help of the Adobe Acrobat tool are: Width:11264 pix Height:15940 pix

As you may see I provide you the method that I use to read the pdf an create per page a image. The possibilities that I have are to use the .Render Method which needs a int page(index) float dpiX, float dpiY, bool forPrinting

But some how it has no effect on the image saved ?

using (var document = PdfiumViewer.PdfDocument.Load(file)) 
{
    //This int is used to get the page count for each document 
    int pagecount = document.PageCount;

    //With the int pagecount we can create as may screenshots as there are pages in the document
    for (int index = 0; index < pagecount; index++)
    {
        //render the image created
        var image = document.Render(index,8448,11955, true);

        //savde the created screenshot temporerlay as a png + number (count)
        image.Save(@"C:\Users\chnikos\Desktop\Test\output" + index.ToString("000") + ".png",ImageFormat.Png);
        application.Selection.InlineShapes.AddPicture(@"C:\Users\chnikos\Desktop\Test\output" + index.ToString("000") + ".png");
    }
}
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Niko.K
  • 297
  • 4
  • 17

3 Answers3

6
try
{
    using (var document = PdfiumViewer.PdfDocument.Load(@"C:\Users\ohernandez\Pictures\img\descarga.pdf"))
    {
        for (int index = 0; index < document.PageCount; index++)
        {
            var image = document.Render(index, 300, 300, PdfRenderFlags.CorrectFromDpi);
            image.Save(@"C:\Users\ohernandez\Pictures\img\output.Jpeg" + index.ToString("000") + ".Jpeg", ImageFormat.Jpeg);
        }
    }
}
catch (Exception ex)
{
    // handle exception here;
}
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
ohernandez
  • 96
  • 1
  • 5
0

Use PDFSharp & Migradocs

// Create a new PDF document
PdfDocument document = new PdfDocument();

// Create a font
XFont font = new XFont("Times", 25, XFontStyle.Bold);

PageSize[] pageSizes = (PageSize[])Enum.GetValues(typeof(PageSize));
foreach (PageSize pageSize in pageSizes)
{
  if (pageSize == PageSize.Undefined)
    continue;

  // One page in Portrait...
  PdfPage page = document.AddPage();
  page.Size = pageSize;
  XGraphics gfx = XGraphics.FromPdfPage(page);
  gfx.DrawString(pageSize.ToString(), font, XBrushes.DarkRed,
    new XRect(0, 0, page.Width, page.Height),
    XStringFormat.Center);

  // ... and one in Landscape orientation.
  page = document.AddPage();
  page.Size = pageSize;
  page.Orientation = PageOrientation.Landscape;
  gfx = XGraphics.FromPdfPage(page);
  gfx.DrawString(pageSize.ToString() + " (landscape)", font,
    XBrushes.DarkRed, new XRect(0, 0, page.Width, page.Height),
    XStringFormat.Center);
}

// Save the document...
string filename = "PageSizes.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
Gomze
  • 51
  • 10
  • What are you trying to do here?? You do know that pdfsharp can't convert pdfpage to image right?? It can only draw on a pdfpage.. – ArchAngel Oct 27 '16 at 11:16
  • Yes, it does when you create a pdf file, but when you need to convert the pdf file into images, it doesn't.. That was the question.. – ArchAngel Oct 27 '16 at 16:40
-1

This might be a better example: Library files

using System.Drawing.Imaging;

string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

//This one is with Spire.Pdf
void PDF_To_Image(string pdf)
{
   var doc = new Spire.Pdf.PdfDocument();
   doc.LoadFromFile(pdf);

   //This int is used to get the page count for each document 
   int pagecount = doc.Pages.Count;

   //With the int pagecount we can create as many screenshots as there are pages in the document
   for (int index = 0; index < pagecount; index++)
   {
      //render the image created
      var dpiX = 75;
      var dpiY = 75;
      var image = doc.SaveAsImage(index, Spire.Pdf.Graphics.PdfImageType.Bitmap, dpiX, dpiY);

      //save the created screenshot temporerlay as a png + number (count)
      image.Save(Desktop + @"\Test\output\" + index.ToString("000") + ".png", ImageFormat.Png);
   }
}

//This one is with Aspose.Pdf & xps2img.
//Aspose.Pdf converts the Pdf document to Xps format..
//xps2img creates images from the xps file..
void PDF_To_Image2(string pdf)
{
   var doc = new Aspose.Pdf.Document(pdf);
   var saveOptions = new Aspose.Pdf.XpsSaveOptions();
   doc.Save("Preview.xps", saveOptions);

   xps2img.Parameters pp = new xps2img.Parameters();
   pp.Dpi = 300;
   pp.ImageType = xps2img.ImageType.Png;
   pp.RequiredSize = new System.Drawing.Size((int)doc.PageInfo.Width, (int)doc.PageInfo.Height);
   pp.ImageOptions = xps2img.ImageOptions.Default;
   var img3 = xps2img.Xps2Image.ToBitmap("Preview.xps", pp).ToList();


   //This int is used to get the page count for each document 
   int pagecount = img3.Count;

   //With the int pagecount we can create as many screenshots as there are pages in the document
   for (int index = 0; index < pagecount; index++)
   {
      img3[index].Save(Desktop + @"\Test\Output\" + index.ToString("000") + ".png", ImageFormat.Png);
   }
}
ArchAngel
  • 634
  • 7
  • 16
  • that is not bad the quality is still not as good as the orignal can we improve that somehow ? – Niko.K Oct 27 '16 at 09:27
  • error parameter not valid. and is there a trial version where I dont have this strange header – Niko.K Oct 27 '16 at 09:39
  • The code is working just fine on mine, But I'm looking into a better solution without the header.. – ArchAngel Oct 27 '16 at 09:47
  • This should give you some alternatives, on how to convert pdf pages to images, but Pdf is not an entirely free format to convert from so you are going to lose some quality when the conversion occurs.. – ArchAngel Oct 27 '16 at 11:20
  • Library Files will be deleted in a few days, so anyone that need it, can have a change to get them.. – ArchAngel Oct 27 '16 at 17:21