C# 如何将PDF文件转换为图像
我需要将PDF文件转换为图像。如果PDF文件是多页的,我只需要一个包含所有PDF页面的图像 有没有像Acrobat产品那样收费的开源解决方案?线程“”适合您的要求 一种解决方案是使用第三方库。是一个非常受欢迎的网站,也是免费提供的。你可以为它买一个.NET包装器。原始ImageMagick下载页面为C# 如何将PDF文件转换为图像,c#,image,pdf,C#,Image,Pdf,我需要将PDF文件转换为图像。如果PDF文件是多页的,我只需要一个包含所有PDF页面的图像 有没有像Acrobat产品那样收费的开源解决方案?线程“”适合您的要求 一种解决方案是使用第三方库。是一个非常受欢迎的网站,也是免费提供的。你可以为它买一个.NET包装器。原始ImageMagick下载页面为 使用Solid Framework将PDF页面转换为图像文件(死链接,已删除的文档位于) 通用文档转换器 你也可以看看这条线 “” 如果使用,则可以使用此类从中检索位图 您可以使用将PDF转换
- 使用Solid Framework将PDF页面转换为图像文件(死链接,已删除的文档位于)
- 通用文档转换器
GetImage(outputFileName, firstPage, lastPage, resolution, imageFormat)
Converts specifies pages to image and save them to outputFileName (tiff allows multi-page or creates several files)
您可以在这里查看:至于2018年,如何将PDF文档转换为C#格式图像的问题仍然没有一个简单的答案;许多图书馆使用,在大多数情况下,生产使用需要昂贵的商业许可证 一个好的替代方案可能是使用流行的具有许可证的“pdftoppm”实用程序;它可以从C#作为命令行工具,与System.Diagnostics.Process一起执行。流行的工具在Linux世界是众所周知的,但也可以使用 如果您不想自己集成pdftoppm,您可以使用my(同时支持经典.NET Framework和)—它不是免费的,但价格非常便宜。NuGet软件包是免费的,并且只受非常开放的保护 我已经做了一些测试,这是将PDF文件转换为图像的代码(tt将图像保存在调试文件夹中)
使用cs\u pdf\u制作图像;
使用PdfToImage;
私有void BtnConvert_单击(对象发送者,事件参数e)
{
如果(openFileDialog1.ShowDialog()==DialogResult.OK)
{
尝试
{
字符串PdfFile=openFileDialog1.FileName;
字符串PngFile=“Convert.png”;
列表转换=cs_pdf_to_image.Pdf2Image.Convert(PdfFile,PngFile);
位图输出=新位图(PngFile);
PbConversion.Image=输出;
}
捕获(例外E)
{
MessageBox.Show(E.Message);
}
}
}
Google Chrome中使用的PDF引擎名为“BSD 3-clause”许可证下的开源引擎。我相信这允许在商业产品中使用时重新分配。
有一个名为()的.NET包装器,在我尝试过的范围内,它工作得很好。它在Apache许可证下,该许可证还允许重新分发
(请注意,这与需要商业许可证的“包装器”不同*)
(还有另外一个PDFium.NET包装器,但我尚未对其进行评估。)
在我看来,到目前为止,这可能是开源(像啤酒一样免费)PDF库的最佳选择,因为它们不会限制使用它们的软件的封闭源代码/商业性质。据我所知,我认为这里的答案中没有任何其他内容符合该标准。使用Android默认库(如AppCompat),您可以将所有PDF页面转换为图像。这种方法是非常快速和优化的下面的代码用于获取PDF页面的单独图像。它非常快
ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(new File("pdfFilePath.pdf"), MODE_READ_ONLY);
PdfRenderer renderer = new PdfRenderer(fileDescriptor);
final int pageCount = renderer.getPageCount();
for (int i = 0; i < pageCount; i++) {
PdfRenderer.Page page = renderer.openPage(i);
Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap, 0, 0, null);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
page.close();
if (bitmap == null)
return null;
if (bitmapIsBlankOrWhite(bitmap))
return null;
String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + filename + ".png");
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
Log.v("Saved Image - ", file.getAbsolutePath());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
ParcelFileDescriptor fileDescriptor=ParcelFileDescriptor.open(新文件(“pdfFilePath.pdf”),模式为只读);
PdfRenderer renderer=新的PdfRenderer(文件描述符);
final int pageCount=renderer.getPageCount();
for(int i=0;i
=======================================================
private static boolean bitmapIsBlankOrWhite(Bitmap bitmap) {
if (bitmap == null)
return true;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int pixel = bitmap.getPixel(i, j);
if (pixel != Color.WHITE) {
return false;
}
}
}
return true;
}
private静态布尔bitmapIsBlankOrWhite(位图){
如果(位图==null)
返回true;
int w=bitmap.getWidth();
int h=bitmap.getHeight();
对于(int i=0;i
关于:经过细化后,我能够创建PNG文件
using cs_pdf_to_image;
using PdfToImage;
private void BtnConvert_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
string PdfFile = openFileDialog1.FileName;
string PngFile = "Convert.png";
List<string> Conversion = cs_pdf_to_image.Pdf2Image.Convert(PdfFile, PngFile);
Bitmap Output = new Bitmap(PngFile);
PbConversion.Image = Output;
}
catch(Exception E)
{
MessageBox.Show(E.Message);
}
}
}
ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(new File("pdfFilePath.pdf"), MODE_READ_ONLY);
PdfRenderer renderer = new PdfRenderer(fileDescriptor);
final int pageCount = renderer.getPageCount();
for (int i = 0; i < pageCount; i++) {
PdfRenderer.Page page = renderer.openPage(i);
Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap, 0, 0, null);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
page.close();
if (bitmap == null)
return null;
if (bitmapIsBlankOrWhite(bitmap))
return null;
String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + filename + ".png");
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
Log.v("Saved Image - ", file.getAbsolutePath());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean bitmapIsBlankOrWhite(Bitmap bitmap) {
if (bitmap == null)
return true;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int pixel = bitmap.getPixel(i, j);
if (pixel != Color.WHITE) {
return false;
}
}
}
return true;
}
using PDFiumSharp;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
public class Program
{
static public void Main(String[] args)
{
var renderfoo = new Renderfoo()
renderfoo.RenderPDFAsImages(@"C:\Temp\example.pdf", @"C:\temp");
}
}
public class Renderfoo
{
public void RenderPDFAsImages(string Inputfile, string OutputFolder)
{
string fileName = Path.GetFileNameWithoutExtension(Inputfile);
using (PDFiumSharp.PdfDocument doc = new PDFiumSharp.PdfDocument(Inputfile))
{
for (int i = 0; i < doc.Pages.Count; i++)
{
var page = doc.Pages[i];
using (var bitmap = new System.Drawing.Bitmap((int)page.Width, (int)page.Height))
{
var grahpics = Graphics.FromImage(bitmap);
grahpics.Clear(Color.White);
page.Render(bitmap);
var targetFile = Path.Combine(OutputFolder, fileName + "_" + i + ".png");
bitmap.Save(targetFile);
}
}
}
}
}
/// <summary>
/// Saves a thumbnail (jpg) to the same folder as the PDF file, using dimensions 300x423,
/// which corresponds to the aspect ratio of 'A' paper sizes like A4 (ratio h/w=sqrt(2))
/// </summary>
/// <param name="pdfPath">Source path of the pdf file.</param>
/// <param name="thumbnailPath">Target path of the thumbnail file.</param>
/// <param name="width"></param>
/// <param name="height"></param>
public static void SaveThumbnail(string pdfPath, string thumbnailPath = "", int width = 300, int height = 423)
{
using var pdfDocument = new PdfDocument(pdfPath);
var firstPage = pdfDocument.Pages[0];
using var pageBitmap = new PDFiumBitmap(width, height, true);
firstPage.Render(pageBitmap);
var imageJpgPath = string.IsNullOrWhiteSpace(thumbnailPath)
? Path.ChangeExtension(pdfPath, "jpg")
: thumbnailPath;
var image = Image.Load(pageBitmap.AsBmpStream());
// Set the background to white, otherwise it's black. https://github.com/SixLabors/ImageSharp/issues/355#issuecomment-333133991
image.Mutate(x => x.BackgroundColor(Rgba32.White));
image.Save(imageJpgPath, new JpegEncoder());
}
javar -jar pdfbox-app-2.0.19.jar PDFToImage -quality 1.0 -dpi 150 -prefix out_dir/page -format png
string file = "c:\\tmp\\test.pdf";
List<System.Drawing.Image> images = PdfSplitter.GetImages(file, PdfSplitter.Scale.High);
PdfSplitter.WriteImages(file, "c:\\tmp", PdfSplitter.Scale.High, PdfSplitter.CompressionLevel.Medium);
string pdf_filename = @"C:\TEMP\test.pdf";
var pdf2Image = new Cyotek.GhostScript.PdfConversion.Pdf2Image(pdf_filename);
for (var page = 1; page < pdf2Image.PageCount; page++)
{
string png_filename = @"C:\TEMP\test" + page + ".png";
pdf2Image.ConvertPdfPageToImage(png_filename, page);
}
var pageIndex = 0;
var scale = 2;
fpdfview.FPDF_InitLibrary();
var document = fpdfview.FPDF_LoadDocument("test.pdf", null);
var page = fpdfview.FPDF_LoadPage(document, pageIndex);
var size = new FS_SIZEF_();
fpdfview.FPDF_GetPageSizeByIndexF(document, 0, size);
var width = (int)Math.Round(size.Width * scale);
var height = (int)Math.Round(size.Height * scale);
var bitmap = fpdfview.FPDFBitmapCreateEx(
width,
height,
4, // BGRA
IntPtr.Zero,
0);
fpdfview.FPDFBitmapFillRect(bitmap, 0, 0, width, height, (uint)Color.White.ToArgb());
// | | a b 0 |
// | matrix = | c d 0 |
// | | e f 1 |
using var matrix = new FS_MATRIX_();
using var clipping = new FS_RECTF_();
matrix.A = scale;
matrix.B = 0;
matrix.C = 0;
matrix.D = scale;
matrix.E = 0;
matrix.F = 0;
clipping.Left = 0;
clipping.Right = width;
clipping.Bottom = 0;
clipping.Top = height;
fpdfview.FPDF_RenderPageBitmapWithMatrix(bitmap, page, matrix, clipping, (int)RenderFlags.RenderAnnotations);
var bitmapImage = new Bitmap(
width,
height,
fpdfview.FPDFBitmapGetStride(bitmap),
PixelFormat.Format32bppArgb,
fpdfview.FPDFBitmapGetBuffer(bitmap));
bitmapImage.Save("test.jpg", ImageFormat.Jpeg);