PDF处理控件Aspose.PDF功能演示:使用C#添加,删除,提取和替换PDF中的图像

  • 使用C#在PDF中添加图像
  • 使用C#从PDF提取图像
  • 使用C#从PDF删除图像
  • 使用C#替换PDF中的图像

.NET API的

整合所有格式API处理控件Aspose.Total永久授权火热促销中,联系客服立马1分钟了解全部!


使用C#在PDF文件中添加图像

以下是使用Aspose.PDF for .NET将图像添加到PDF文件的步骤。

  • 使用Document类创建新的或加载现有的PDF文件。
  • 在Page对象中获取所需页面的引用。
  • 将图像添加到页面的资源集合。
  • 使用以下运算符将图像放置在页面上:
    • GSave运算符可保存当前的图形状态。
    • ConcatenateMatrix运算符,用于指定要放置图像的位置。
    • 做操作员在页面上绘制图像。
    • GRestore操作员保存更新的图形状态。
  • 使用Document.Save(String)方法保存更新的PDF文件。

下面的代码示例演示如何使用C#将图像添加到PDF文件。

// Open documentDocument pdfDocument = new Document("AddImage.pdf");// Set coordinatesint lowerLeftX = 100;int lowerLeftY = 100;int upperRightX = 200;int upperRightY = 200;// Get the page where image needs to be addedPage page = pdfDocument.Pages[1];// Load image into streamFileStream imageStream = new FileStream("aspose-logo.jpg", FileMode.Open);// Add image to Images collection of Page Resourcespage.Resources.Images.Add(imageStream);// Using GSave operator: this operator saves current graphics statepage.Contents.Add(new Aspose.Pdf.Operators.GSave());// Create Rectangle and Matrix objectsAspose.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 placedpage.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));XImage ximage = page.Resources.Images[page.Resources.Images.Count];// Using Do operator: this operator draws imagepage.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));// Using GRestore operator: this operator restores graphics statepage.Contents.Add(new Aspose.Pdf.Operators.GRestore());// Save updated documentpdfDocument.Save("AddImage_out.pdf");

使用C#从PDF提取图像

如果要从PDF文件中提取所有图像,可以按照以下步骤操作。

  • 使用Document类加载现有的PDF文件。
  • 使用索引从特定页面的Resources集合中获得XImage对象中所需的图像。
  • 使用XImage.Save(FileStream,ImageFormat)方法将提取的图像保存为所需的格式。

下面的代码示例演示如何使用C#从PDF提取图像。

// Open documentDocument pdfDocument = new Document("ExtractImages.pdf");// Extract a particular imageXImage xImage = pdfDocument.Pages[1].Resources.Images[1];FileStream outputImage = new FileStream("output.jpg", FileMode.Create);// Save output imagexImage.Save(outputImage, ImageFormat.Jpeg);outputImage.Close();

使用C#从PDF删除图像

一旦可以访问PDF页面的资源,就可以从其中删除图像。以下是使用C#从PDF文件中删除图像的步骤。

  • Delete() –删除所有图像。
  • Delete(Int32) –按索引删除图像。
    • Delete(String) –按名称删除图像。
  • 使用Document.Save(String)方法保存更新的PDF文件。

以下代码示例显示了如何使用C#从PDF中删除图像。

// Open documentDocument pdfDocument = new Document("DeleteImages.pdf");// Delete a particular imagepdfDocument.Pages[1].Resources.Images.Delete(1);// Save updated PDF filepdfDocument.Save("output.pdf");

使用C#替换PDF中的图像

.NET的Aspose.PDF还可让您替换PDF中的特定图像。为此,您可以替换页面图像集中的图像。以下是使用C#替换PDF中的图像的步骤。

  • 使用Document类加载PDF文件。
  • 使用Document.Pages [1] .Resources.Images.Replace(Int32,Stream,Int32,Boolean)方法替换所需的图像。
  • 使用Document.Save(String)方法保存更新的PDF文件。

下面的代码示例演示如何使用C#替换PDF中的图像。

// Open documentDocument pdfDocument = new Document("input.pdf");// Replace a particular imagepdfDocument.Pages[1].Resources.Images.Replace(1, new FileStream("lovely.jpg", FileMode.Open));// Save updated PDF filepdfDocument.Save("output.pdf");


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月24日
下一篇 2021年1月24日

相关推荐

发表回复

登录后才能评论