当前位置:网站首页>Realize PDF to picture conversion with C #
Realize PDF to picture conversion with C #
2022-07-03 06:40:00 【zxy2847225301】
This week I came across a demand , Due to the company's system framework , It cannot directly display the information returned by the third party pdf( Explain , Returned by the third party pdf Is to bring the signature information ( That is to say pdf Signature pictures are added to )), Need to put pdf Turn it into a picture for display , But in the process of doing it, I stepped on a lot of thunder . Finally, use third-party plug-ins PDFRender4NET
1 Third party plug-ins PdfiumViewer( shortcoming , Missing signature information )
First, I tried a third-party plug-in PdfiumViewer, The code is simple , There are a lot of them on the Internet demo, Copy the code and modify it , It's done in three or two , Tried it on , Indeed, it can be achieved pdf Transfer pictures , But when I finish writing the business code , When running on a business system , Find out , Mama of , It's careless , The converted image has lost its signature information . Here is some of the code I have slightly modified :
public class PdfToImageHelper
{
/// <summary>
/// pdf Turn picture (base64 Format string )
/// </summary>
/// <param name="pdfBase64String">pdf Corresponding base64 character string </param>
/// <returns>Pdf If there are more than one page , Just return multiple pictures (base64 String collection )</returns>
public static List<string> GetBase64StringArray(string pdfBase64String)
{
if (pdfBase64String==null|| pdfBase64String.Length==0) return null;
List<string> base64StringList = new List<string>();
byte[] buffer=Convert.FromBase64String(pdfBase64String);
if (buffer == null || buffer.Length == 0) return base64StringList;
MemoryStream ms = new MemoryStream(buffer);
var pdfDocument = PdfiumViewer.PdfDocument.Load(ms);
for (int index = 0; index <pdfDocument.PageCount; index++)
{
Image image = pdfDocument.Render(index, (int)pdfDocument.PageSizes[index].Width, (int)pdfDocument.PageSizes[index].Height, 300, 300, false);
string base64Str=ImageToBase64String(image);
if (base64Str != null && base64Str.Length > 0)
{
base64StringList.Add(base64Str);
}
}
// Free up streaming resources
return base64StringList;
}
/// <summary>
/// Image Object turn base64 character string
/// </summary>
/// <param name="Picture"></param>
/// <returns></returns>
private static string ImageToBase64String(Image Picture)
{
MemoryStream ms = new MemoryStream();
if (Picture == null)
return null;
Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] BPicture = new byte[ms.Length];
BPicture = ms.GetBuffer();
// Free up streaming resources
return Convert.ToBase64String(BPicture);
}
}
call :List<string> imageBase64StringList=PdfToImageHelper.GetBase64StringArray("pdf Corresponding base64 character string ");
2 Third party plug-ins Spire.pdf ( shortcoming : charge , There is a free version of , however pdf There is a page limit for converting to pictures ( most 3 page ) , And the converted image is very blurred )
Use PdfiumViewer No, after , Start using Spire.pdf, adopt vistual studio Of nuget You can get dll, Here's the picture :
first Spire.PDF Is the charge , The watermark information shown in the following figure will be carried on the upper left corner of the converted image
the second FreeSpire.PDF It's free. , however pdf If exceeded 3 page , Only turn forward 3 page , The following conversion is all blank pages
The code is not posted , There are many on the Internet demo
3 Third party plug-ins PDFRender4NET(O2S.Components.PDFRender4NET.dll, The version information is shown in the figure below )
The code I slightly modified is posted below :
using O2S.Components.PDFRender4NET;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
namespace iih.gdrmh.ca.PatientSign.bp
{
public class PdfToImageHelper
{
public static List<string> GetBase64StringArrayByPdfPath(string pdfPath)
{
if (pdfPath == null || pdfPath.Length == 0) return null;
List<string> base64StringList = new List<string>();
PDFFile pdfFile = PDFFile.Open(pdfPath);
for (int index =0; index <pdfFile.PageCount; index++)
{
Bitmap pageImage = pdfFile.GetPageImage(index, 56 * 10);
string base64Str = BitmapToBase64String(pageImage);
if (base64Str != null && base64Str.Length > 0)
{
base64StringList.Add(base64Str);
}
}
pdfFile.Dispose();
return base64StringList;
}
private static string ImageToBase64String(Image Picture)
{
MemoryStream ms = new MemoryStream();
if (Picture == null)
return null;
Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] BPicture = new byte[ms.Length];
BPicture = ms.GetBuffer();
return Convert.ToBase64String(BPicture);
}
private static string BitmapToBase64String(Bitmap bitmap)
{
// 1. First the BitMap Convert to memory stream
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
// 2. Then the memory is transferred to byte[] And back to
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, bytes.Length);
ms.Flush();
ms.Close();
ms.Dispose();
return Convert.ToBase64String(bytes);
}
}
}
call :List<string> imageBase64StringList=PdfToImageHelper.GetBase64StringArrayByPdfPath("pdf The corresponding file path ");
Finally found , The converted image , The signature information is still , The sharpness ratio of the converted picture FreeSpire.PDF Still high
expand :
Go to stack overflow Search discovery ,pdf There are many ways to convert pictures , But the most recommended is Ghostscript.NET. github The address is :https://github.com/jhabjan/Ghostscript.NET demo Code :https://github.com/jhabjan/Ghostscript.NET/blob/master/Ghostscript.NET.Samples/Samples/RasterizerSample1.cs
stack overflow Reference link :
1 Convert Pdf to Image C# .NET - Stack Overflow
2 Converting pdf to image using c# and Ghostscript - Stack Overflow
3 asp.net - Convert PDF file to images using C# - Stack Overflow
边栏推荐
- Apifix installation
- ODL framework project construction trial -demo
- 堆排序和优先队列
- SQL实现将多行记录合并成一行
- UNI-APP中条件注释 实现跨段兼容、导航跳转 和 传参、组件创建使用和生命周期函数
- YOLOV3学习笔记
- Time format record
- Selenium ide installation recording and local project maintenance
- Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
- Ruoyi interface permission verification
猜你喜欢
Read blog type data from mysql, Chinese garbled code - solved
数值法求解最优控制问题(一)——梯度法
Golang operation redis: write and read kV data
Chapter 8. MapReduce production experience
2022 cisp-pte (III) command execution
2022年华东师范大学计科考研复试机试题-详细题解
Selenium - 改变窗口大小,不同机型呈现的宽高长度会不一样
有意思的鼠標指針交互探究
Summary of the design and implementation of the weapon system similar to the paladin of vitality
第8章、MapReduce 生产经验
随机推荐
. Net program configuration file operation (INI, CFG, config)
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
Interface test weather API
保险公司怎么查高血压?
Decision tree of machine learning
Personally design a highly concurrent seckill system
【无标题】
[C /vb.net] convert PDF to svg/image, svg/image to PDF
pytorch练习小项目
Page text acquisition
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
Request weather interface format, automation
Chapter 8. MapReduce production experience
Cannot get value with @value, null
利用C#实现Pdf转图片
SSH link remote server and local display of remote graphical interface
Install VM tools
Scroll view specifies the starting position of the scrolling element
Understand software testing
Daily question brushing record (11)