Save pdf to jpeg using c#

后端 未结 3 1587
北恋
北恋 2020-12-02 09:49

I need to convert a pdf file into jpeg using c#. And the solution(library) have to be free.

I have searched a lot of information but seems that I dont get nothing cl

3条回答
  •  攒了一身酷
    2020-12-02 10:27

    This is how I did it with PDFLibNet:

    public void ConvertPDFtoHojas(string filename, String dirOut)
    {
        PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
        _pdfDoc.LoadPDF(filename);
    
        for (int i = 0; i < _pdfDoc.PageCount; i++)
        {
    
            Image img = RenderPage(_pdfDoc, i);
    
            img.Save(Path.Combine(dirOut, string.Format("{0}{1}.jpg", i,DateTime.Now.ToString("mmss"))));
    
        }
        _pdfDoc.Dispose();
        return;
    }
    public  Image RenderPage(PDFLibNet.PDFWrapper doc, int page)
    {
        doc.CurrentPage = page + 1;
        doc.CurrentX = 0;
        doc.CurrentY = 0;
    
        doc.RenderPage(IntPtr.Zero);
    
            // create an image to draw the page into
            var buffer = new Bitmap(doc.PageWidth, doc.PageHeight);
            doc.ClientBounds = new Rectangle(0, 0, doc.PageWidth, doc.PageHeight);
            using (var g = Graphics.FromImage(buffer))
            {
                var hdc = g.GetHdc();
                try
                {
                    doc.DrawPageHDC(hdc);
                }
                finally
                {
                    g.ReleaseHdc();
                }
            }
            return buffer;
    
    }
    

提交回复
热议问题