当前位置:网站首页>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
边栏推荐
- 【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
- Code management tools
- Use @data in Lombok to simplify entity class code
- 表达式的动态解析和计算,Flee用起来真香
- 爬虫代码基础教学
- 认识弹性盒子flex
- In depth learning
- 10万奖金被瓜分,快来认识这位上榜者里的“乘风破浪的姐姐”
- 利用C#实现Pdf转图片
- C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
猜你喜欢

Numerical method for solving optimal control problem (I) -- gradient method

SQL实现将多行记录合并成一行

2022 cisp-pte (III) command execution

2022 CISP-PTE(三)命令执行

golang操作redis:写入、读取hash类型数据

Reinstalling the system displays "setup is applying system settings" stationary

Summary of UI module design and practical application of agent mode

“我为开源打榜狂”第一周榜单公布,160位开发者上榜

Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)

These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
随机推荐
Install VM tools
[C /vb.net] convert PDF to svg/image, svg/image to PDF
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
[system design] proximity service
YOLOV3学习笔记
ssh链接远程服务器 及 远程图形化界面的本地显示
SQL implementation merges multiple rows of records into one row
Pdf files can only print out the first page
opencv
Mysql database binlog log enable record
vmware虚拟机C盘扩容
2022 CISP-PTE(三)命令执行
Request weather interface format, automation
Various usages of MySQL backup database to create table select and how many days are left
冒泡排序的简单理解
[untitled] 5 self use history
Oracle Database Introduction
Unit test framework + Test Suite
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
. Net program configuration file operation (INI, CFG, config)