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 Document in a using statement 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 Save only once after all modifications — repeated saves are slower but safe.
  • Use PageCollection.Accept(AnnotationSelector) to batch-process annotations across all pages.

Common Issues

IssueCauseFix
Document.Open throws on valid PDFFile is encrypted with a passwordSupply the password parameter in the constructor
Page count is 0 after constructionNew Document() starts emptyCall doc.Pages.Add() to add a blank page
Saved file is larger than expectedImages or fonts embedded multiple timesUse optimization (see Conversion and Optimization guide)
Pages[0] throws index errorPages are 1-based, not 0-basedUse 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 / MethodDescription
DocumentRoot PDF object; holds pages, metadata, and form fields
Document.OpenStatic factory to open a PDF from a byte array
Document.SaveSerialize the document to a file or stream
PageSingle PDF page with content, annotations, and geometry
Page.SetMediaBoxDefine the page media box
Page.SetCropBoxDefine the visible crop area
Page.SetRotationRotate the page by 0, 90, 180, or 270 degrees
PageCollectionOrdered collection of pages in a document
PageCollection.AddAppend a blank page
OperatorCollectionRaw content-stream operators for a page
FileSpecificationEmbedded file attachment descriptor
ImageStampImage overlay stamp for pages
XFormCollectionReusable form XObjects in the document

See Also

 English