字节数组 有助于存储或传输数据。同样,PDF 文件格式因其功能和兼容性而广受欢迎。您可以使用 C# 语言将 PDF 文件转换为字节数组以及将字节数组转换为 PDF 文件。这可以帮助您更有效地在数据库中存储和归档 PDF 文件。您还可以通过使用字节数组来序列化数据。让我们探索这些格式的相互转换。
PDF文件到字节数组和字节数组到PDF文件的转换——API安装
Aspose.PDF for .NET API 提供了许多处理 PDF 文档的功能。您可以通过简单易用的 API 调用来创建、编辑、操作或转换 PDF 文件。要将 PDF 文件转换为字节数组或反之亦然,您需要通过从 官方网站 或 NuGet 库下载 API 来安装 API,使用 Visual Studio IDE 中的以下安装命令。
PM> Install-Package Aspose.Pdf
使用 C# 将 PDF 文件转换为字节数组
您可以将 PDF 转换为字节数组,以便传输或存储它以供进一步处理。例如,您可能需要序列化 PDF 文档,然后将其转换为字节数组会有所帮助。您需要按照以下步骤将 PDF 转换为字节数组:
- 加载输入 PDF 文件
- 初始化字节数组
- 初始化 FileStream 对象
- 加载字节数组中的文件内容
完成所有这些步骤后,现在您可以以字节数组的形式处理 PDF 文件。例如,您可以将其传递给另一个函数,如下例所示。
以下代码显示了如何使用 C# 将 PDF 文件转换为字节数组,其中生成的 ByteArray 被传递给用于将输入文件转换为图像的方法:
dataDir = @"D:\Test\";
// 加载输入 PDF 文件
string inputFile = dataDir + @"testpdf.pdf";
// 初始化一个字节数组
byte[] buff = null;
// 初始化 FileStream 对象
FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(inputFile).Length;
// 加载字节数组中的文件内容
buff = br.ReadBytes((int) numBytes);
fs.Close();
// 使用字节数组中的 PDF 文件
ConvertPDFToJPEG(buff, 300, dataDir);
public static void ConvertPDFToJPEG(Byte[] PDFBlob, int resolution, string dataDir)
{
// 打开文档
using (MemoryStream InputStream = new MemoryStream(PDFBlob))
{
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(InputStream);
for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
using (FileStream imageStream = new FileStream(dataDir + "image" + pageCount + "_out" + ".jpg", FileMode.Create))
{
// 创建具有指定属性的 JPEG 设备
// 宽度、高度、分辨率、质量
// 质量 [0-100],100 为最大值
// 创建分辨率对象
Aspose.Pdf.Devices.Resolution res = new Aspose.Pdf.Devices.Resolution(resolution);
// JpegDevice jpegDevice = new JpegDevice(500, 700, 分辨率, 100);
// 添加以下内容以确定是否为横向
Int32 height, width = 0;
PdfFileInfo info = new PdfFileInfo(pdfDocument);
width = Convert.ToInt32(info.GetPageWidth(pdfDocument.Pages[pageCount].Number));
height = Convert.ToInt32(info.GetPageHeight(pdfDocument.Pages[pageCount].Number));
Aspose.Pdf.Devices.JpegDevice jpegDevice =
//新 Aspose.Pdf.Devices.JpegDevice(Aspose.Pdf.PageSize.A4, res, 100);
new Aspose.Pdf.Devices.JpegDevice(width, height, res, 100);
// 转换特定页面并将图像保存到流
//Aspose.Pdf.PageSize.A4.IsLandscape = true;
jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream);
// 关闭流
imageStream.Close();
}
}
}
}
使用 C# 将字节数组转换为 PDF 文件
让我们更进一步,可以将字节数组转换为 PDF 文件。让我们通过将图像作为字节数组转换为 PDF 文件的示例来了解这一点。您需要按照以下步骤将字节数组转换为 PDF 文件。
- 加载输入文件
- 初始化字节数组
- 将输入图像加载到字节数组中
- 初始化 Document 类的一个实例
- 在 PDF 页面上添加图像
- 保存输出 PDF 文件
以下代码说明了如何使用 C# 以编程方式将字节数组转换为 PDF 文件:
// 加载输入文件
string inputFile = dataDir + @"Test.PNG";
// 初始化字节数组
byte[] buff = null;
FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(inputFile).Length;
// 将输入图像加载到字节数组中
buff = br.ReadBytes((int)numBytes);
Document doc = new Document();
// 将页面添加到文档的页面集合
Page page = doc.Pages.Add();
// 将源图像文件加载到 Stream 对象
MemoryStream outstream = new MemoryStream();
MemoryStream mystream = new MemoryStream(buff);
// 使用加载的图像流实例化 BitMap 对象
Bitmap b = new Bitmap(mystream);
// 设置边距,以便图像适合等。
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);
// 创建图像对象
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
// 将图像添加到该部分的段落集合中
page.Paragraphs.Add(image1);
// 设置图片文件流
image1.ImageStream = mystream;
// 保存生成的 PDF 文件
doc.Save(outstream, SaveFormat.Pdf);
//doc.Save(dataDir + "outstream.pdf", SaveFormat.Pdf);
// 关闭 memoryStream 对象
mystream.Close();
结论
在本文中,我们探讨了如何使用 C# 编程语言将 PDF 文件转换为字节数组以及字节数组到 PDF 文件。如果您有兴趣进一步处理 PDF 文件,请通过 免费支持论坛 与我们分享您的要求。此外,您还可以浏览 API 文档 和 API 参考 以深入分析 API 提供的功能。我们期待与您取得联系!