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
44 changes: 44 additions & 0 deletions facades-stamps/add-background-text-stamp-30-opacity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;

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

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

// Load the PDF document
var document = new Document(inputPath);

// Create a text stamp that will act as a background watermark
var stamp = new TextStamp("CONFIDENTIAL");
stamp.TextState.Font = FontRepository.FindFont("Helvetica");
stamp.TextState.FontSize = 48;
stamp.TextState.ForegroundColor = Aspose.Pdf.Color.Gray;
stamp.Opacity = 0.3f; // 30% opacity
stamp.Background = true; // place behind page content
stamp.HorizontalAlignment = HorizontalAlignment.Center;
stamp.VerticalAlignment = VerticalAlignment.Center;

// Apply the stamp to every page
foreach (Page page in document.Pages)
{
page.AddStamp(stamp);
}

// Save the modified PDF
document.Save(outputPath);

Console.WriteLine($"Background stamp applied with 30% opacity: {outputPath}");
}
}
52 changes: 52 additions & 0 deletions facades-stamps/add-background-watermark-stamp-to-pdf-pages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Facades;
using System.Drawing; // for System.Drawing.Color

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

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

// Initialize the PdfFileStamp facade using the modern API.
PdfFileStamp fileStamp = new PdfFileStamp();
fileStamp.BindPdf(inputPath); // replaces the obsolete InputFile property

// Create a stamp that will be used as a background watermark.
Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();

// Build the formatted text for the watermark.
// NOTE: Use System.Drawing.Color for the color argument and a float for the font size.
Aspose.Pdf.Facades.FormattedText ft = new Aspose.Pdf.Facades.FormattedText(
"CONFIDENTIAL", // text
System.Drawing.Color.Gray, // text color (System.Drawing.Color)
"Helvetica", // font name
EncodingType.Winansi, // encoding
false, // embed font?
48f); // font size (float)

stamp.BindLogo(ft); // bind the formatted text to the stamp
stamp.IsBackground = true; // place the stamp behind existing page content

// Apply the stamp only to pages 2 through 5 (1‑based indexing).
stamp.Pages = new int[] { 2, 3, 4, 5 };

// Add the stamp to the document.
fileStamp.AddStamp(stamp);

// Save the modified PDF using the modern API.
fileStamp.Save(outputPath); // replaces the obsolete OutputFile property
fileStamp.Close();

Console.WriteLine($"Background watermark applied to pages 2‑5 and saved as '{outputPath}'.");
}
}
46 changes: 46 additions & 0 deletions facades-stamps/add-creation-date-stamp-to-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;

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

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

// Load the source PDF
Document doc = new Document(inputPath);

// Retrieve the document creation date and format it as yyyy-MM-dd
DateTime creationDate = doc.Info.CreationDate;
string dateText = creationDate.ToString("yyyy-MM-dd");

// Create a text stamp that will be placed at the top‑left corner
TextStamp stamp = new TextStamp(dateText);
stamp.TextState.Font = FontRepository.FindFont("Helvetica");
stamp.TextState.FontSize = 12;
stamp.TextState.ForegroundColor = Aspose.Pdf.Color.Black; // fully‑qualified Aspose color
stamp.HorizontalAlignment = HorizontalAlignment.Left;
stamp.VerticalAlignment = VerticalAlignment.Top;
stamp.XIndent = 10f; // margin from the left edge
stamp.YIndent = 10f; // margin from the top edge

// Apply the stamp to every page (or change to a specific page if needed)
foreach (Page page in doc.Pages)
{
page.AddStamp(stamp);
}

// Save the stamped PDF
doc.Save(outputPath);
Console.WriteLine($"Stamped PDF saved to '{outputPath}'.");
}
}
48 changes: 48 additions & 0 deletions facades-stamps/add-custom-border-stamp-to-pdf-page.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.IO;
using Aspose.Pdf.Facades;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "highlighted.pdf";

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

// Use PdfContentEditor (facade) to add a square annotation with custom border thickness and color.
using (PdfContentEditor editor = new PdfContentEditor())
{
// Bind the existing PDF document.
editor.BindPdf(inputPath);

// Define the area to highlight on the page (x, y, width, height).
// Fully qualify System.Drawing.Rectangle to avoid ambiguity with Aspose.Pdf.Rectangle.
var highlightRect = new System.Drawing.Rectangle(100, 500, 200, 100);

// Create a square annotation:
// - contents: description shown when the annotation is selected.
// - color: border color (red in this example).
// - square: true (square shape; false would create a circle).
// - page: 1 (Aspose.Pdf uses 1‑based page indexing).
// - borderWidth: custom thickness of the border (5 points here).
editor.CreateSquareCircle(
highlightRect,
"Important Section",
System.Drawing.Color.Red,
true,
1,
5);

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

Console.WriteLine($"Stamped PDF saved to '{outputPath}'.");
}
}
53 changes: 53 additions & 0 deletions facades-stamps/add-disclaimer-stamp-to-first-page.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.IO;
using Aspose.Pdf.Facades;

