กรณีการใช้งาน
คู่มือนี้แสดงกรณีการใช้งานจริงสำหรับ Aspose.PDF FOSS สำหรับ TypeScript, ครอบคลุมการสร้างเอกสาร, กระบวนการตรวจทาน, การลบข้อมูลที่เป็นความลับ, การกระจายอย่างปลอดภัย, การรวบรวมข้อมูลแบบฟอร์ม, และการเผยแพร่เว็บที่มีแท็ก. จำเป็นต้องใช้ Node.js 22 หรือใหม่กว่า.
การสร้างเอกสารและการประกอบหน้า
สร้าง PDF ตั้งแต่ต้นโดยเริ่มต้น Document ว่างที่ PageFormat, วางข้อความด้วย Page.AddText(), และต่อหน้าต่อไป. รูปแบบนี้เป็นพื้นฐานของการสร้างใบแจ้งหนี้, การพิมพ์ใบรับรอง, และกระบวนการผลิตรายงาน:
import { Document, PageFormat } from '@asposefoss/pdf';
const doc = Document.New(PageFormat.A4); // one blank A4 page
doc.Pages[0].AddText('Monthly Report', 72, 720, { fontSize: 14 });
doc.AddPage(PageFormat.A4.landscape()); // append more as you go
doc.WriteTo('report.pdf');การตรวจทานและการทำเครื่องหมายแก้ไขด้วยคำอธิบายประกอบ
ทำเครื่องหมายบนร่างที่แชร์ด้วยการไฮไลท์, โน้ตสีสัน, และความคิดเห็นแบบข้อความอิสระ, จากนั้นบันทึกการตรวจทานเป็นหน้าสุดท้ายที่ไม่สามารถแก้ไขได้เมื่อได้รับการอนุมัติ:
const draft = Document.OpenFile('draft.pdf');
const page = draft.Pages[0];
page.AddHighlight({ quads: [72, 700, 300, 700, 72, 685, 300, 685], color: [1, 1, 0] });
const note = page.AddFreeText({
rect: [320, 685, 500, 715],
contents: 'Please confirm this figure before publishing.',
fontSize: 10,
});
// Once the review is resolved, bake the comment into static content:
note.Flatten();
draft.WriteTo('reviewed.pdf');การลบข้อมูลที่เป็นความลับก่อนการกระจาย
ทำเครื่องหมายและจากนั้นลบฟิลด์ที่ละเอียดอ่อนอย่างทำลายล้าง — หมายเลขประจำพนักงาน, หมายเลขบัญชีธนาคาร — ก่อนที่เอกสารจะออกจากองค์กร:
const memo = Document.OpenFile('internal-memo.pdf');
const page2 = memo.Pages[0];
page2.AddRedact({
rect: [220, 548, 460, 566],
fill: [0, 0, 0],
overlayText: '[REDACTED]',
align: 'center',
fontSize: 9,
textColor: [1, 1, 1],
});
page2.ApplyRedactions(); // destructive — the covered content is gone
memo.WriteTo('memo-for-distribution.pdf');การรักษาความปลอดภัยและการลงนามเอกสารเพื่อการแจกจ่าย
รับรองเอกสารที่เสร็จสมบูรณ์ จากนั้นเข้ารหัสด้วยใบรับรองของผู้รับเฉพาะเพื่อให้ผู้รับคนนั้นเท่านั้นที่สามารถเปิดได้:
async function secureAndDistribute(sourcePath: string): Promise<void> {
const doc = Document.OpenFile(sourcePath);
await doc.Certify(
{ certificate: authorCert, privateKey: authorKey },
{ permissions: 'form-fill', reason: 'Certifying the final report' },
);
const encryptedBytes = doc.Save({
encrypt: {
recipients: [{ certificate: recipientCertPem }],
algorithm: 'aes256',
permissions: { copying: false },
},
});
writeFileSync('report-secure.pdf', encryptedBytes);
}การเก็บรวบรวมข้อมูลเชิงโครงสร้างด้วยแบบฟอร์มที่กรอกได้
สร้างแบบฟอร์มรับข้อมูลที่สามารถกรอกได้ จากนั้นอ่านค่าที่ส่งกลับจากสำเนาที่คืนคืน — มีประโยชน์สำหรับการประมวลผลแบบฟอร์มสมัครที่กรอกแล้วโดยไม่ต้องใช้เซิร์ฟเวอร์ส่งแบบฟอร์มแยกต่างหาก:
const template = Document.New(PageFormat.A4);
const form = template.Form;
const pageNum = template.Pages[0].Number;
form.AddTextField({ page: pageNum, rect: [200, 670, 450, 690], name: 'FullName' });
form.AddCheckbox({ page: pageNum, rect: [200, 630, 218, 648], name: 'Subscribe' });
template.WriteTo('intake-form.pdf');
// Later, after the form comes back filled in:
const submitted = Document.OpenFile('intake-form-filled.pdf');
for (const field of submitted.Form.Fields) {
console.log(field.FullName, '=', field.Value);
}การเผยแพร่เอกสารที่มีการแท็กและพร้อมใช้งานบนเว็บ
ทำการแท็กโครงสร้างเชิงตรรกะของเอกสาร จากนั้นส่งออกเป็น HTML เชิงความหมายที่ปรับการไหลได้อย่างถูกต้องสำหรับผู้อ่านบนเว็บ — การส่งออกจะเดินตามต้นไม้โครงสร้างเดียวกันที่ขั้นตอนการแท็กสร้างขึ้น:
const source = Document.OpenFile('report.pdf');
const report = source.AutoTag({ title: true });
console.log(`tagged: ${report.headings} heading(s), ${report.paragraphs} paragraph(s)`);
const html = source.ToHtml(); // reflowable markup, built from the structure tree
writeFileSync('report.html', html, 'utf8');