C#PDF转图片

1.spire

官方的有水印,通过引用

复制代码
 //private readonly static PdfDocument doc = new PdfDocument();
        //public static Stream PtfToPng(string FileInfo)
        //{
        //byte[] by = Convert.FromBase64String(FileInfo);
        //doc.LoadFromBytes(by);
        //Image bmp = doc.SaveAsImage(0);//默认第一页
        //return ImageToStream(bmp);
或者 //遍历PDF每一页 //for (int i = 0; i < doc.Pages.Count; i++) //{ // //将PDF页转换成Bitmap图形 // Image bmp = doc.SaveAsImage(i); // //将Bitmap图形保存为Png格式的图片 // string fileName = string.Format("Page-{0}.png", i + 1); // bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); //} //}
复制代码

2.

Install-Package PdfiumViewer -Version 2.13.0

Install-Package ImageResizer.Plugins.PdfiumRenderer.Pdfium.Dll -Version 1.0.1

 不支持32位程序

复制代码
        /// <summary>
        /// ptf转图片流
        /// </summary>
        /// FileInf base64string
        /// <returns></returns>
        public static Stream PtfToPng(string FileInfo)
        {
            byte[] by = Convert.FromBase64String(FileInfo);
            Stream sm = new MemoryStream(by);
            using (var document = PdfiumViewer.PdfDocument.Load(sm))
            {
                var size = document.PageSizes[0];
                Image ig = document.Render(0, (int)size.Width, (int)size.Height, 300, 300, PdfiumViewer.PdfRenderFlags.Annotations);
                return ImageToStream(ig);
            }
        }

        /// <summary>
        /// 返回图片流
        /// </summary>
        public static Stream ImageToStream(Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms;
        }
复制代码

3.aspose.ptf,最下方有方块,

复制代码
private Stream ConvertToImage(Stream sm)
{
    Document doc = new Document(sm);
    if (doc == null)
        Console.Write(doc);
    MemoryStream stream = new MemoryStream();
    Resolution reso = new Resolution(128);
    JpegDevice jpegDevice = new JpegDevice(reso, 100);
    jpegDevice.Process(doc.Pages[1], stream);
    return stream;
        //    //Image img = Image.FromStream(stream);
}
复制代码

 4.O2S.Components.PDFRender4NET

复制代码
 /// <summary>
        /// 将PDF文档转换为图片的方法
        /// </summary>
        /// <param name="definition">设置图片的清晰度,数字越大越清晰(1-10)</param>
        public static Stream ConvertPdfImage(string Base64, int definition)
        {
            byte[] by = Convert.FromBase64String(Base64);
            Stream sm = new MemoryStream(by);
            PDFFile pdfFile = PDFFile.Open(sm);
            Bitmap pageImage = pdfFile.GetPageImage(0, 56 * definition);
            pdfFile.Dispose();
            return ImageToStream(pageImage);
        }

        /// <summary>
        /// 返回图片流
        /// </summary>
        public static Stream ImageToStream(Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms;
        }
复制代码

无水印的O2S.Components.PDFRender4NET地址https://pan.baidu.com/s/1h8Zb4j4ofUnS4ZBoHJecHQ        码:cum8

转自:https://www.cnblogs.com/shuaimeng/p/10523790.html

posted @ 2021-01-25 19:05  yangyang1900  阅读(1602)  评论(0编辑  收藏  举报