翻譯|使用教程|編輯:李顯亮|2020-12-01 09:50:32.080|閱讀 466 次
概述:字節數組有助于存儲或傳輸數據。同樣,PDF文件格式因其功能和兼容性而廣受歡迎??梢允褂肅#語言將PDF文件轉換為字節數組,也可以將字節數組轉換為PDF文件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
字節數組有助于存儲或傳輸數據。同樣,PDF文件格式因其功能和兼容性而廣受歡迎。可以使用C#語言將PDF文件轉換為字節數組,也可以將字節數組轉換為PDF文件。這可以幫助更有效地在數據庫中存儲和歸檔PDF文件,還可以通過使用字節數組來序列化數據。讓我們探討這些格式的互轉換性。
(安裝包僅提供部分功能,并設置限制,如需試用完整功能請。)
使用C#將PDF文件轉換為字節數組
可以將PDF轉換為字節數組,以便傳輸或存儲它以進行進一步處理。例如,您可能需要序列化PDF文檔,然后將其轉換為字節數組會有所幫助。您需要按照以下步驟將PDF轉換為字節數組:
以下代碼顯示了如何使用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文件。
以下代碼說明了如何使用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();
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn