Podstawowe zarządzanie dokumentami
Podstawowe zarządzanie dokumentami
Klasa Document jest głównym punktem wejścia dla Aspose.PDF FOSS dla .NET. Implementuje IDisposable i powinna być używana w bloku using, aby zapewnić szybkie zwolnienie wszystkich zasobów.
Tworzenie nowego dokumentu PDF
Użyj Document.Create(), aby utworzyć pusty dokument PDF:
using Aspose.Pdf;
using var doc = Document.Create();
doc.Pages.Add();
doc.Save("new-document.pdf");Alternatywnie, skonstruuj instancję Document bezpośrednio przy użyciu new Document():
using Aspose.Pdf;
using var doc = new Document();
doc.Pages.Add();
doc.Save("new-document.pdf");Otwieranie istniejącego dokumentu PDF
Document.Open akceptuje byte[], ścieżkę pliku string lub Stream.
Otwórz z tablicy bajtów:
using Aspose.Pdf;
byte[] data = File.ReadAllBytes("input.pdf");
using var doc = Document.Open(data);
Console.WriteLine($"Pages: {doc.Pages.Count}");Otwórz z ścieżki pliku:
using Aspose.Pdf;
using var doc = Document.Open("input.pdf");
Console.WriteLine($"Pages: {doc.Pages.Count}");Otwórz z Stream:
using Aspose.Pdf;
using var stream = File.OpenRead("input.pdf");
using var doc = Document.Open(stream);
Console.WriteLine($"Pages: {doc.Pages.Count}");Otwieranie pliku PDF zabezpieczonego hasłem
Przekaż hasło jako drugi argument do dowolnego przeciążenia Document.Open:
using Aspose.Pdf;
using var doc = Document.Open("protected.pdf", "mypassword");Zapisywanie dokumentu
Document.Save zapisuje bieżący dokument do ścieżki pliku lub strumienia.Document.ToArray zwraca zserializowane bajty bezpośrednio.
Zapisz do pliku:
using Aspose.Pdf;
using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
doc.Pages.Add();
doc.Save("output.pdf");Zapisz pełny cykl do MemoryStream (z 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());Serializuj do tablicy bajtów:
using Aspose.Pdf;
using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
var bytes = doc.ToArray();
File.WriteAllBytes("output.pdf", bytes);Dostęp do stron
Strony są dostępne poprzez Document.Pages, które jest indeksowane od 1 PageCollection. Użyj indeksu całkowitego, aby uzyskać konkretny 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}");Uzyskiwanie dostępu do formularza
Interaktywne pola AcroForm są dostępne za pośrednictwem Document.Form. Sprawdź null przed dostępem, gdy dokument może nie zawierać formularza:
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}");
}
}Zwalnianie dokumentu
Document implementuje IDisposable. Zawsze używaj using lub wywołaj Dispose() jawnie:
using Aspose.Pdf;
using var doc = Document.Open("input.pdf");
// doc is disposed automatically at the end of the using block.