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
79 changes: 39 additions & 40 deletions facades-texts-and-images/AGENTS.md

Large diffs are not rendered by default.

47 changes: 0 additions & 47 deletions facades-texts-and-images/README.md

This file was deleted.

85 changes: 52 additions & 33 deletions facades-texts-and-images/add-background-png-to-all-pdf-pages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,75 @@ class Program
{
static void Main()
{
const string inputPdfPath = "input.pdf";
const string outputPdfPath = "output.pdf";
const string backgroundPng = "background.png";
const string inputPdf = "input.pdf";
const string outputPdf = "output.pdf";
const string imagePath = "background.png";

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

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

// Create the PdfFileMend facade
PdfFileMend mender = new PdfFileMend();

// Load the source PDF
mender.BindPdf(inputPdfPath);
// -----------------------------------------------------------------
// 1. Load the PDF to obtain page count and page rectangles.
// Aspose.Pdf uses 1‑based page indexing.
// -----------------------------------------------------------------
int pageCount;
(double llx, double lly, double urx, double ury)[] pageRects;

// Open the background image once and reuse the stream for each page
using (FileStream imgStream = File.OpenRead(backgroundPng))
using (Document doc = new Document(inputPdf))
{
// Get total page count (Aspose.Pdf uses 1‑based indexing)
int pageCount = mender.Document.Pages.Count;
pageCount = doc.Pages.Count;
pageRects = new (double, double, double, double)[pageCount];

// Iterate over all pages and add the image as background
for (int pageNum = 1; pageNum <= pageCount; pageNum++)
for (int i = 1; i <= pageCount; i++)
{
// Reset stream position for each AddImage call
imgStream.Position = 0;
var rect = doc.Pages[i].Rect;
pageRects[i - 1] = (rect.LLX, rect.LLY, rect.URX, rect.URY);
}
}

// -----------------------------------------------------------------
// 2. Initialise PdfFileMend without the obsolete destination ctor.
// Bind the source PDF, then add images, and finally Save.
// -----------------------------------------------------------------
PdfFileMend mender = new PdfFileMend();
mender.BindPdf(inputPdf);

// Retrieve page dimensions
Page page = mender.Document.Pages[pageNum];
float pageWidth = (float)page.Rect.Width;
float pageHeight = (float)page.Rect.Height;
// -----------------------------------------------------------------
// 3. Iterate over all pages and add the background image.
// Use the stream overload of AddImage and cast coordinates to float.
// -----------------------------------------------------------------
for (int i = 1; i <= pageCount; i++)
{
var (llx, lly, urx, ury) = pageRects[i - 1];

// Add the image covering the whole page
// lowerLeftX = 0, lowerLeftY = 0, upperRightX = pageWidth, upperRightY = pageHeight
mender.AddImage(imgStream, pageNum, 0, 0, pageWidth, pageHeight);
using (FileStream imgStream = File.OpenRead(imagePath))
{
mender.AddImage(
imgStream,
i,
(float)llx,
(float)lly,
(float)urx,
(float)ury);
}
}

// Save the modified PDF
mender.Save(outputPdfPath);

// Release resources
// -----------------------------------------------------------------
// 4. Save the modified PDF and release resources.
// -----------------------------------------------------------------
mender.Save(outputPdf);
mender.Close();

Console.WriteLine($"Background image added to all {pageCount} pages. Output saved to '{outputPdf}'.");
}
}
}
48 changes: 48 additions & 0 deletions facades-texts-and-images/add-header-image-to-pdf-documents.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(string[] args)
{
// Expect at least a header image path and one PDF file path
if (args.Length < 2)
{
Console.Error.WriteLine("Usage: app.exe <headerImagePath> <pdfPath1> [<pdfPath2> ...]");
return;
}

string headerImagePath = args[0];
if (!File.Exists(headerImagePath))
{
Console.Error.WriteLine($"Header image not found: {headerImagePath}");
return;
}

// Process each PDF file supplied on the command line
for (int i = 1; i < args.Length; i++)
{
string inputPdfPath = args[i];
if (!File.Exists(inputPdfPath))
{
Console.Error.WriteLine($"PDF not found: {inputPdfPath}");
continue;
}

// Create an output file name by appending "_header" before the extension
string outputPdfPath = Path.Combine(
Path.GetDirectoryName(inputPdfPath) ?? string.Empty,
Path.GetFileNameWithoutExtension(inputPdfPath) + "_header.pdf");

// Use PdfFileStamp to add the header image
PdfFileStamp fileStamp = new PdfFileStamp();
fileStamp.BindPdf(inputPdfPath); // Load the source PDF
fileStamp.AddHeader(headerImagePath, 50f); // Add image as header with a top margin of 50 units
fileStamp.Save(outputPdfPath); // Write the modified PDF to the output path
fileStamp.Close(); // Release resources

Console.WriteLine($"Processed: {inputPdfPath} → {outputPdfPath}");
}
}
}
78 changes: 12 additions & 66 deletions facades-texts-and-images/add-header-image-to-pdfs.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,12 @@
using System;
using System.IO;
using Aspose.Pdf.Facades;

