ユースケース

ユースケース

このガイドでは、Aspose.PDF FOSS for TypeScript の実際の使用例を示します。文書生成、レビュー ワークフロー、赤字処理、セキュアな配布、フォームベースのデータ収集、タグ付きウェブ公開が含まれます。Node.js 22 以降が必要です。

文書生成とページ組み立て

指定された PageFormat で空白の Document を開始し、Page.AddText() でテキストを配置し、さらにページを追加して PDF をゼロから構築します。このパターンは請求書生成、証明書印刷、レポート作成パイプラインの基盤となります:

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');

配布前の機微情報の赤字処理

組織から文書が出る前に、機密フィールド(従業員ID、銀行口座番号など)にマークを付け、破壊的に削除します:

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);
}

タグ付けされた、Web対応文書の公開

文書の論理構造にタグを付け、次にそれをウェブリーダーで正しくリフローするセマンティック 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');

参照

 日本語