Skip to content

Commit 9e5aa8b

Browse files
authored
Merge pull request #296 from aspose-pdf/PDFNET_57975_Working_with_images_and_graphs_drawing
Working with images and graphs (PDFNET-57975)
2 parents daed967 + c676e1e commit 9e5aa8b

File tree

15 files changed

+156
-494
lines changed

15 files changed

+156
-494
lines changed

net/advanced-operations/attachments/add-attachment-to-pdf-document/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ aliases:
8080

8181
Attachments can contain a wide variety of information and can be of a variety of file types. This article explains how to add an attachment to a PDF file.
8282

83-
The next code snippet also works with a new graphical [Aspose.Drawing](/pdf/net/drawing/) interface.
83+
The next code snippet also works with [Aspose.Drawing](/pdf/net/drawing/) library.
8484

8585
1. Create a new C# project.
8686
1. Add a reference to Aspose.PDF DLL.

net/advanced-operations/working-with-documents/working-with-headings/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ The **Style** property of **Aspose.PDF.Heading** class is used to set the number
9393
| :- |
9494
The source code, to obtain the output shown in the above figure, is given below in the example.
9595

96-
The next code snippet also works with a new graphical [Aspose.Drawing](/pdf/net/drawing/) interface.
96+
The next code snippet also works with [Aspose.Drawing](/pdf/net/drawing/) library.
9797

9898
```csharp
9999
// The path to the documents directory.

net/advanced-operations/working-with-images/add-image-to-existing-pdf-file/_index.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,37 @@ mender.AddImage(imageFileName, 1, 0, 0, (float)page.CropBox.Width, (float)page.C
154154
document.Save(outputPdfFileName);
155155
```
156156

157+
Sometimes, it is necessary to crop an image before inserting it into a PDF. Use can use `AddImage()` method to support adding cropped images:
158+
159+
```csharp
160+
var imageFileName = Path.Combine(_dataDir, "Images", "Sample-01.jpg");
161+
var outputPdfFileName = Path.Combine(_dataDir, "Example-add-image-mender.pdf");;
162+
163+
using (Document document = new Document())
164+
{
165+
using (Stream imgStream = File.OpenRead(imageFileName))
166+
{
167+
// Define the rectangle where the image will be placed on the PDF page
168+
Rectangle imageRect = new Rectangle(17.62, 65.25, 602.62, 767.25);
169+
170+
// Crop the image to half its original width and height
171+
var w = imageRect.Width / 2;
172+
var h = imageRect.Height / 2;
173+
Rectangle bbox = new Rectangle(imageRect.LLX, imageRect.LLY, imageRect.LLX + w, imageRect.LLY + h);
174+
175+
// Add a new page to the document
176+
Page page = document.Pages.Add();
177+
178+
// Insert the cropped image onto the page, specifying the original position (imageRect)
179+
// and the cropping area (bbox)
180+
page.AddImage(imgStream, imageRect, bbox);
181+
}
182+
183+
// Save the document to the specified file path
184+
document.Save(outputPdfFileName);
185+
}
186+
```
187+
157188
## Place image on page and preserve (control) aspect ratio
158189

159190
If we do not know the dimensions of the image there is every chance of getting a distorted image on the page. The following example shows one of the ways to avoid this.
@@ -195,7 +226,7 @@ string dataDir = RunExamples.GetDataDir_AsposePdf_Images();
195226
// Counter for grayscale images
196227
int grayscaled = 0;
197228
// Counter for RGB images
198-
int rgd = 0;
229+
int rgb = 0;
199230

200231
using (Document document = new Document(dataDir + "ExtractImages.pdf"))
201232
{
@@ -218,7 +249,7 @@ using (Document document = new Document(dataDir + "ExtractImages.pdf"))
218249
Console.WriteLine("Image {0} is GrayScale...", image_counter);
219250
break;
220251
case ColorType.Rgb:
221-
++rgd;
252+
++rgb;
222253
Console.WriteLine("Image {0} is RGB...", image_counter);
223254
break;
224255
}

0 commit comments

Comments
 (0)