การจัดการเอกสารหลัก
การจัดการเอกสารหลัก
คลาส Document เป็นจุดเริ่มต้นหลักสำหรับ Aspose.PDF FOSS สำหรับ .NET.
It implements IDisposable และควรใช้ในบล็อก using เพื่อให้แน่ใจว่าทรัพยากรทั้งหมดจะถูกปล่อยออกอย่างทันท่วงที.
สร้างเอกสาร PDF ใหม่
ใช้ Document.Create() เพื่อสร้างเอกสาร PDF ว่าง:
using Aspose.Pdf;
using var doc = Document.Create();
doc.Pages.Add();
doc.Save("new-document.pdf");หรืออีกทางหนึ่ง ให้สร้างอินสแตนซ์ Document โดยตรงด้วย new Document():
using Aspose.Pdf;
using var doc = new Document();
doc.Pages.Add();
doc.Save("new-document.pdf");การเปิดเอกสาร PDF ที่มีอยู่
Document.Open รับ byte[], เส้นทางไฟล์ string, หรือ Stream.
เปิดจากอาร์เรย์ไบต์:
using Aspose.Pdf;
byte[] data = File.ReadAllBytes("input.pdf");
using var doc = Document.Open(data);
Console.WriteLine($"Pages: {doc.Pages.Count}");เปิดจากเส้นทางไฟล์:
using Aspose.Pdf;
using var doc = Document.Open("input.pdf");
Console.WriteLine($"Pages: {doc.Pages.Count}");เปิดจาก Stream:
using Aspose.Pdf;
using var stream = File.OpenRead("input.pdf");
using var doc = Document.Open(stream);
Console.WriteLine($"Pages: {doc.Pages.Count}");การเปิด PDF ที่มีการป้องกันด้วยรหัสผ่าน
ส่งรหัสผ่านเป็นอาร์กิวเมนต์ที่สองให้กับการโอเวอร์โหลดใด ๆ ของ Document.Open:
using Aspose.Pdf;
using var doc = Document.Open("protected.pdf", "mypassword");กำลังบันทึกเอกสาร
Document.Save เขียนเอกสารปัจจุบันไปยังเส้นทางไฟล์หรือสตรีม.Document.ToArray คืนค่าไบต์ที่ถูกซีเรียลไลซ์โดยตรง.
บันทึกลงไฟล์:
using Aspose.Pdf;
using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
doc.Pages.Add();
doc.Save("output.pdf");บันทึกการเดินทางรอบกลับไปยัง MemoryStream (จาก 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());แปลงเป็นอาร์เรย์ไบต์:
using Aspose.Pdf;
using var doc = Document.Open(File.ReadAllBytes("input.pdf"));
var bytes = doc.ToArray();
File.WriteAllBytes("output.pdf", bytes);การเข้าถึงหน้า
หน้าถูกเข้าถึงผ่าน Document.Pages ซึ่งเป็น PageCollection ที่เริ่มจาก 1. ใช้ดัชนีจำนวนเต็มเพื่อรับ 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}");การเข้าถึงแบบฟอร์ม
ฟิลด์ Interactive AcroForm สามารถเข้าถึงได้ผ่าน Document.Form. ตรวจสอบ null ก่อนเข้าถึงเมื่อเอกสารอาจไม่มีฟอร์ม:
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}");
}
}การทำลายเอกสาร
Document ทำการใช้งาน IDisposable. ควรใช้ using เสมอหรือเรียก Dispose() อย่างชัดเจน:
using Aspose.Pdf;
using var doc = Document.Open("input.pdf");
// doc is disposed automatically at the end of the using block.