| name | aspose-pdf-examples |
|---|---|
| description | AI-friendly C# code examples for Aspose.PDF for .NET |
| language | csharp |
| framework | net10.0 |
| package | Aspose.PDF 26.3.0 |
AI-friendly repository containing validated C# examples for Aspose.PDF for .NET API.
You are a C# developer specializing in PDF processing using Aspose.PDF for .NET. When working in this repository:
- Each
.csfile is a standalone Console Application — do not create multi-file projects - All examples must compile and run without errors using
dotnet buildanddotnet run - Follow the conventions, boundaries, and anti-patterns documented below exactly
- Use the Command Reference section for build/run commands
This repository contains 2708 working code examples demonstrating Aspose.PDF for .NET capabilities.
Statistics (as of 2026-04-10):
- Total Examples: 2708
- Categories: 34
- Examples: 45
- Guide: agents.md
- Examples: 57
- Guide: agents.md
- Examples: 28
- Guide: agents.md
- Examples: 102
- Guide: agents.md
- Examples: 122
- Guide: agents.md
- Examples: 41
- Guide: agents.md
- Examples: 107
- Guide: agents.md
- Examples: 35
- Guide: agents.md
- Examples: 40
- Guide: agents.md
- Examples: 101
- Guide: agents.md
- Examples: 214
- Guide: agents.md
- Examples: 83
- Guide: agents.md
- Examples: 35
- Guide: agents.md
- Examples: 88
- Guide: agents.md
- Examples: 40
- Guide: agents.md
- Examples: 117
- Guide: agents.md
- Examples: 40
- Guide: agents.md
- Examples: 35
- Guide: agents.md
- Examples: 50
- Guide: agents.md
- Examples: 29
- Guide: agents.md
- Examples: 45
- Guide: agents.md
- Examples: 84
- Guide: agents.md
- Examples: 99
- Guide: agents.md
- Examples: 65
- Guide: agents.md
- Examples: 87
- Guide: agents.md
- Examples: 50
- Guide: agents.md
- Examples: 163
- Guide: agents.md
- Examples: 50
- Guide: agents.md
- Examples: 240
- Guide: agents.md
- Examples: 85
- Guide: agents.md
- Examples: 72
- Guide: agents.md
- Examples: 109
- Guide: agents.md
- Examples: 75
- Guide: agents.md
- Examples: 75
- Guide: agents.md
These rules are mandatory for every example.
// CORRECT
Document document = new Document("input.pdf");
Page page = document.Pages[1];
TextFragmentAbsorber absorber = new TextFragmentAbsorber("search");
// WRONG
// var document = new Document("input.pdf");
// var page = document.Pages[1];// CORRECT — first page is index 1
Page firstPage = document.Pages[1];
Annotation firstAnnotation = page.Annotations[1];
FileSpecification firstFile = document.EmbeddedFiles[1];
// WRONG — index 0 throws IndexOutOfRangeException
// Page page = document.Pages[0];// CORRECT
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(100, 200, 300, 400);
Aspose.Pdf.Drawing.Rectangle drawRect = new Aspose.Pdf.Drawing.Rectangle(50, 50, 200, 100);
Aspose.Pdf.Color pdfColor = Aspose.Pdf.Color.Blue;
// WRONG — ambiguous CS0104
// Rectangle rect = new Rectangle(100, 200, 300, 400);
// Color color = Color.Blue;// CORRECT
using (Document document = new Document("input.pdf"))
{
// work with document
document.Save("output.pdf");
}Document document = new Document("input.pdf");
// ... make modifications ...
document.Save("output.pdf");Check with a human before doing any of these:
- Creating multi-file projects — each example must be a single
.csfile - Using deprecated APIs — check the Aspose.PDF changelog for the current API surface
- Adding NuGet packages beyond
Aspose.PDF— the.csprojtemplate only includes Aspose.PDF - Modifying shared infrastructure —
.csprojtemplates,agents.mdfiles, CI configs
See the full Common Mistakes section below for code-level prohibitions with examples.
- Never use
varfor variable declarations - Never use 0-based indexing for
Pages,Annotations, orEmbeddedFiles - Never use unqualified type names for
Rectangle,Color,Path,Image,Matrix,Point - Never use
Aspose.Pdf.Savingnamespace (it does not exist) - Never mix
Aspose.Pdf.LogicalStructureandAspose.Pdf.Structurenamespaces - Never modify
agents.mdfiles — they are auto-generated - Never modify the
.csprojtemplate — it is generated
These are verified mistakes that cause build failures. Never use the wrong patterns.
BorderInfo does not have settable .Color or .Width properties. Use the constructor instead: new BorderInfo(BorderSide.All, (float)width, color). Pass BorderSide, width as float, and Aspose.Pdf.Color t
// WRONG
BorderInfo b = new BorderInfo();
b.Color = Aspose.Pdf.Color.Blue; // CS1061
b.Width = 1.5; // CS1061// CORRECT
Aspose.Pdf.Color borderColor = Aspose.Pdf.Color.Blue;
float borderWidth = 1.5f;
cell.Border = new BorderInfo(BorderSide.All, borderWidth, borderColor);
// Width-only variant:
cell.Border = new BorderInfo(BorderSide.All, 1f);
// Cast from double if needed:
cell.Border = new BorderInfo(BorderSide.All, (float)someDoubleWidth, Aspose.Pdf.Color.Green);The Aspose.Pdf.Rotation enum values use the 'on' prefix, NOT 'Rotate'. The correct values are: Rotation.None, Rotation.on90, Rotation.on180, Rotation.on270, Rotation.on360. Using 'Rotate90', 'Rotate18
// WRONG
page.Rotate = Rotation.Rotate180; // CS0117
page.Rotate = Rotation.Rotate90; // CS0117// CORRECT
page.Rotate = Rotation.on180;
page.Rotate = Rotation.on90;
page.Rotate = Rotation.on270;When a type name can resolve to multiple namespaces (especially with Aspose.Pdf, Aspose.Pdf.Drawing, System.IO, and System.Drawing), ALWAYS use fully qualified names for ANY type that exists in more t
// WRONG
Rectangle rect = new Rectangle(100, 500, 300, 550); // CS0104
Path.GetFileName("input.pdf"); // CS0104
Image img = Image.FromFile("photo.jpg"); // CS0104
Color c = Color.Blue; // CS0104// CORRECT
Aspose.Pdf.Rectangle pageRect = new Aspose.Pdf.Rectangle(100, 500, 300, 550);
Aspose.Pdf.Drawing.Rectangle shapeRect = new Aspose.Pdf.Drawing.Rectangle(100, 500, 200, 100);
System.IO.Path.GetFileName("input.pdf");
Aspose.Pdf.Image pdfImg = new Aspose.Pdf.Image();
System.Drawing.Image sysImg = System.Drawing.Image.FromFile("photo.jpg");
Aspose.Pdf.Color pdfColor = Aspose.Pdf.Color.Blue;
System.Drawing.Color sysColor = System.Drawing.Color.Blue;
Aspose.Pdf.Point pdfPoint = new Aspose.Pdf.Point(100, 200);
// ...Aspose.Pdf annotation collections use 1-based indexing, same as Pages. page.Annotations[1] is the first annotation. page.Annotations[0] throws an exception. page.Annotations.Delete(index) also expects
// WRONG
page.Annotations[0]; // throws IndexOutOfRangeException
page.Annotations.Delete(page.Annotations.Count - 1); // deletes second-to-last// CORRECT
for (int i = 1; i <= page.Annotations.Count; i++)
{
Annotation ann = page.Annotations[i];
}
page.Annotations.Delete(page.Annotations.Count);The 'Title' property belongs to MarkupAnnotation, NOT the base Annotation class. When iterating page.Annotations or using FindByName(), the returned type is Annotation which does not have Title. You m
// WRONG
Annotation ann = page.Annotations[1];
Console.WriteLine(ann.Title); // CS1061// CORRECT
Annotation ann = page.Annotations[1];
string title = ann is MarkupAnnotation markup ? markup.Title : "N/A";
Console.WriteLine($"Title={title}, Contents={ann.Contents}");
Annotation retrieved = page.Annotations.FindByName("MyNote");
if (retrieved is MarkupAnnotation ma)
{
Console.WriteLine(ma.Title);
}The Border class has no parameterless constructor — it requires an Annotation parent argument: new Border(annotation). Border also has NO Color property; border color is controlled by the annotation's
// WRONG
var ann = new TextAnnotation(page, rect)
{
Border = new Border { Width = 1, Color = Aspose.Pdf.Color.Black }
};
Errors: CS1729, CS1061
var ann = new TextAnnotation(page, rect)
...// CORRECT
TextAnnotation ann = new TextAnnotation(page, rect)
{
Title = "Note",
Contents = "Hello",
Color = Aspose.Pdf.Color.Yellow
};
ann.Border = new Border(ann) { Width = 1 };Always use fully qualified Aspose.Pdf.Rectangle and Aspose.Pdf.Color to avoid ambiguity with System.Drawing types. Even if System.Drawing is not explicitly referenced, implicit usings or transitive de
// WRONG
Rectangle rect = new Rectangle(100, 500, 300, 550);
txtAnn.Color = Color.Yellow;// CORRECT
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(100, 500, 300, 550);
txtAnn.Color = Aspose.Pdf.Color.Yellow;The MhtSaveOptions class is not in the Aspose.Pdf namespace — it resides in Aspose.Pdf.Facades. You must add 'using Aspose.Pdf.Facades;' and ensure the Aspose.PDF.dll reference includes the Facades as
// WRONG
using Aspose.Pdf; // alone is insufficient
MhtSaveOptions mhtOptions = new MhtSaveOptions(); // ❌ CS0246// CORRECT
using Aspose.Pdf;
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Facades; // ← Required for MhtSaveOptions
MhtSaveOptions mhtOptions = new MhtSaveOptions
{
PartsEmbeddingMode = MhtSaveOptions.PartsEmbeddingModes.EmbedAllIntoMht
};The enum 'CaretAnnotationSymbol' does not exist in Aspose.Pdf.Annotations. The 'Symbol' property of 'CaretAnnotation' expects a value of type 'Aspose.Pdf.Annotations.CaretSymbol', which is an enum def
// WRONG
Symbol = CaretAnnotationSymbol.Insert // ❌ enum does not exist// CORRECT
using Aspose.Pdf.Annotations;
...
CaretAnnotation caret = new CaretAnnotation(page, rect)
{
Symbol = CaretSymbol.Insert,
Contents = "...",
Color = Aspose.Pdf.Color.Red
};The TeXFragment class does not have a 'Margin' property; instead, use 'TopMargin', 'BottomMargin', etc. Also, 'Color' is ambiguous between System.Drawing.Color and Aspose.Pdf.Color — use fully qualifi
// WRONG
TeXFragment texFragment = new TeXFragment(...);
texFragment.Margin = new Margin { Top = 20, Bottom = 20 }; // ❌ No such property
DefaultAppearance appearance = new DefaultAppearance(..., Color.Black); // ❌ ambiguous// CORRECT
TeXFragment texFragment = new TeXFragment(@"\frac{a}{b} = c", true);
texFragment.HorizontalAlignment = HorizontalAlignment.Center;
texFragment.TopMargin = 20;
texFragment.BottomMargin = 20;
DefaultAppearance appearance = new DefaultAppearance("Helvetica", 12, Aspose.Pdf.Color.Black);Cross-cutting rules and API-specific gotchas.
- Create a new {doc} and add a {page} via {doc}.Pages.Add(). (Applies to: Graphs, Working-Document)
- Set {table}.ColumnWidths to a space‑separated string of column widths (e.g., "40 100 100") and assign {table}.DefaultCellBorder = new BorderInfo(BorderSide.All, {float}) to apply a uniform border to all cells. (Applies to: Tables)
- Populate the table from a System.Data.DataTable using {table}.ImportDataTable({data_table}, true, 0, 0, {int}, {int}) where the last two integers represent the total number of rows (including header) and the number of columns. (Applies to: Tables)
- Iterate over {row}.Cells to customize appearance: set {cell}.BackgroundColor, {cell}.DefaultCellTextState.Font, {cell}.DefaultCellTextState.ForegroundColor, and {cell}.DefaultCellTextState.HorizontalAlignment. (Applies to: Tables)
- Apply distinct styling to the header row (row index 0) versus data rows (row index >= 1) by using separate loops or conditional logic. (Applies to: Tables)
- Create a TextFragmentAbsorber with a {string_literal} search phrase and apply it to a specific {page} of a {doc} via page.Accept(absorber). (Applies to: Text)
- Iterate over absorber.TextFragments; for each {text_fragment}, set Text = {string_literal} and adjust TextState properties: Font = FontRepository.FindFont({font}), FontSize = {float}, ForegroundColor = Color.FromRgb({color}), BackgroundColor = Color.FromRgb({color}). (Applies to: Text)
# Create a new project (if needed)
dotnet new console -n ExampleProject --framework net10.0
# Add Aspose.PDF NuGet package
dotnet add package Aspose.PDF --version 26.3.0
# Build
dotnet build --configuration Release --verbosity minimal
# Run
dotnet run<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspose.PDF" Version="26.3.0" />
</ItemGroup>
</Project>- .NET SDK: 10.0 or higher
- NuGet: Aspose.PDF 26.3.0
- All examples are standalone Console Applications
- Each
.csfile can be compiled and run independently
Every example must pass these verification steps.
dotnet build --configuration Release --verbosity minimal- Success: Exit code 0, no
CSerror codes in output - Failure: Any
error CS####line indicates a build failure
dotnet run- Success: Exit code 0, no unhandled exception stack traces
- Failure:
Unhandled exception,System.Exception, or non-zero exit code
- Console output confirming the operation (e.g., "PDF saved successfully")
- Output files created in the working directory (e.g.,
output.pdf) - No
NullReferenceException,IndexOutOfRangeException, orFileNotFoundException
| Code | Meaning | Fix |
|---|---|---|
CS0104 |
Ambiguous type reference | Use fully qualified name (Aspose.Pdf.Rectangle) |
CS1061 |
Member does not exist on type | Check API docs for correct property/method |
CS0246 |
Type or namespace not found | Add missing using directive |
CS0029 |
Cannot convert type | Cast explicitly or use correct type |
- .NET SDK (10.0 or higher)
- Aspose.PDF for .NET (26.3.0 or higher)
- NuGet package restore enabled
- Navigate to any category folder
- Each .cs file is a standalone Console Application
- Ensure
input.pdfexists in the same directory (where required) - Compile and run:
dotnet run <example-file.cs>
Updated: 2026-04-10 | Run: 20260410_174139_d90957 | Examples: 2708 | Categories: 34
This repository is maintained by automated code generation. Last updated: 2026-04-10 | Total examples: 2708