当前位置:网站首页>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
边栏推荐
- Install VM tools
- Redis cluster creation, capacity expansion and capacity reduction
- How does the insurance company check hypertension?
- Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
- 【无标题】8 简易版通讯录
- 熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
- Push box games C #
- HMS core helps baby bus show high-quality children's digital content to global developers
- Some thoughts on machine learning
- YOLOV3学习笔记
猜你喜欢

Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)

ROS+Pytorch的联合使用示例(语义分割)

熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
![[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)](/img/a4/00aca72b268f77fe4fb24ac06289f5.jpg)
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)

HMS core helps baby bus show high-quality children's digital content to global developers

数值法求解最优控制问题(一)——梯度法

scroll-view指定滚动元素的起始位置

Local rviz call and display of remote rostopic

【类和对象】深入浅出类和对象

Daily question brushing record (11)
随机推荐
Local rviz call and display of remote rostopic
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
[5g NR] UE registration process
The most classic 100 sentences in the world famous works
vmware虚拟机C盘扩容
Unittest attempt
Climb movie paradise 2021 hot
Une exploration intéressante de l'interaction souris - pointeur
Introduction to software engineering
Time format record
error C2017: 非法的转义序列
Pdf files can only print out the first page
Opencv mouse and keyboard events
卡特兰数(Catalan)的应用场景
Shell conditional statement
Important knowledge points of redis
【code】if (list != null && list.size() > 0)优化,集合判空实现方式
Oracle database synonym creation
Know flex box
Code management tools