class Program
{
static void Main()
{
// Path to the header image that will be added to each PDF.
const string headerImagePath = "header.jpg";

// List of PDF files to process.
string[] inputPdfFiles = new[]
{
"document1.pdf",
"document2.pdf",
"document3.pdf"
};

// Directory where the output PDFs with headers will be saved.
const string outputDirectory = "ProcessedPdfs";

// Ensure the output directory exists.
Directory.CreateDirectory(outputDirectory);

// Verify that the header image exists before processing.
if (!File.Exists(headerImagePath))
{
Console.Error.WriteLine($"Header image not found: {headerImagePath}");
return;
}

foreach (string inputPath in inputPdfFiles)
{
// Skip missing input files.
if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"Input PDF not found: {inputPath}");
continue;
}

// Build the output file path.
string outputPath = Path.Combine(
outputDirectory,
Path.GetFileNameWithoutExtension(inputPath) + "_withHeader.pdf");

// Use PdfFileStamp to add the header image.
using (PdfFileStamp stamp = new PdfFileStamp())
{
// Bind the source PDF.
stamp.BindPdf(inputPath);

// Add the header image. Top margin is set to 20 points.
stamp.AddHeader(headerImagePath, 20f);

// Save the modified PDF to the output path.
stamp.Save(outputPath);

// Close the stamp facade (writes any remaining changes).
stamp.Close();
}

Console.WriteLine($"Processed: {inputPath} → {outputPath}");
}
}
}
// URL-STABILITY STUB
//
// add header image to pdfs
//
// This example has been consolidated or moved. Current location:
// https://github.com/aspose-pdf/agentic-net-examples/blob/main/facades-texts-and-images/add-header-image-to-pdf-documents.cs
//
// This stub keeps the original URL live for existing bookmarks,
// search-engine indexes, and blog-post links. See the canonical
// location above for the current, maintained implementation.

class Program { static void Main() { } }
106 changes: 12 additions & 94 deletions facades-texts-and-images/add-image-and-text-audit-pdf.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,12 @@
using System;
using System.IO;
using System.Drawing; // for System.Drawing.Rectangle
using Aspose.Pdf;
using Aspose.Pdf.Facades;

class PdfAuditProcessor
{
// Simple audit logger – appends a line to a log file.
private static void LogOperation(string operation, int pageNumber, string detail)
{
string timestamp = DateTime.UtcNow.ToString("o"); // ISO 8601 format
string logLine = $"{timestamp} | {operation} | Page: {pageNumber} | Detail: {detail}";
File.AppendAllText("audit.log", logLine + Environment.NewLine);
}

static void Main()
{
const string inputPdfPath = "input.pdf";
const string outputPdfPath = "output.pdf";

// ----- Add Image using PdfFileMend -----
const string imagePath = "logo.png";
int imagePage = 1;
float imgLlX = 50f, imgLlY = 700f, imgUrX = 150f, imgUrY = 800f;

// Ensure input files exist
if (!File.Exists(inputPdfPath))
{
Console.Error.WriteLine($"Input PDF not found: {inputPdfPath}");
return;
}
if (!File.Exists(imagePath))
{
Console.Error.WriteLine($"Image file not found: {imagePath}");
return;
}

// Load PDF and bind it to PdfFileMend
using (Document doc = new Document(inputPdfPath))
using (PdfFileMend mend = new PdfFileMend())
{
mend.BindPdf(doc); // initialize facade with the document

// Add the image
using (FileStream imgStream = File.OpenRead(imagePath))
{
bool added = mend.AddImage(imgStream, imagePage, imgLlX, imgLlY, imgUrX, imgUrY);
if (added)
{
LogOperation("AddImage", imagePage, Path.GetFileName(imagePath));
}
else
{
Console.Error.WriteLine("Failed to add image.");
}
}

// Save intermediate result (still the final output file)
mend.Save(outputPdfPath);
}

// ----- Add Text using PdfContentEditor -----
const string textContent = "Confidential";
int textPage = 2;
// System.Drawing.Rectangle expects (x, y, width, height)
System.Drawing.Rectangle textRect = new System.Drawing.Rectangle(
100, // X (lower‑left X)
500, // Y (lower‑left Y)
300 - 100, // Width
550 - 500); // Height

const string fontName = "Helvetica";
const int fontSize = 24;
const bool isBold = false;
const string colorHex = "#FF0000"; // Red in hex

// Load the PDF (the one just saved with the image) and bind it to PdfContentEditor
using (Document doc = new Document(outputPdfPath))
using (PdfContentEditor editor = new PdfContentEditor())
{
editor.BindPdf(doc); // initialize facade with the document

// Create a text annotation on the specified page
editor.CreateText(textRect, textContent, fontName, isBold, colorHex, fontSize);
LogOperation("AddText", textPage, textContent);

// Save final PDF
editor.Save(outputPdfPath);
}

Console.WriteLine($"Processing completed. Output saved to '{outputPdfPath}'. Audit log written to 'audit.log'.");
}
}
// URL-STABILITY STUB
//
// add image and text audit pdf
//
// This example has been consolidated or moved. Current location:
// https://github.com/aspose-pdf/agentic-net-examples/tree/main/facades-texts-and-images
//
// This stub keeps the original URL live for existing bookmarks,
// search-engine indexes, and blog-post links. See the canonical
// location above for the current, maintained implementation.

class Program { static void Main() { } }
Loading
Loading