Skip to content

Commit 6b72f5e

Browse files
Add 43 example(s) for Facades - XMP metadata
1 parent fc1a468 commit 6b72f5e

45 files changed

Lines changed: 2848 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
using System.Runtime.CompilerServices;
6+
7+
public static class PdfDocumentExtensions
8+
{
9+
public static void AddCreatorTool(this Document pdfDocument, string creator)
10+
{
11+
// Use PdfFileInfo facade to set the Creator metadata
12+
PdfFileInfo fileInfo = new PdfFileInfo();
13+
fileInfo.BindPdf(pdfDocument);
14+
fileInfo.Creator = creator;
15+
// No explicit Save required; the change is applied to the bound document
16+
}
17+
}
18+
19+
class Program
20+
{
21+
static void Main()
22+
{
23+
const string inputPath = "input.pdf";
24+
const string outputPath = "output.pdf";
25+
const string creatorValue = "MyApp Creator";
26+
27+
if (!File.Exists(inputPath))
28+
{
29+
Console.Error.WriteLine($"File not found: {inputPath}");
30+
return;
31+
}
32+
33+
using (Document document = new Document(inputPath))
34+
{
35+
document.AddCreatorTool(creatorValue);
36+
document.Save(outputPath);
37+
}
38+
39+
Console.WriteLine($"Creator set to '{creatorValue}' and saved to '{outputPath}'.");
40+
}
41+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using Aspose.Pdf;
5+
using Aspose.Pdf.Facades;
6+
using Aspose.Pdf.Text;
7+
8+
public class Program
9+
{
10+
public static void Main()
11+
{
12+
const string outputPath = "timestamped.pdf";
13+
14+
// Create a new PDF document
15+
using (Document doc = new Document())
16+
{
17+
// Add a blank page
18+
Page page = doc.Pages.Add();
19+
20+
// Add sample text to the page
21+
TextFragment text = new TextFragment("Sample PDF with timestamped XMP metadata.");
22+
page.Paragraphs.Add(text);
23+
24+
// Initialize XMP metadata facade bound to the document
25+
PdfXmpMetadata xmp = new PdfXmpMetadata(doc);
26+
27+
// Create an ISO‑8601 UTC timestamp
28+
string timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
29+
30+
// Add the timestamp as the ModifyDate property in XMP metadata
31+
xmp.Add("xmp:ModifyDate", timestamp);
32+
33+
// Save the PDF – guard against missing GDI+ (libgdiplus) on non‑Windows platforms
34+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
35+
{
36+
doc.Save(outputPath);
37+
Console.WriteLine($"PDF saved to '{outputPath}'.");
38+
}
39+
else
40+
{
41+
try
42+
{
43+
doc.Save(outputPath);
44+
Console.WriteLine($"PDF saved to '{outputPath}'.");
45+
}
46+
catch (TypeInitializationException ex) when (ContainsDllNotFound(ex))
47+
{
48+
Console.WriteLine("Warning: GDI+ (libgdiplus) is not available on this platform. " +
49+
"PDF was not saved. Install libgdiplus or run on Windows.");
50+
}
51+
}
52+
}
53+
}
54+
55+
// Helper to walk the inner‑exception chain and detect a missing native library
56+
private static bool ContainsDllNotFound(Exception ex)
57+
{
58+
while (ex != null)
59+
{
60+
if (ex is DllNotFoundException)
61+
return true;
62+
ex = ex.InnerException;
63+
}
64+
return false;
65+
}
66+
}

facades-xmp-metadata/agents.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
name: Facades - XMP metadata
3+
description: C# examples for Facades - XMP metadata using Aspose.PDF for .NET
4+
language: csharp
5+
framework: net10.0
6+
parent: ../agents.md
7+
---
8+
9+
# AGENTS - Facades - XMP metadata
10+
11+
## Persona
12+
13+
You are a C# developer specializing in PDF processing using Aspose.PDF for .NET,
14+
working within the **Facades - XMP metadata** category.
15+
This folder contains standalone C# examples for Facades - XMP metadata operations.
16+
See the root [agents.md](../agents.md) for repository-wide conventions and boundaries.
17+
18+
## Scope
19+
- This folder contains examples for **Facades - XMP metadata**.
20+
- Files are standalone `.cs` examples stored directly in this folder.
21+
22+
## Required Namespaces
23+
24+
- `using Aspose.Pdf;` (34/43 files) ← category-specific
25+
- `using Aspose.Pdf.Facades;` (32/43 files) ← category-specific
26+
- `using Aspose.Pdf.Optimization;` (1/43 files)
27+
- `using Aspose.Pdf.Text;` (1/43 files)
28+
- `using Aspose.Pdf.XfaConverter;` (1/43 files)
29+
- `using System;` (43/43 files)
30+
- `using System.IO;` (41/43 files)
31+
- `using System.Text;` (4/43 files)
32+
- `using System.Xml;` (3/43 files)
33+
- `using System.Collections.Generic;` (2/43 files)
34+
- `using NUnit.Framework;` (1/43 files)
35+
- `using System.Diagnostics;` (1/43 files)
36+
- `using System.Drawing;` (1/43 files)
37+
- `using System.Runtime.CompilerServices;` (1/43 files)
38+
- `using System.Runtime.InteropServices;` (1/43 files)
39+
- `using System.Text.Json;` (1/43 files)
40+
- `using System.Xml.Linq;` (1/43 files)
41+
- `using System.Xml.Schema;` (1/43 files)
42+
43+
## Common Code Pattern
44+
45+
Most files follow this pattern:
46+
47+
```csharp
48+
using (Document doc = new Document("input.pdf"))
49+
{
50+
// ... operations ...
51+
doc.Save("output.pdf");
52+
}
53+
```
54+
55+
## Files in this folder
56+
57+
| File | Title | Key APIs | Description |
58+
|------|-------|----------|-------------|
59+
| [add-creator-metadata](./add-creator-metadata.cs) | Add Creator Metadata to PDF via Extension Method | `Document`, `PdfFileInfo` | Demonstrates a reusable extension method that sets the Creator metadata on any Aspose.Pdf Document. |
60+
| [add-timestamp-xmp-metadata](./add-timestamp-xmp-metadata.cs) | Add Timestamp to XMP Metadata in PDF | `Document`, `Page`, `TextFragment` | Creates a PDF, adds a page with sample text, and inserts a timestamp into the XMP metadata using ... |
61+
| [benchmark-xmp-metadata](./benchmark-xmp-metadata.cs) | Benchmark XMP Metadata Extraction from Large PDF | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Measures the time required to read XMP metadata from a PDF with many pages using Aspose.Pdf. |
62+
| [bind-pdf-byte-array-xmpmetadata](./bind-pdf-byte-array-xmpmetadata.cs) | Bind PDF from Byte Array to PdfXmpMetadata | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Demonstrates how to bind a PDF loaded from a byte array to the PdfXmpMetadata facade and extract ... |
63+
| [bind-pdf-xmpmetadata](./bind-pdf-xmpmetadata.cs) | Bind PDF to PdfXmpMetadata and Retrieve XMP Metadata | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Demonstrates loading a PDF file and binding it to a PdfXmpMetadata object to read the XMP metadata. |
64+
| [clear-xmp-metadata](./clear-xmp-metadata.cs) | Clear XMP Metadata from PDF | `Document`, `PdfXmpMetadata`, `Clear` | Demonstrates how to remove all XMP metadata from a PDF, leaving only the required schema header. |
65+
| [compress-pdf-read-xmp](./compress-pdf-read-xmp.cs) | Compress PDF and Read XMP Metadata | `Document`, `CompressionLevel`, `PdfXmpMetadata` | Demonstrates compressing a PDF with high compression settings and then reading its XMP metadata. |
66+
| [copy-xmp-metadata-merge-pdf](./copy-xmp-metadata-merge-pdf.cs) | Copy XMP Metadata and Merge PDFs | `PdfXmpMetadata`, `SetXmpMetadata`, `Add` | Demonstrates extracting XMP metadata from a source PDF, applying it to a target PDF, and then mer... |
67+
| [decrypt-update-reencrypt-pdf](./decrypt-update-reencrypt-pdf.cs) | Decrypt, Update CreatorTool, and Re‑Encrypt PDF | `Document`, `Decrypt`, `Encrypt` | Shows how to open a password‑protected PDF, decrypt it, modify the CreatorTool metadata, and re‑e... |
68+
| [detect-xmp-metadata](./detect-xmp-metadata.cs) | Detect and Add XMP Metadata to PDF | `Document`, `PdfXmpMetadata`, `BindPdf` | Shows how to check if a PDF contains XMP metadata using PdfXmpMetadata and optionally add a custo... |
69+
| [export-xmp-metadata-json](./export-xmp-metadata-json.cs) | Export PDF XMP Metadata as JSON | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Demonstrates how to extract XMP metadata from a PDF using Aspose.Pdf and convert it to JSON for i... |
70+
| [export-xmp-metadata](./export-xmp-metadata.cs) | Export XMP Metadata from PDF to Side‑car File | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Demonstrates how to extract XMP metadata from a PDF using Aspose.Pdf and save it as a separate .x... |
71+
| [extract-xmp-metadata](./extract-xmp-metadata.cs) | Extract XMP Metadata from PDF to Dictionary | `PdfXmpMetadata`, `BindPdf`, `ToStringValue` | Demonstrates how to read XMP metadata from a PDF file and convert it into a simple Dictionary<str... |
72+
| [generate-minimal-xmp-metadata](./generate-minimal-xmp-metadata.cs) | Generate Minimal XMP Metadata When Missing | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Loads a PDF, checks for existing XMP metadata, and adds a minimal set of metadata if none is pres... |
73+
| [handle-encrypted-pdf-binding](./handle-encrypted-pdf-binding.cs) | Handle Encrypted PDF Binding Errors | `BindPdf`, `Save`, `InvalidPasswordException` | Demonstrates how to catch InvalidPasswordException when binding an encrypted PDF using a facade. |
74+
| [html-to-pdf-baseurl-switch](./html-to-pdf-baseurl-switch.cs) | Generate PDF from HTML with optional BaseUrl injection | `Document`, `HtmlLoadOptions` | Demonstrates how to disable BaseUrl injection when converting HTML to PDF by using HtmlLoadOption... |
75+
| [insert-page-update-xmp](./insert-page-update-xmp.cs) | Insert Page and Update XMP Metadata in PDF | `Document`, `Page`, `Insert` | Demonstrates how to insert a new page into a PDF and update its XMP metadata before saving using ... |
76+
| [list-xmp-namespaces](./list-xmp-namespaces.cs) | List XMP Namespaces in PDF | `PdfXmpMetadata`, `BindPdf`, `Keys` | Demonstrates how to extract and display all XMP namespace prefixes and their URIs from a PDF file... |
77+
| [log-original-xmp-metadata](./log-original-xmp-metadata.cs) | Log Original XMP Metadata from PDF | `Document`, `GetXmpMetadata` | Demonstrates how to read and log the original XMP XML metadata from a PDF document before any cha... |
78+
| [log-xmp-overwrite-warning](./log-xmp-overwrite-warning.cs) | Log Warning When Overwriting Existing XMP Metadata | `PdfXmpMetadata`, `BindPdf`, `Add` | Demonstrates checking existing XMP metadata and logging a warning if a property already has a non... |
79+
| [log-xmp-size](./log-xmp-size.cs) | Log XMP Metadata Size Before and After Modification | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Demonstrates how to read the XMP metadata block size, modify it, and log the size before and afte... |
80+
| [modify-xmp-metadata](./modify-xmp-metadata.cs) | Modify XMP Metadata Property in PDF | `Document`, `PdfXmpMetadata`, `SetXmpMetadata` | Demonstrates how to read XMP metadata from a PDF, change a property, and write the updated metada... |
81+
| [modify-xmp-metadata__v2](./modify-xmp-metadata__v2.cs) | Modify XMP Metadata and Save PDF | `PdfXmpMetadata`, `BindPdf`, `Add` | Demonstrates how to edit XMP metadata of a PDF using Aspose.Pdf and save the updated document. |
82+
| [read-xmp-metadata-unc](./read-xmp-metadata-unc.cs) | Read XMP Metadata from PDF on UNC Path | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Demonstrates how to load a PDF from a network share using a UNC path and extract its XMP metadata... |
83+
| [read-xmp-schema-properties](./read-xmp-schema-properties.cs) | Read Specific XMP Schema Properties from PDF | `PdfXmpMetadata`, `XmlDocument`, `XmlNamespaceManager` | Demonstrates how to retrieve XMP metadata from a PDF, parse the XML, and extract specific schema ... |
84+
| [refresh-creator-tool](./refresh-creator-tool.cs) | Refresh CreatorTool Metadata for PDFs Nightly | `Document`, `Creator`, `Save` | A console app that scans a repository of PDF files, updates the Creator metadata field, and saves... |
85+
| [remove-nickname-xmp](./remove-nickname-xmp.cs) | Remove Nickname from PDF XMP Metadata | `PdfXmpMetadata`, `BindPdf`, `Remove` | Demonstrates how to delete the Nickname element from a PDF's XMP metadata using Aspose.Pdf. |
86+
| [remove-xmp-metadata](./remove-xmp-metadata.cs) | Remove XMP Metadata from PDF | `Document`, `RemoveMetadata`, `Save` | Demonstrates how to delete the entire XMP metadata block from a PDF, producing a metadata‑free file. |
87+
| [replace-pdf-xmp-metadata](./replace-pdf-xmp-metadata.cs) | Replace PDF XMP Metadata from External File | `SetXmpMetadata`, `Save` | Loads a PDF, replaces its XMP metadata block with the contents of an external .xmp file, and save... |
88+
| [retrieve-xmp-metadata](./retrieve-xmp-metadata.cs) | Retrieve XMP Metadata from PDF | `PdfXmpMetadata`, `BindPdf`, `GetXmpMetadata` | Demonstrates how to extract the raw XMP XML metadata from a PDF using Aspose.Pdf's PdfXmpMetadata... |
89+
| ... | | | *and 13 more files* |
90+
91+
## Category Statistics
92+
- Total examples: 43
93+
94+
## General Tips
95+
- See parent [agents.md](../agents.md) for:
96+
- **Boundaries** — Always / Ask First / Never rules for all examples
97+
- **Common Mistakes** — verified anti-patterns that cause build failures
98+
- **Domain Knowledge** — cross-cutting API-specific gotchas
99+
- **Testing Guide** — build and run verification steps
100+
- Review code examples in this folder for Facades - XMP metadata patterns
101+
102+
<!-- AUTOGENERATED:START -->
103+
Updated: 2026-04-01 | Run: `20260401_153432_9ad69f`
104+
<!-- AUTOGENERATED:END -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using Aspose.Pdf;
5+
using Aspose.Pdf.Facades;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPath = "large.pdf";
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine("File not found: " + inputPath);
16+
return;
17+
}
18+
19+
using (Document document = new Document(inputPath))
20+
{
21+
PdfXmpMetadata xmpMetadata = new PdfXmpMetadata();
22+
xmpMetadata.BindPdf(document);
23+
24+
Stopwatch timer = new Stopwatch();
25+
timer.Start();
26+
byte[] xmpData = xmpMetadata.GetXmpMetadata();
27+
timer.Stop();
28+
29+
Console.WriteLine("XMP metadata size (bytes): " + xmpData.Length);
30+
Console.WriteLine("Time elapsed (ms): " + timer.ElapsedMilliseconds);
31+
}
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputMetadataPath = "metadata.xml";
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine($"File not found: {inputPath}");
16+
return;
17+
}
18+
19+
// Load the PDF file into a byte array
20+
byte[] pdfBytes = File.ReadAllBytes(inputPath);
21+
22+
// Create a memory stream from the byte array and load it into a Document
23+
using (MemoryStream pdfStream = new MemoryStream(pdfBytes))
24+
using (Document pdfDocument = new Document(pdfStream))
25+
{
26+
// Bind the Document to PdfXmpMetadata facade
27+
PdfXmpMetadata xmpMetadata = new PdfXmpMetadata();
28+
xmpMetadata.BindPdf(pdfDocument);
29+
30+
// Retrieve the XMP metadata as a byte array (XML format)
31+
byte[] metadataBytes = xmpMetadata.GetXmpMetadata();
32+
33+
// Save the metadata to a file for inspection
34+
File.WriteAllBytes(outputMetadataPath, metadataBytes);
35+
Console.WriteLine($"XMP metadata extracted to '{outputMetadataPath}'.");
36+
}
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf.Facades;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPath = "input.pdf";
10+
11+
if (!File.Exists(inputPath))
12+
{
13+
Console.Error.WriteLine($"File not found: {inputPath}");
14+
return;
15+
}
16+
17+
// Create a new PdfXmpMetadata instance
18+
PdfXmpMetadata xmpMetadata = new PdfXmpMetadata();
19+
// Bind the PDF file to the facade
20+
xmpMetadata.BindPdf(inputPath);
21+
22+
// Retrieve the XMP metadata as a byte array
23+
byte[] xmpBytes = xmpMetadata.GetXmpMetadata();
24+
Console.WriteLine($"XMP metadata size: {xmpBytes.Length} bytes");
25+
26+
// Clean up resources
27+
xmpMetadata.Close();
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "output.pdf";
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine($"File not found: {inputPath}");
16+
return;
17+
}
18+
19+
using (Document doc = new Document(inputPath))
20+
{
21+
PdfXmpMetadata xmp = new PdfXmpMetadata(doc);
22+
xmp.Clear();
23+
xmp.Save(outputPath);
24+
xmp.Close();
25+
}
26+
27+
Console.WriteLine($"XMP metadata cleared. Saved to '{outputPath}'.");
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
using Aspose.Pdf.Optimization;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPath = "input.pdf";
12+
const string compressedPath = "compressed.pdf";
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine("Input file not found: " + inputPath);
17+
return;
18+
}
19+
20+
// Load the PDF, apply high compression, and save the result
21+
using (Document document = new Document(inputPath))
22+
{
23+
// Apply high compression using OptimizationOptions. This works for all
24+
// Aspose.Pdf versions, even when the Document.Compress() method is not
25+
// available.
26+
OptimizationOptions opt = OptimizationOptions.All();
27+
// Increase compression aggressiveness (you can tune these values).
28+
opt.CompressImages = true;
29+
opt.ImageQuality = 50; // lower quality → higher compression
30+
opt.RemoveUnusedObjects = true;
31+
opt.RemoveUnusedStreams = true;
32+
document.OptimizeResources(opt);
33+
34+
document.Save(compressedPath);
35+
}
36+
37+
// Read XMP metadata from the compressed PDF
38+
PdfXmpMetadata xmpMetadata = new PdfXmpMetadata();
39+
xmpMetadata.BindPdf(compressedPath);
40+
byte[] metadataBytes = xmpMetadata.GetXmpMetadata();
41+
string metadataXml = System.Text.Encoding.UTF8.GetString(metadataBytes);
42+
Console.WriteLine("XMP Metadata:");
43+
Console.WriteLine(metadataXml);
44+
}
45+
}

0 commit comments

Comments
 (0)