Core Document Management
Core Document Management
The Document class is the main entry point for Aspose.PDF FOSS for .NET.
It implements IDisposable and should be used in a using block to ensure
all resources are released promptly.
Creating a new PDF document
Use Document.Create() to instantiate an empty PDF document:
using Aspose.Pdf;
using var doc = Document.Create();
doc.Pages.Add();
doc.Save("new-document.pdf");Alternatively, construct a Document instance directly with new Document():
using Aspose.Pdf;
using var doc = new Document();
doc.Pages.Add();
doc.Save("new-document.pdf");Opening an existing PDF document
Document.Open accepts a byte[], a file path string, or a Stream.
Open from a byte array:
using Aspose.Pdf;
byte[] data = File.ReadAllBytes("input.pdf");
using var doc = Document.Open(data);
Console.WriteLine($"Pages: {doc.Pages.Count}");Open from a file path:
using Aspose.Pdf;
using var doc = Document.Open("input.pdf");
Console.WriteLine($"Pages: {doc.Pages.Count}");Open from a Stream:
using Aspose.Pdf;
using var stream = File.OpenRead("input.pdf");
using var doc = Document.Open(stream);
Console.WriteLine($"Pages: {doc.Pages.Count}");Opening a password-protected PDF
Pass the password as a second argument to any Document.Open overload:
using Aspose.Pdf;
using var doc = Document.Open("protected.pdf", "mypassword");Saving a document
Document.Save writes the current document to a file path or stream.
Document.ToArray returns the serialised bytes directly.
Save to a file:
using Aspose.Pdf;
using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
doc.Pages.Add();
doc.Save("output.pdf");Save round-trip to a MemoryStream (from snippet_014.cs):
using Aspose.Pdf;
var data = File.ReadAllBytes("input.pdf");
using var doc = Document.Open(data);
var page = doc.Pages[1];
var action = PdfAction.CreateJavaScript("app.alert('Round trip');");
page.Annotations.AddLinkAnnotation(new Rectangle(50, 600, 200, 620), action);
using var ms = new MemoryStream();
doc.Save(ms);
ms.Position = 0;
using var doc2 = Document.Open(ms.ToArray());Serialise to a byte array:
using Aspose.Pdf;
using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
var bytes = doc.ToArray();
File.WriteAllBytes("output.pdf", bytes);Accessing pages
Pages are accessed through Document.Pages, which is a 1-based
PageCollection. Use the integer index to get a specific Page:
using Aspose.Pdf;
using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
var page = doc.Pages[1];
Console.WriteLine($"Width: {page.Width}, Height: {page.Height}");
Console.WriteLine($"Rotation: {page.RotateDegrees}");
Console.WriteLine($"Annotations: {page.Annotations.Count}");Accessing the form
Interactive AcroForm fields are available through Document.Form. Check for
null before accessing when the document may not contain a form:
using Aspose.Pdf;
using var doc = Document.Open(File.ReadAllBytes("form.pdf"));
if (doc.Form is not null)
{
foreach (var field in doc.Form.Fields)
{
Console.WriteLine($"{field.Name}: {field.Value}");
}
}Disposing a document
Document implements IDisposable. Always use using or call Dispose()
explicitly:
using Aspose.Pdf;
using var doc = Document.Open("input.pdf");
// doc is disposed automatically at the end of the using block.