class Program
{
static void Main()
{
// Paths – adjust as needed
const string inputPdf = "target.pdf"; // PDF to be stamped
const string disclaimerPdf = "disclaimer.pdf"; // PDF containing the legal disclaimer (first page will be used)
const string outputPdf = "target_with_disclaimer.pdf";

// Verify files exist
if (!File.Exists(inputPdf))
{
Console.Error.WriteLine($"Input PDF not found: {inputPdf}");
return;
}
if (!File.Exists(disclaimerPdf))
{
Console.Error.WriteLine($"Disclaimer PDF not found: {disclaimerPdf}");
return;
}

// Initialize the facade, bind the source document and configure the stamp
PdfFileStamp fileStamp = new PdfFileStamp();
try
{
// Bind the target PDF that will receive the stamp
fileStamp.BindPdf(inputPdf);

// Create a stamp that uses the first page of the disclaimer PDF
Stamp stamp = new Stamp();
stamp.BindPdf(disclaimerPdf, 1); // use page 1 of disclaimer.pdf as stamp content
stamp.IsBackground = false; // place stamp on top of existing content
stamp.Pages = new int[] { 1 }; // apply only to the first page of the target PDF

// Add the stamp to the document
fileStamp.AddStamp(stamp);

// Save the result
fileStamp.Save(outputPdf);
}
finally
{
// Close releases resources; PdfFileStamp does not implement IDisposable
fileStamp.Close();
}

Console.WriteLine($"Stamped PDF saved to '{outputPdf}'.");
}
}
43 changes: 43 additions & 0 deletions facades-stamps/add-dynamic-text-stamp-to-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output.pdf";
const string author = "John Doe";

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

// Load the PDF document inside a using block for deterministic disposal
using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document(inputPath))
{
// Create a text stamp with interpolated content (date and author)
string stampText = $"Created on {DateTime.Now:yyyy-MM-dd} by {author}";
Aspose.Pdf.TextStamp textStamp = new Aspose.Pdf.TextStamp(stampText);

// Configure stamp appearance (optional)
textStamp.Background = false; // draw on top of page content
textStamp.Opacity = 0.7; // semi‑transparent
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Bottom;
textStamp.YIndent = 20; // distance from bottom edge

// Add the stamp to the first page (pages are 1‑based)
doc.Pages[1].AddStamp(textStamp);

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

Console.WriteLine($"Stamp added and saved to '{outputPath}'.");
}
}
43 changes: 43 additions & 0 deletions facades-stamps/add-file-name-header-stamp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;

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

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

// Load the PDF document
Document pdfDocument = new Document(inputPath);

// Create a TextStamp that uses the {FileName} placeholder.
// Aspose.Pdf replaces {FileName} with the actual file name when the stamp is applied.
TextStamp headerStamp = new TextStamp("{FileName}");
headerStamp.TextState.Font = FontRepository.FindFont("Helvetica");
headerStamp.TextState.FontSize = 12;
headerStamp.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
headerStamp.HorizontalAlignment = HorizontalAlignment.Center;
headerStamp.VerticalAlignment = VerticalAlignment.Top;
headerStamp.YIndent = 20f; // top margin in points

// Apply the stamp to every page
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(headerStamp);
}

// Save the modified PDF
pdfDocument.Save(outputPath);

Console.WriteLine($"Stamped PDF saved to '{outputPath}'.");
}
}
43 changes: 43 additions & 0 deletions facades-stamps/add-footer-date-stamp-to-last-page.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;

class Program
{
static void Main()
{
const string inputPath = "input.pdf";
const string outputPath = "output_lastpage_footer.pdf";

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

// Load the PDF document
Document doc = new Document(inputPath);
// Get the last page (pages are 1‑based)
Page lastPage = doc.Pages[doc.Pages.Count];

// Prepare footer text (current date in MM-dd-yyyy format)
string footerText = DateTime.Now.ToString("MM-dd-yyyy");

// Create a text stamp for the footer
TextStamp stamp = new TextStamp(footerText);
stamp.TextState.Font = FontRepository.FindFont("Helvetica");
stamp.TextState.FontSize = 12;
stamp.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
stamp.HorizontalAlignment = HorizontalAlignment.Center;
stamp.VerticalAlignment = VerticalAlignment.Bottom;
stamp.YIndent = 10; // distance from the bottom edge

// Add the stamp only to the last page
lastPage.AddStamp(stamp);

// Save the modified PDF
doc.Save(outputPath);
Console.WriteLine($"Footer stamp added to last page. Output saved to '{outputPath}'.");
}
}
Loading
Loading