将PDF文件拆分成页面(最好是C#)的步骤如下:
System.Drawing
namespaceusing System.Drawing;using System.IO;
using Pdfium.PdfiumCore;var filePath = @"C:\example.pdf";
var fileStream = File.OpenRead(filePath);
var byteArray = new byte[fileStream.Length];
fileStream.Read(byteArray, 0, byteArray.Length);
fileStream.Close();var pdfiumPDFium = PdfiumPDFium.PdfiumPDFiumDriver(new PdfiumCore.PdfReader(byteArray));int numPages = pdfiumPDFium.CountPages();int[] pageNumbers = new int[numPages];
int index = 0;
for (int i = 1; i <= numPages; i++)
{
pageNumbers[index++] = i;
}foreach (var num in pageNumbers)
{
// Read the current page as a graphics stream,
// convert it to a bitmap, and get its size.
byte[] pageBytes = pdfiumPDFium.RenderPageToStream(num);
// Resize the page to the desired output size.
// Note that we've limited the image size for simplicity,
// and it's possible to render a larger image directly.
Bitmap pageBmp = new Bitmap(612, 496);
using (MemoryStream bmpStream = new MemoryStream())
{
pageBmp.Save(bmpStream, ImageFormat.Jpeg);
// Continue processing if the page image is successfully drawn.
if (bmpStream.Length > 0)
{
// Save the image data to its local file. You can use any file format you prefer.
var outputFilePath = Path.Combine(Path.GetTempPath(), "page" + num + ".jpg");
bmpStream.Position = 0;
bmpStream.CopyTo(outputFilePath, byteArray.Length, byteArray, 0, byteArray.Length);
}
}
pageBmp.Dispose();
}foreach (var num in pageNumbers)
{
// Check for errors while rendering the current page.
if (pdfiumPDFium.ErrorFound)
{
// Print error message.
Console.WriteLine($"Error while rendering page {num}: {pdfiumPDFium.GetPdfError().Message}");
// Try the next page.
continue;
}
}pdfiumPDFium.Dispose();CountPages
方法计算 pdf 文件中的页数以下是根据给定问答内容的腾讯云相关产品推荐,具体链接地址为官网:
以上是 PDF 拆分成页面的答案,希望能对您的开发项目有所帮助。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云