Last active
June 20, 2022 07:19
-
-
Save aspose-com-gists/7507cd848ed75862c2d1f62fdce91c38 to your computer and use it in GitHub Desktop.
使用C#将PDF文件转换为字节数组或将字节数组转换为PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 加载输入文件 | |
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, resolution, 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 = | |
//new Aspose.Pdf.Devices.JpegDevice(Aspose.Pdf.PageSize.A4, res, 100); | |
new Aspose.Pdf.Devices.JpegDevice(width, height, res, 100); | |
// Convert a particular page and save the image to stream | |
//Aspose.Pdf.PageSize.A4.IsLandscape = true; | |
jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream); | |
// 关闭流 | |
imageStream.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment