Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions working-with-images/add-background-image-multiply-blend-mode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Drawing; // for BlendMode enum (if available)

class Program
{
static void Main()
{
const string inputPdf = "input.pdf";
const string outputPdf = "output.pdf";
const string imagePath = "background.png";

if (!File.Exists(inputPdf))
{
Console.Error.WriteLine($"Input PDF not found: {inputPdf}");
return;
}

if (!File.Exists(imagePath))
{
Console.Error.WriteLine($"Background image not found: {imagePath}");
return;
}

// Load the existing PDF document
using (Document doc = new Document(inputPdf))
{
// Create a background artifact for the first page
BackgroundArtifact bgArtifact = new BackgroundArtifact();

// Set the image for the background artifact
bgArtifact.SetImage(imagePath);

// Mark the artifact as a background (placed behind page contents)
bgArtifact.IsBackground = true;

// OPTIONAL: Set blend mode to Multiply for subtle shading.
// The BlendMode property exists on Artifact (or its derived types) in newer versions.
// If the property is not available in your version, you may need to adjust opacity instead.
// Uncomment the following line if BlendMode is supported:
// bgArtifact.BlendMode = BlendMode.Multiply;

// Add the artifact to the page's artifact collection
// (Page indices are 1‑based in Aspose.Pdf)
doc.Pages[1].Artifacts.Add(bgArtifact);

// Save the modified PDF
doc.Save(outputPdf);
}

Console.WriteLine($"Background image added and saved to '{outputPdf}'.");
}
}
48 changes: 48 additions & 0 deletions working-with-images/add-background-image-to-pdf-pages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.IO;
using Aspose.Pdf; // Core Aspose.Pdf namespace

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output_with_background.pdf";
const string imagePath = "background.png"; // Path to the background image

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"Input PDF not found: {inputPath}");
return;
}

if (!File.Exists(imagePath))
{
Console.Error.WriteLine($"Background image not found: {imagePath}");
return;
}

// Load the existing PDF document
using (Document doc = new Document(inputPath))
{
// Iterate over all pages (Aspose.Pdf uses 1‑based indexing)
for (int i = 1; i <= doc.Pages.Count; i++)
{
Page page = doc.Pages[i];

// Create a background artifact, set the image and opacity (30%)
BackgroundArtifact bgArtifact = new BackgroundArtifact();
bgArtifact.SetImage(imagePath); // Load image from file
bgArtifact.Opacity = 0.3; // 30 percent opacity

// Add the artifact to the page's artifact collection
page.Artifacts.Add(bgArtifact);
}

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"PDF saved with background image: {outputPath}");
}
}
53 changes: 53 additions & 0 deletions working-with-images/add-background-pattern-image-to-pdf-pages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.IO;
using Aspose.Pdf;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output.pdf";
const string imagePath = "pattern.png";

// Verify input files exist
if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"Input PDF not found: {inputPath}");
return;
}
if (!File.Exists(imagePath))
{
Console.Error.WriteLine($"Background image not found: {imagePath}");
return;
}

// Load the PDF document (lifecycle rule: use using)
using (Document doc = new Document(inputPath))
{
// Iterate over all pages (1‑based indexing)
foreach (Page page in doc.Pages)
{
// Create a background artifact for the page
BackgroundArtifact bg = new BackgroundArtifact();

// Place the artifact behind page contents
bg.IsBackground = true;

// Set opacity to 10 percent (0.1)
bg.Opacity = 0.1;

// Assign the pattern image to the artifact
bg.SetImage(imagePath);

// Add the artifact to the page
page.Artifacts.Add(bg);
}

// Save the modified PDF (lifecycle rule: use Save inside using)
doc.Save(outputPath);
}

Console.WriteLine($"Background pattern applied and saved to '{outputPath}'.");
}
}
55 changes: 55 additions & 0 deletions working-with-images/add-background-pattern-to-pdf-pages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.IO;
using Aspose.Pdf;

class Program
{
static void Main()
{
const string inputPath = "input.pdf"; // source PDF
const string outputPath = "output_with_bg.pdf"; // result PDF
const string patternPath = "pattern.png"; // background pattern image

if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"Input file not found: {inputPath}");
return;
}

if (!File.Exists(patternPath))
{
Console.Error.WriteLine($"Pattern image not found: {patternPath}");
return;
}

// Load the PDF document (using block ensures proper disposal)
using (Document doc = new Document(inputPath))
{
// Iterate over all pages (Aspose.Pdf uses 1‑based indexing)
for (int i = 1; i <= doc.Pages.Count; i++)
{
Page page = doc.Pages[i];

// Create a background artifact for the page
BackgroundArtifact bgArtifact = new BackgroundArtifact();

// Set the image that will be used as the background pattern
bgArtifact.SetImage(patternPath);

// Place the artifact behind page contents
bgArtifact.IsBackground = true;

// Set a subtle opacity (5 percent)
bgArtifact.Opacity = 0.05;

// Add the artifact to the page's artifact collection
page.Artifacts.Add(bgArtifact);
}

// Save the modified PDF
doc.Save(outputPath);
}

