Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Pdf file pages to Images with itextsharp

Tags:

c#

itextsharp

I want to convert Pdf pages in Images using ItextSharp lib.

Have any idea how to convert each page in image file

like image 663
Prithvi Raj Nandiwal Avatar asked Apr 12 '12 14:04

Prithvi Raj Nandiwal


3 Answers

iText/iTextSharp can generate and/or modify existing PDFs but they do not perform any rendering which is what you are looking for. I would recommend checking out Ghostscript or some other library that knows how to actually render a PDF.

like image 192
Chris Haas Avatar answered Oct 09 '22 01:10

Chris Haas


you can use ImageMagick convert pdf to image

convert -density 300 "d:\1.pdf" -scale @1500000 "d:\a.jpg"

and split pdf can use itextsharp

here is the code from others.

void SplitePDF(string filepath)
    {
        iTextSharp.text.pdf.PdfReader reader = null;
        int currentPage = 1;
        int pageCount = 0;
        //string filepath_New = filepath + "\\PDFDestination\\";

        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        //byte[] arrayofPassword = encoding.GetBytes(ExistingFilePassword);
        reader = new iTextSharp.text.pdf.PdfReader(filepath);
        reader.RemoveUnusedObjects();
        pageCount = reader.NumberOfPages;
        string ext = System.IO.Path.GetExtension(filepath);
        for (int i = 1; i <= pageCount; i++)
        {
            iTextSharp.text.pdf.PdfReader reader1 = new iTextSharp.text.pdf.PdfReader(filepath);
            string outfile = filepath.Replace((System.IO.Path.GetFileName(filepath)), (System.IO.Path.GetFileName(filepath).Replace(".pdf", "") + "_" + i.ToString()) + ext);
            reader1.RemoveUnusedObjects();
            iTextSharp.text.Document doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage));
            iTextSharp.text.pdf.PdfCopy pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(outfile, System.IO.FileMode.Create));
            doc.Open();
            for (int j = 1; j <= 1; j++)
            {
                iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader1, currentPage);
                pdfCpy.SetFullCompression();
                pdfCpy.AddPage(page);
                currentPage += 1;
            }
            doc.Close();
            pdfCpy.Close();
            reader1.Close();
            reader.Close();

        }
    }
like image 22
changcn Avatar answered Oct 09 '22 02:10

changcn


You can use Ghostscript to convert the PDF files into Images, I used the following parameters to convert the needed PDF into tiff image with multiple frames :

gswin32c.exe   -sDEVICE=tiff12nc -dBATCH -r200 -dNOPAUSE  -sOutputFile=[Output].tiff [PDF FileName]

Also you can use the -q parameter for silent mode You can get more information about its output devices from here

After that I can easily load the tiff frames like the following

using (FileStream stream = new FileStream(@"C:\tEMP\image_$i.tiff", FileMode.Open, FileAccess.Read, FileShare.Read))
{
    BitmapDecoder dec = BitmapDecoder.Create(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.None);
    BitmapEncoder enc = BitmapEncoder.Create(dec.CodecInfo.ContainerFormat);
    enc.Frames.Add(dec.Frames[frameIndex]);
}
like image 33
Amer Sawan Avatar answered Oct 09 '22 02:10

Amer Sawan