Document Management
Document Management
Document is the root object in Aspose.PDF FOSS for .NET. It represents an
entire PDF file and provides access to pages, metadata, form fields, and all
other PDF structures. Open existing files with Document.Open and create new
ones with the Document constructor.
Opening and creating documents
// Open from a byte array
using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
// Open from a file path
using var doc2 = new Document("input.pdf");
// Create a new empty document
using var newDoc = new Document();
newDoc.Pages.Add();
newDoc.Save("new.pdf");Working with pages
PageCollection manages the document’s pages. Access pages by 1-based index,
add new pages, or remove existing ones.
using var doc = Document.Open(pdfBytes);
// Access first page
var page = doc.Pages[1];
// Add a blank page
var newPage = doc.Pages.Add();
// Get page count
int count = doc.Pages.Count;Page geometry
Each page defines several bounding boxes. Use the setter methods on Page to
adjust them.
var page = doc.Pages[1];
page.SetMediaBox(new Rectangle(0, 0, 612, 792));
page.SetCropBox(new Rectangle(36, 36, 576, 756));
page.SetRotation(90);Saving documents
Save to a file path, a stream, or a byte array.
// Save to file
doc.Save("output.pdf");
// Save to stream
using var ms = new MemoryStream();
doc.Save(ms);File attachments
FileSpecification represents embedded file attachments in a PDF.
var spec = new FileSpecification("data.csv", "Embedded data file");
doc.EmbeddedFiles.Add(spec);Image stamps
ImageStamp overlays a raster image on a page at a specified position.
var stamp = new ImageStamp("logo.png");
stamp.Put(doc.Pages[1]);
doc.Save("stamped.pdf");Operator collections
OperatorCollection on each page holds the raw content-stream operators. Use
it for low-level content inspection or manipulation.
var ops = doc.Pages[1].Contents;
foreach (var op in ops)
{
// Inspect each operator
}Tips and Best Practices
- Wrap
Documentin ausingstatement to release file handles promptly. - Use
Document.Open(byte[])for in-memory workflows to avoid file-lock issues. - Modify page boxes (MediaBox, CropBox) before adding content to ensure correct coordinate space.
- Call
Saveonly once after all modifications — repeated saves are slower but safe. - Use
PageCollection.Accept(AnnotationSelector)to batch-process annotations across all pages.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
Document.Open throws on valid PDF | File is encrypted with a password | Supply the password parameter in the constructor |
| Page count is 0 after construction | New Document() starts empty | Call doc.Pages.Add() to add a blank page |
| Saved file is larger than expected | Images or fonts embedded multiple times | Use optimization (see Conversion and Optimization guide) |
Pages[0] throws index error | Pages are 1-based, not 0-based | Use Pages[1] for the first page |
FAQ
How do I merge two PDF documents?
Use PdfFileEditor.Concatenate from the Facades API, or manually copy pages
from one document to another using PageCollection.
Can I open a PDF from a URL or stream?
Yes. Read the URL content into a byte[] first, then pass it to
Document.Open(byte[]).
How do I delete a page?
Call doc.Pages.Delete(pageNumber) where pageNumber is 1-based.
Is Document thread-safe?
No. Each thread should work with its own Document instance.
API Reference Summary
| Class / Method | Description |
|---|---|
Document | Root PDF object; holds pages, metadata, and form fields |
Document.Open | Static factory to open a PDF from a byte array |
Document.Save | Serialize the document to a file or stream |
Page | Single PDF page with content, annotations, and geometry |
Page.SetMediaBox | Define the page media box |
Page.SetCropBox | Define the visible crop area |
Page.SetRotation | Rotate the page by 0, 90, 180, or 270 degrees |
PageCollection | Ordered collection of pages in a document |
PageCollection.Add | Append a blank page |
OperatorCollection | Raw content-stream operators for a page |
FileSpecification | Embedded file attachment descriptor |
ImageStamp | Image overlay stamp for pages |
XFormCollection | Reusable form XObjects in the document |