TypeScriptでCore PDF APIを使用する方法
このガイドでは、TypeScript 用の Aspose.PDF FOSS を使用して PDF ドキュメントを開く、作成する、編集する、分割する、保存する方法を示します。Document と Page クラスは、ライブラリ内のすべての操作のエントリーポイントであり、ファイルのオープンと作成、ページのインプレイス編集、ドキュメントの分割と結合、結果の保存を行います。必要なのは Node.js 22 以降だけです。
ステップバイステップガイド
ステップ 1: パッケージをインストールする
git clone https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-TypeScript.git
cd Aspose.PDF-FOSS-for-TypeScript
npm install
npm run buildパッケージがインストールされたら、TypeScript ファイル内で Document クラスをインポートしてインストールを確認してください — この行はエラーなく解決されるはずです:
import { Document } from '@asposefoss/pdf';ステップ 2: 必要なクラスをインポートする
PDF を読み込むまたは作成するには Document をインポートし、ドキュメントを最初から開始する場合は PageFormat をインポートしてください:
import { Document, PageFormat } from '@asposefoss/pdf';ステップ 3: 既存の PDF を開いてページを編集する
Document.OpenFile() はファイルパスから PDF を開きます; Document.Open() はインメモリ Uint8Array を受け取ります。ページはその場で検査・編集でき、Document.WriteTo() が結果を書き出します。
import { Document } from '@asposefoss/pdf';
// Open from disk (or Document.Open(uint8array) for in-memory data)
const doc = Document.OpenFile('input.pdf');
// Inspect and edit pages
const pages = doc.Pages;
console.log(pages.length);
pages[0].Rotate = 90;
doc.RemovePage(2); // 1-based page number
doc.Reorder([3, 1, 2]);
// Save
doc.WriteTo('output.pdf'); // or: const bytes = doc.Save();
ステップ 4: ゼロから新しい PDF を作成する
Document.New() は指定された PageFormat で空白ドキュメントを開始します。Page.AddText() は位置指定テキストを配置し、Document.AddPage() はさらにページを追加します。
import { Document, PageFormat } from '@asposefoss/pdf';
const doc = Document.New(PageFormat.A4); // one blank A4 page
doc.Pages[0].AddText('Hello', 72, 720, { fontSize: 14 });
doc.AddPage(PageFormat.A4.landscape()); // append more as you go
doc.WriteTo('scratch.pdf');ステップ 5: ページの分割、抽出、結合
Document.Split() はページごとに 1 つの Document を返します。Document.ExtractPages() は 1 ベースで繰り返しを許可したページリストを受け取り、該当ページだけの新しい Document を返します。Document.Append() と Document.InsertPage() はドキュメント間でページをコピーし、Document.Merge() は複数のドキュメントを 1 つに結合します。
const parts = doc.Split(); // one Document per page
const chapter = doc.ExtractPages([3, 4, 5]); // subset (1-based, repeats allowed) as a new Document
docA.Append(docB); // copy all of docB's pages onto docA
docA.InsertPage(1, docB.Pages[0]); // copy a single page across documents
一般的な問題と解決策
Document.OpenFile() throws InvalidPasswordError. PDFはパスワードで保護されており、パスワードが提供されていない(または間違っている)です。Pass { password: '...' } to Document.OpenFile() / Document.Open().
RemovePage() or ExtractPages() 誤ったページを削除または抽出します。 これらのメソッドは1ベースのページ番号を受け取りますが、 doc.Pages は単純な0ベース配列です — 2つのインデックス方式は混同しやすいです。
WriteTo() 出力が予想より大きくなります。 デフォルトの保存ではxref/object-stream圧縮が使用されません。Pass { compressed: true } to Document.WriteTo() 有効にするには.
に対して行われた編集 doc.Pages[i] 保存されたファイルには表示されません。 ページの編集は呼び出しの前に行う必要があります。 Document.WriteTo() / Document.Save() — 別個の “commit” ステップはありませんが、保存後に行われた編集は、すでにディスクに書き込まれたファイルには反映されません。
よくある質問
ファイルパスではなく、メモリバッファから PDF をロードするにはどうすればよいですか?
パスではなく、Uint8Array を Document.Open() に渡してください(Document.OpenFile() へのパスの代わりに)。
ページを回転させるにはどうすればよいですか?
page.Rotate を 90、180、または 270 に設定し、Document.WriteTo() でドキュメントを保存します。
1つのドキュメントから別のドキュメントへ単一のページをコピーできますか?
はい — Document.InsertPage(at, sourcePage) は、指定された1ベースの位置から別のドキュメントの1ページだけをコピーし、ドキュメント全体はコピーしません。
Document.ExtractPages() は同じページが2回表示されることを許可しますか?
はい。Document.ExtractPages() に渡されるページ番号リストは重複を許可しているため、同じ元ページが結果に複数回表示されることがあります。