翻譯|使用教程|編輯:李顯亮|2021-01-27 10:38:01.557|閱讀 300 次
概述:于PDF已成為最流行和廣泛使用的文件格式之一,因此本文著眼于如何以編程方式處理PDF文件中的圖像。更精確地講,本文將學習如何使用C#從PDF文件中添加,提取,刪除和替換圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
一張圖片勝過千言萬語。因此,圖像和圖形在PDF和其他文檔中起著重要的作用。由于PDF已成為最流行和廣泛使用的文件格式之一,因此本文著眼于如何以編程方式處理PDF文件中的圖像。更精確地講,本文將學習如何使用C#從PDF文件中添加,提取,刪除和替換圖像。
.NET API的Aspose.PDF可從.NET應用程序中創建和處理PDF文檔。使用API,可以輕松執行基本以及高級的PDF自動化功能。此外,可以處理現有PDF文件中的圖像。 您可以從“新發行版”部分下載最新的DLL文件,
使用C#在PDF文件中添加圖像
以下是使用Aspose.PDF for .NET將圖像添加到PDF文件的步驟。
下面的代碼示例演示如何使用C#將圖像添加到PDF文件。
// Open document Document pdfDocument = new Document("AddImage.pdf"); // Set coordinates int lowerLeftX = 100; int lowerLeftY = 100; int upperRightX = 200; int upperRightY = 200; // Get the page where image needs to be added Page page = pdfDocument.Pages[1]; // Load image into stream FileStream imageStream = new FileStream("aspose-logo.jpg", FileMode.Open); // Add image to Images collection of Page Resources page.Resources.Images.Add(imageStream); // Using GSave operator: this operator saves current graphics state page.Contents.Add(new Aspose.Pdf.Operators.GSave()); // Create Rectangle and Matrix objects Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY); Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY }); // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix)); XImage ximage = page.Resources.Images[page.Resources.Images.Count]; // Using Do operator: this operator draws image page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name)); // Using GRestore operator: this operator restores graphics state page.Contents.Add(new Aspose.Pdf.Operators.GRestore()); // Save updated document pdfDocument.Save("AddImage_out.pdf");
使用C#從PDF提取圖像
如果要從PDF文件中提取所有圖像,可以按照以下步驟操作。
下面的代碼示例演示如何使用C#從PDF提取圖像。
// Open document Document pdfDocument = new Document("ExtractImages.pdf"); // Extract a particular image XImage xImage = pdfDocument.Pages[1].Resources.Images[1]; FileStream outputImage = new FileStream("output.jpg", FileMode.Create); // Save output image xImage.Save(outputImage, ImageFormat.Jpeg); outputImage.Close();
使用C#從PDF刪除圖像
一旦可以訪問PDF頁面的資源,就可以從其中刪除圖像。以下是使用C#從PDF文件中刪除圖像的步驟。
以下代碼示例顯示了如何使用C#從PDF中刪除圖像。
// Open document Document pdfDocument = new Document("DeleteImages.pdf"); // Delete a particular image pdfDocument.Pages[1].Resources.Images.Delete(1); // Save updated PDF file pdfDocument.Save("output.pdf");
使用C#替換PDF中的圖像
.NET的Aspose.PDF還可讓您替換PDF中的特定圖像。為此,您可以替換頁面圖像集中的圖像。以下是使用C#替換PDF中的圖像的步驟。
下面的代碼示例演示如何使用C#替換PDF中的圖像。
// Open document Document pdfDocument = new Document("input.pdf"); // Replace a particular image pdfDocument.Pages[1].Resources.Images.Replace(1, new FileStream("lovely.jpg", FileMode.Open)); // Save updated PDF file pdfDocument.Save("output.pdf");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn