Niko.K Niko.K - 2 months ago 9
C# Question

Read all pages from PDF c#

I want to read all pages of my PDF and save them as a images, so far what I am doing is only getting me the page defined

0 = 1
first etc .. Is there a chance that I can define a range ?

static void Main(string[] args)
{
try
{
string path = @"C:\Users\test\Desktop\pdfToWord\";
foreach (string file in Directory.EnumerateFiles(path, "*.pdf")) {
using (var document = PdfiumViewer.PdfDocument.Load(file))
{
int i = 1;
var image = document.Render(0,300,300, true);
image.Save(@"C:\Users\test\Desktop\pdfToWord\output.png", ImageFormat.Png);
}
}
}
catch (Exception ex)
{
// handle exception here;
}

Answer

if your document-object gives you the pagecount,

you could replace

int i = 1;
var image = document.Render(0,300,300, true);
image.Save(@"C:\Users\test\Desktop\pdfToWord\output.png", ImageFormat.Png);

by

for(int index = 0; index < document.PageCount; index++)
{
     var image = document.Render(index,300,300, true);
     image.Save(@"C:\Users\test\Desktop\pdfToWord\output"+index.ToString("000")+".png", ImageFormat.Png);
}