Skip to content

Commit c694ff7

Browse files
authored
Merge pull request #298 from aspose-pdf/examples/3c85dbb5-working-with-images
Add 70 new working-with-images code examples
2 parents 547466f + 00c552a commit c694ff7

72 files changed

Lines changed: 5913 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf; // Core API
4+
using Aspose.Pdf.Facades; // For any facade usage if needed (not used here)
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "output_with_background.pdf";
12+
const string imagePath = "background.png"; // Path to the background image
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine($"Input PDF not found: {inputPath}");
17+
return;
18+
}
19+
20+
if (!File.Exists(imagePath))
21+
{
22+
Console.Error.WriteLine($"Background image not found: {imagePath}");
23+
return;
24+
}
25+
26+
// Load the existing PDF document (using the lifecycle rule for disposal)
27+
using (Document doc = new Document(inputPath))
28+
{
29+
// Iterate pages using 1‑based indexing (global rule)
30+
for (int i = 1; i <= doc.Pages.Count; i++)
31+
{
32+
Page page = doc.Pages[i];
33+
34+
// Create a background artifact, set the image and opacity (30 %)
35+
BackgroundArtifact bgArtifact = new BackgroundArtifact();
36+
bgArtifact.SetImage(imagePath); // Load image from file
37+
bgArtifact.Opacity = 0.3; // 30 % opacity (range 0..1)
38+
bgArtifact.IsBackground = true; // Ensure it is placed behind page content
39+
40+
// Add the artifact to the page
41+
page.Artifacts.Add(bgArtifact);
42+
}
43+
44+
// Save the modified PDF (using the lifecycle rule)
45+
doc.Save(outputPath);
46+
}
47+
48+
Console.WriteLine($"PDF saved with background image to '{outputPath}'.");
49+
}
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades; // for potential future extensions
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPdf = "input.pdf";
11+
const string outputPdf = "output_with_background.pdf";
12+
const string bgImagePath = "background.png";
13+
14+
if (!File.Exists(inputPdf))
15+
{
16+
Console.Error.WriteLine($"Input PDF not found: {inputPdf}");
17+
return;
18+
}
19+
if (!File.Exists(bgImagePath))
20+
{
21+
Console.Error.WriteLine($"Background image not found: {bgImagePath}");
22+
return;
23+
}
24+
25+
// Load the existing PDF document
26+
using (Document doc = new Document(inputPdf))
27+
{
28+
// Iterate over all pages to add the background image
29+
foreach (Page page in doc.Pages)
30+
{
31+
// Create a BackgroundArtifact – this places content behind page elements
32+
BackgroundArtifact bgArtifact = new BackgroundArtifact
33+
{
34+
IsBackground = true, // ensure it is rendered behind page content
35+
Opacity = 0.5f // subtle shading; blend mode "multiply" is not directly exposed
36+
};
37+
38+
// Load the image into the artifact
39+
bgArtifact.SetImage(bgImagePath);
40+
41+
// Optionally position the artifact (full‑page coverage)
42+
// If Position is not set, the artifact will fill the page automatically.
43+
// For explicit positioning you could use:
44+
// bgArtifact.Position = new Aspose.Pdf.Rectangle(0, 0, page.PageInfo.Width, page.PageInfo.Height);
45+
46+
// Add the artifact to the page
47+
page.Artifacts.Add(bgArtifact);
48+
}
49+
50+
// Save the modified PDF
51+
doc.Save(outputPdf);
52+
}
53+
54+
Console.WriteLine($"PDF saved with background image: {outputPdf}");
55+
}
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPdf = "input.pdf"; // source PDF
10+
const string outputPdf = "output_with_bg.pdf"; // result PDF
11+
const string patternImg = "pattern.png"; // background pattern image
12+
13+
if (!File.Exists(inputPdf))
14+
{
15+
Console.Error.WriteLine($"Input file not found: {inputPdf}");
16+
return;
17+
}
18+
19+
if (!File.Exists(patternImg))
20+
{
21+
Console.Error.WriteLine($"Pattern image not found: {patternImg}");
22+
return;
23+
}
24+
25+
// Load the PDF document
26+
using (Document doc = new Document(inputPdf))
27+
{
28+
// Iterate over all pages (1‑based indexing)
29+
foreach (Page page in doc.Pages)
30+
{
31+
// Create a background artifact
32+
BackgroundArtifact bgArtifact = new BackgroundArtifact();
33+
34+
// Set the image for the artifact (path to the pattern file)
35+
bgArtifact.SetImage(patternImg);
36+
37+
// Set opacity to 10 percent (range 0..1)
38+
bgArtifact.Opacity = 0.1;
39+
40+
// Ensure the artifact is placed behind page contents
41+
bgArtifact.IsBackground = true;
42+
43+
// Add the artifact to the page
44+
page.Artifacts.Add(bgArtifact);
45+
}
46+
47+
// Save the modified PDF
48+
doc.Save(outputPdf);
49+
}
50+
51+
Console.WriteLine($"PDF saved with background pattern: {outputPdf}");
52+
}
53+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPath = "input.pdf";
10+
const string outputPath = "output.pdf";
11+
const string patternPath = "pattern.png";
12+
13+
if (!File.Exists(inputPath) || !File.Exists(patternPath))
14+
{
15+
Console.Error.WriteLine("Input PDF or pattern image not found.");
16+
return;
17+
}
18+
19+
// Load the PDF document (using rule: document-disposal-with-using)
20+
using (Document doc = new Document(inputPath))
21+
{
22+
// Pages are 1‑based (rule: page-indexing-one-based)
23+
for (int i = 1; i <= doc.Pages.Count; i++)
24+
{
25+
Page page = doc.Pages[i];
26+
27+
// Create a background artifact, set the image and opacity
28+
BackgroundArtifact bg = new BackgroundArtifact();
29+
bg.SetImage(patternPath); // load image from file
30+
bg.Opacity = 0.05; // 5 percent opacity
31+
bg.IsBackground = true; // place behind page contents
32+
33+
// Add the artifact to the page
34+
page.Artifacts.Add(bg);
35+
}
36+
37+
// Save the modified PDF (rule: document-disposal-with-using)
38+
doc.Save(outputPath);
39+
}
40+
41+
Console.WriteLine($"Background pattern applied and saved to '{outputPath}'.");
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf; // Core API for Document, Page, ImageStamp
4+
using Aspose.Pdf.Facades; // For ImageStamp (if needed, otherwise core namespace suffices)
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPdf = "input.pdf"; // source PDF
11+
const string outputPdf = "output.pdf"; // result PDF
12+
const string logoPath = "logo.png"; // company logo image
13+
14+
// Verify files exist
15+
if (!File.Exists(inputPdf))
16+
{
17+
Console.Error.WriteLine($"Input PDF not found: {inputPdf}");
18+
return;
19+
}
20+
if (!File.Exists(logoPath))
21+
{
22+
Console.Error.WriteLine($"Logo image not found: {logoPath}");
23+
return;
24+
}
25+
26+
// Load the PDF document (lifecycle rule: use using for deterministic disposal)
27+
using (Document pdfDoc = new Document(inputPdf))
28+
{
29+
// Create an ImageStamp for the logo
30+
ImageStamp logoStamp = new ImageStamp(logoPath)
31+
{
32+
// Center the stamp horizontally and vertically on the page
33+
HorizontalAlignment = HorizontalAlignment.Center,
34+
VerticalAlignment = VerticalAlignment.Center,
35+
36+
// Optional visual settings
37+
Background = false, // place over page content
38+
Opacity = 1.0f // fully opaque
39+
};
40+
41+
// Add the stamp only to the first page (pages are 1‑based)
42+
pdfDoc.Pages[1].AddStamp(logoStamp);
43+
44+
// Save the modified PDF (lifecycle rule: save inside using block)
45+
pdfDoc.Save(outputPdf);
46+
}
47+
48+
Console.WriteLine($"Logo added to first page and saved as '{outputPdf}'.");
49+
}
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPath = "input.pdf"; // source PDF
10+
const string outputPath = "output_footer.pdf"; // result PDF
11+
const string footerImgPath = "footer.png"; // decorative footer image
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine($"Input file not found: {inputPath}");
16+
return;
17+
}
18+
19+
if (!File.Exists(footerImgPath))
20+
{
21+
Console.Error.WriteLine($"Footer image not found: {footerImgPath}");
22+
return;
23+
}
24+
25+
// Load the PDF document (using rule: create/load with using)
26+
using (Document doc = new Document(inputPath))
27+
{
28+
// Iterate through all pages (1‑based indexing)
29+
foreach (Page page in doc.Pages)
30+
{
31+
// Ensure a Footer object exists for the page
32+
if (page.Footer == null)
33+
page.Footer = new HeaderFooter();
34+
35+
// Create an Image object for the footer
36+
Image footerImage = new Image
37+
{
38+
File = footerImgPath,
39+
// Scale the image proportionally to 80% of the page width
40+
FixWidth = page.PageInfo.Width * 0.8,
41+
FixHeight = 0, // 0 preserves aspect ratio
42+
HorizontalAlignment = HorizontalAlignment.Center
43+
};
44+
45+
// Add the image to the footer's paragraph collection
46+
page.Footer.Paragraphs.Add(footerImage);
47+
}
48+
49+
// Save the modified PDF (using rule: save within using block)
50+
doc.Save(outputPath);
51+
}
52+
53+
Console.WriteLine($"Footer image added to each page. Saved as '{outputPath}'.");
54+
}
55+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf; // Core PDF API
4+
using Aspose.Pdf.Facades; // For ImageStamp (inherits from Stamp)
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf"; // source PDF
11+
const string watermarkPath = "watermark.png"; // image to use as watermark
12+
const string outputPath = "watermarked.pdf"; // result PDF
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine($"Input file not found: {inputPath}");
17+
return;
18+
}
19+
20+
if (!File.Exists(watermarkPath))
21+
{
22+
Console.Error.WriteLine($"Watermark image not found: {watermarkPath}");
23+
return;
24+
}
25+
26+
// Load the PDF document (create/load rule)
27+
using (Document doc = new Document(inputPath))
28+
{
29+
// Create an image stamp with the watermark image
30+
ImageStamp imgStamp = new ImageStamp(watermarkPath);
31+
32+
// Set arbitrary rotation angle (45 degrees) for diagonal placement
33+
imgStamp.RotateAngle = 45; // Stamp.RotateAngle property
34+
35+
// Optional: make the watermark semi‑transparent and place it over content
36+
imgStamp.Background = false; // false = overlay (on top of page content)
37+
imgStamp.Opacity = 0.5; // 0..1 range
38+
39+
// Apply the stamp to every page in the document
40+
foreach (Page page in doc.Pages)
41+
{
42+
page.AddStamp(imgStamp); // Page.AddStamp method
43+
}
44+
45+
// Save the modified PDF (save rule)
46+
doc.Save(outputPath);
47+
}
48+
49+
Console.WriteLine($"Watermarked PDF saved to '{outputPath}'.");
50+
}
51+
}

0 commit comments

Comments
 (0)