文檔半島外圍網上直營>>Aspose.PDF使用教程>>Aspose.PDF功能演示:使用C#實現PDF文件和字節數組的相互轉換
Aspose.PDF功能演示:使用C#實現PDF文件和字節數組的相互轉換
字節數組有助于存儲或傳輸數據。同樣,PDF文件格式因其功能和兼容性而廣受歡迎。可以使用C#語言將PDF文件轉換為字節數組,也可以將字節數組轉換為PDF文件。這可以幫助更有效地在數據庫中存儲和歸檔PDF文件,還可以通過使用字節數組來序列化數據。讓我們探討這些格式的互轉換性。
- 使用C#將PDF文件轉換為字節數組
- 使用C#將字節數組轉換為PDF文件
使用C#將PDF文件轉換為字節數組
可以將PDF轉換為字節數組,以便傳輸或存儲它以進行進一步處理。例如,您可能需要序列化PDF文檔,然后將其轉換為字節數組會有所幫助。您需要按照以下步驟將PDF轉換為字節數組:
- 加載輸入PDF文件
- 初始化字節數組
- 初始化FileStream對象
- 將文件內容加載到字節數組中
以下代碼顯示了如何使用C#將PDF文件轉換為字節數組,其中將所得的ByteArray傳遞給將輸入文件轉換為圖像的方法:
dataDir = @"D:\Test\"; // Load input PDF file string inputFile = dataDir + @"testpdf.pdf"; // Initialize a byte array byte[] buff = null; // Initialize FileStream object FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); long numBytes = new FileInfo(inputFile).Length; // Load the file contents in the byte array buff = br.ReadBytes((int) numBytes); fs.Close(); // Work with the PDF file in byte array ConvertPDFToJPEG(buff, 300, dataDir); public static void ConvertPDFToJPEG(Byte[] PDFBlob, int resolution, string dataDir) { // Open document 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)) { // Create JPEG device with specified attributes // Width, Height, Resolution, Quality // Quality [0-100], 100 is Maximum // Create Resolution object Aspose.Pdf.Devices.Resolution res = new Aspose.Pdf.Devices.Resolution(resolution); // JpegDevice jpegDevice = new JpegDevice(500, 700, resolution, 100); // added the following to determine if landscape or not 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); // Close stream imageStream.Close(); } } } }
使用C#將字節數組轉換為PDF文件
讓我們進一步進行下一步,可以將字節數組轉換為PDF文件。讓我們通過將圖像作為字節數組轉換為PDF文件的示例來學習這一點。您需要按照以下步驟將字節數組轉換為PDF文件。
- 加載輸入文件
- 初始化字節數組
- 將輸入圖像加載到字節數組中
- 初始化Document類的實例
- 在PDF頁面上添加圖像
- 保存輸出PDF文件
以下代碼說明了如何使用C#以編程方式將字節數組轉換為PDF文件:
// Load input file string inputFile = dataDir + @"Test.PNG"; // Initialize byte array byte[] buff = null; FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); long numBytes = new FileInfo(inputFile).Length; // Load input image into Byte Array buff = br.ReadBytes((int)numBytes); Document doc = new Document(); // Add a page to pages collection of document Page page = doc.Pages.Add(); // Load the source image file to Stream object MemoryStream outstream = new MemoryStream(); MemoryStream mystream = new MemoryStream(buff); // Instantiate BitMap object with loaded image stream Bitmap b = new Bitmap(mystream); // Set margins so image will fit, etc. 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); // Create an image object Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(); // Add the image into paragraphs collection of the section page.Paragraphs.Add(image1); // Set the image file stream image1.ImageStream = mystream; // Save resultant PDF file doc.Save(outstream, SaveFormat.Pdf); //doc.Save(dataDir + "outstream.pdf", SaveFormat.Pdf); // Close memoryStream object mystream.Close();
還想要更多嗎?您可以點擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(761297826),我們很高興為您提供查詢和咨詢。