Last active
January 25, 2021 17:17
-
-
Save aspose-com-gists/73276ad00873beae0d7abe8c587405de to your computer and use it in GitHub Desktop.
Work with Images in PDF using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
// 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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment