Aspose.PDF功能演示:使用C#添加,删除,提取和替换PDF中的图像
一张图片胜过千言万语。因此,图像和图形在PDF和其他文档中起着重要的作用。由于PDF已成为最流行和广泛使用的文件格式之一,因此本文着眼于如何以编程方式处理PDF文件中的图像。更精确地讲,本文将学习如何使用C#从PDF文件中添加,提取,删除和替换图像。
- 使用C#在PDF中添加图像
- 使用C#从PDF提取图像
- 使用C#从PDF删除图像
- 使用C#替换PDF中的图像
.NET API的Aspose.PDF可从.NET应用程序中创建和处理PDF文档。使用API,可以轻松执行基本以及高级的PDF自动化功能。此外,可以处理现有PDF文件中的图像。 您可以从“新发行版”部分下载最新的DLL文件,
使用C#在PDF文件中添加图像
以下是使用Aspose.PDF for .NET将图像添加到PDF文件的步骤。
- 使用Document类创建新的或加载现有的PDF文件。
- 在Page对象中获取所需页面的引用。
- 将图像添加到页面的资源集合。
-
使用以下运算符将图像放置在页面上:
- GSave运算符可保存当前的图形状态。
- ConcatenateMatrix运算符,用于指定要放置图像的位置。
- 做操作员在页面上绘制图像。
- GRestore操作员保存更新的图形状态。
- 使用Document.Save(String)方法保存更新的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文件中提取所有图像,可以按照以下步骤操作。
- 使用Document类加载现有的PDF文件。
- 使用索引从特定页面的Resources集合中获得XImage对象中所需的图像。
- 使用XImage.Save(FileStream,ImageFormat)方法将提取的图像保存为所需的格式。
下面的代码示例演示如何使用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文件中删除图像的步骤。
- Delete() –删除所有图像。
-
Delete(Int32) –按索引删除图像。
- Delete(String) –按名称删除图像。
- 使用Document.Save(String)方法保存更新的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中的图像的步骤。
- 使用Document类加载PDF文件。
- 使用Document.Pages [1] .Resources.Images.Replace(Int32,Stream,Int32,Boolean)方法替换所需的图像。
- 使用Document.Save(String)方法保存更新的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");
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线