我有一些文件.它的格式是PDF.现在有一个项目,这个项目需要将PDF文件转换为Image.如果PDF是多页,我只需要一个包含所有PDF页面的图像.我从谷歌得到了一些答案,但有些工具是收费的.
那么,如何用C#解决呢?非常感谢你!
我知道,在C#中使用Acrobat.dll可以解决这个问题,但它必须安装Adobe Acrobat并且它不是免费的.
HAB*_*JAN 16
您可以使用Ghostscript将PDF转换为图像.
要使用.NET中的Ghostscript,您可以查看Ghostscript.NET库(Ghostscript库周围的托管包装器).
要使用Ghostscript.NET从PDF生成图像,请查看RasterizerSample.
要将多个图像组合到单个图像中,请查看以下示例:http://www.niteshluharuka.com/2012/08/combine-several-images-to-form-a-single-image-using-c/#
Gau*_*kke 15
以下主题适合您的请求. 将pdf文件转换为jpeg图像
一种解决方案是使用第三方库.ImageMagick非常受欢迎,也可以免费使用.您可以在此处获取.NET包装器.最初的ImageMagick下载页面在这里.
你也可以看一下这个帖子: 如何从C#中的pictureBox中的pdf文件打开一个页面
如果使用此过程将PDF转换为tiff,则可以使用此类从tiff检索位图.
public class TiffImage
{
private string myPath;
private Guid myGuid;
private FrameDimension myDimension;
public ArrayList myImages = new ArrayList();
private int myPageCount;
private Bitmap myBMP;
public TiffImage(string path)
{
MemoryStream ms;
Image myImage;
myPath = path;
FileStream fs = new FileStream(myPath, FileMode.Open);
myImage = Image.FromStream(fs);
myGuid = myImage.FrameDimensionsList[0];
myDimension = new FrameDimension(myGuid);
myPageCount = myImage.GetFrameCount(myDimension);
for (int i = 0; i < myPageCount; i++)
{
ms = new MemoryStream();
myImage.SelectActiveFrame(myDimension, i);
myImage.Save(ms, ImageFormat.Bmp);
myBMP = new Bitmap(ms);
myImages.Add(myBMP);
ms.Close();
}
fs.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
private void button1_Click(object sender, EventArgs e)
{
TiffImage myTiff = new TiffImage("D:\\Some.tif");
//imageBox is a PictureBox control, and the [] operators pass back
//the Bitmap stored at that position in the myImages ArrayList in the TiffImage
this.pictureBox1.Image = (Bitmap)myTiff.myImages[0];
this.pictureBox2.Image = (Bitmap)myTiff.myImages[1];
this.pictureBox3.Image = (Bitmap)myTiff.myImages[2];
}
Run Code Online (Sandbox Code Playgroud)
tje*_*ans 11
我在.NET Standard 2.1 类库中使用了PDFiumSharp和ImageSharp 。
/// <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());
}
Run Code Online (Sandbox Code Playgroud)
Vit*_*nko 10
至于2018年的C#如何将PDF文档转换为图片的问题,仍然没有一个简单的答案;许多库使用根据 AGPL 许可的 Ghostscript,在大多数情况下,生产使用需要昂贵的商业许可。
一个不错的选择可能是使用流行的“pdftoppm”实用程序,它具有GPL许可证;它可以从 C# 中用作与 System.Diagnostics.Process 一起执行的命令行工具。流行的工具在 Linux 世界中是众所周知的,但也可以使用Windows 版本。
如果您不想自己集成 pdftoppm,可以使用我的PdfRenderer 流行包装器(同时支持经典 .NET Framework 和.NET Core)——它不是免费的,但价格非常实惠。
在 dotnet core 中搜索适用于 Windows 和 Linux 的强大且免费的解决方案时,我访问了https://github.com/Dtronix/PDFiumCore和https://github.com/GowenGit/docnet。由于 PDFiumCore 使用更新版本的 Pdfium(这似乎是使用 pdf 库的关键点),我最终使用了它。
注意:如果您想在 Linux 上使用它,您应该按照/sf/answers/4147684761/的建议安装“libgdiplus”。
这是一个简单的单线程代码:
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);
Run Code Online (Sandbox Code Playgroud)
对于线程安全的实现,请参阅: https: //github.com/hmdhasani/DtronixPdf/blob/master/src/DtronixPdfBenchmark/Program.cs
您可以查看 Freeware.Pdf2Png MIT 许可证。只需在 nuget 中找到这些名称即可。
var dd = System.IO.File.ReadAllBytes("pdffile.pdf");
byte[] pngByte = Freeware.Pdf2Png.Convert(dd, 1);
System.IO.File.WriteAllBytes(Path.Combine(@"C:\temp", "dd.png"), pngByte );
Run Code Online (Sandbox Code Playgroud)
Google Chrome 中使用的 PDF 引擎称为PDFium,是在“BSD 3-clause”许可下开源的。我相信这允许在商业产品中使用时重新分发。
有一个名为PdfiumViewer ( NuGet )的 .NET 包装器,它在我尝试过的范围内运行良好。它在 Apache 许可下也允许重新分发。
(请注意,这与需要商业许可证的https://pdfium.patgames.com/不同的“包装器” *)
(还有另一种 PDFium .NET 包装器PDFiumSharp,但我尚未对其进行评估。)
在我看来,到目前为止,这可能是开源(如啤酒一样免费)PDF 库的最佳选择,它不会限制使用它们的软件的闭源/商业性质。据我所知,我认为这里的答案中的其他任何内容都不符合该标准。
关于PDFiumSharp:经过详细阐述,我能够从 PDF 解决方案创建 PNG 文件。
这是我的代码:
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);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于初学者,您需要执行以下步骤来启动并运行 PDFium 包装器:
在项目中引用PDFiumSharp和PDFiumsharp.GdiPlus程序集
确保在项目输出目录中找到 pdfium_x64.dll 和/或 pdfium_x86.dll。
NuGet包Pdf2Png是免费提供的,并且仅受MIT许可证保护,该许可证非常开放。
我已经进行了一些测试,这是将 PDF 文件转换为图像的代码(tt 确实将图像保存在调试文件夹中)。
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
91801 次 |
| 最近记录: |