Console.WriteLine($"PDF saved with background pattern: {outputPath}");
}
}
68 changes: 68 additions & 0 deletions working-with-images/add-background-texture-to-pdf-pages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Facades;

class Program
{
static void Main()
{
const string inputPdf = "input.pdf";
const string outputPdf = "output_with_background.pdf";
const string bgImagePath = "texture.png"; // subtle texture image

if (!File.Exists(inputPdf))
{
Console.Error.WriteLine($"Input file not found: {inputPdf}");
return;
}

if (!File.Exists(bgImagePath))
{
Console.Error.WriteLine($"Background image not found: {bgImagePath}");
return;
}

// Load the PDF document
using (Document doc = new Document(inputPdf))
{
// Iterate over all pages (1‑based indexing)
for (int i = 1; i <= doc.Pages.Count; i++)
{
Page page = doc.Pages[i];

// ----- Approach 1: use Page.BackgroundImage (generator only) -----
// This sets the background image for the page. It is not read when loading an existing PDF,
// but it will be written to the output PDF.
Aspose.Pdf.Image bgImg = new Aspose.Pdf.Image();
bgImg.File = bgImagePath;
page.BackgroundImage = bgImg;

// ----- Approach 2: use a BackgroundArtifact -----
// Adding a BackgroundArtifact gives more control (e.g., opacity, alignment).
// The artifact is placed behind page contents (IsBackground = true).
// Blend mode "Overlay" is not directly exposed in Aspose.Pdf; using opacity can simulate a subtle effect.
BackgroundArtifact artifact = new BackgroundArtifact();
artifact.IsBackground = true; // place behind page contents
artifact.Opacity = 0.5; // semi‑transparent for subtle overlay effect
artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
artifact.ArtifactVerticalAlignment = VerticalAlignment.Center;

// Set the image for the artifact
using (FileStream imgStream = File.OpenRead(bgImagePath))
{
artifact.SetImage(imgStream);
}

// Add the artifact to the page
page.Artifacts.Add(artifact);
}

// Save the modified PDF
doc.Save(outputPdf);
}

Console.WriteLine($"PDF saved with background texture: {outputPdf}");
}
}
50 changes: 50 additions & 0 deletions working-with-images/add-company-logo-to-first-page.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.IO;
using Aspose.Pdf;

class Program
{
static void Main()
{
const string inputPdfPath = "input.pdf";
const string outputPdfPath = "output.pdf";
const string logoImagePath = "logo.png";

// Verify that the required files exist
if (!File.Exists(inputPdfPath))
{
Console.Error.WriteLine($"Input PDF not found: {inputPdfPath}");
return;
}
if (!File.Exists(logoImagePath))
{
Console.Error.WriteLine($"Logo image not found: {logoImagePath}");
return;
}

// Load the PDF document (create‑load‑save lifecycle)
using (Document pdfDoc = new Document(inputPdfPath))
{
// Aspose.Pdf uses 1‑based page indexing; get the first page
Page firstPage = pdfDoc.Pages[1];

// Create an ImageStamp for the logo image
ImageStamp logoStamp = new ImageStamp(logoImagePath);

// Center the stamp horizontally and vertically on the page
logoStamp.HorizontalAlignment = HorizontalAlignment.Center;
logoStamp.VerticalAlignment = VerticalAlignment.Center;

// Ensure the stamp appears in front of page content
logoStamp.Background = false;

// Add the stamp to the first page only
firstPage.AddStamp(logoStamp);

// Save the modified PDF (save‑rule)
pdfDoc.Save(outputPdfPath);
}

Console.WriteLine($"Company logo added to first page and saved as '{outputPdfPath}'.");
}
}
51 changes: 51 additions & 0 deletions working-with-images/add-diagonal-image-watermark-to-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.IO;
using Aspose.Pdf;

class Program
{
static void Main()
{
const string inputPdf = "input.pdf"; // source PDF
const string outputPdf = "watermarked.pdf"; // result PDF
const string watermarkImage = "logo.png"; // image to use as watermark

if (!File.Exists(inputPdf))
{
Console.Error.WriteLine($"Input file not found: {inputPdf}");
return;
}

if (!File.Exists(watermarkImage))
{
Console.Error.WriteLine($"Watermark image not found: {watermarkImage}");
return;
}

// Load the PDF document (using the recommended load pattern)
using (Document doc = new Document(inputPdf))
{
// Iterate over all pages (Aspose.Pdf uses 1‑based indexing)
foreach (Page page in doc.Pages)
{
// Create an ImageStamp with the watermark image
ImageStamp stamp = new ImageStamp(watermarkImage);

// Set the arbitrary rotation angle (45 degrees) for diagonal placement
stamp.RotateAngle = 45;

// Optional: center the stamp on the page
stamp.HorizontalAlignment = HorizontalAlignment.Center;
stamp.VerticalAlignment = VerticalAlignment.Center;

// Add the stamp to the current page
page.AddStamp(stamp);
}

// Save the modified PDF (using the standard Save method)
doc.Save(outputPdf);
}

Console.WriteLine($"Watermarked PDF saved to '{outputPdf}'.");
}
}
Loading
Loading