当前位置:网站首页>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
边栏推荐
- Floating menu operation
- How to scan when Canon c3120l is a network shared printer
- DBNet:具有可微分二值化的实时场景文本检测
- In depth learning
- 【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
- Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)
- How does the insurance company check hypertension?
- 表达式的动态解析和计算,Flee用起来真香
- SQL implementation merges multiple rows of records into one row
- UTC time, GMT time, CST time
猜你喜欢
vmware虚拟机C盘扩容
Support vector machine for machine learning
Create your own deep learning environment with CONDA
Redis cluster creation, capacity expansion and capacity reduction
SQL implementation merges multiple rows of records into one row
Push box games C #
The dynamic analysis and calculation of expressions are really delicious for flee
HMS core helps baby bus show high-quality children's digital content to global developers
2022年华东师范大学计科考研复试机试题-详细题解
How to scan when Canon c3120l is a network shared printer
随机推荐
SQL实现将多行记录合并成一行
After the Chrome browser is updated, lodop printing cannot be called
UTC时间、GMT时间、CST时间
堆排序和优先队列
HMS core helps baby bus show high-quality children's digital content to global developers
认识弹性盒子flex
New knowledge! The virtual machine network card causes your DNS resolution to slow down
Creating postgre enterprise database by ArcGIS
使用conda创建自己的深度学习环境
Various usages of MySQL backup database to create table select and how many days are left
Numerical method for solving optimal control problem (I) -- gradient method
YOLOV2学习与总结
POI dealing with Excel learning
这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意
(翻译)异步编程:Async/Await在ASP.NET中的介绍
【5G NR】UE注册流程
Important knowledge points of redis
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
In depth learning
冒泡排序的简单理解