この記事では、C# を使用して PDF ドキュメントを編集する方法について説明します。これには、開発環境を設定するための情報、タスクを実行するために必要な手順のリスト、および C#** を使用して **PDF エディター アプリケーションを開発するための実行可能なサンプル コードが含まれています。 PDF ファイルの変更を実行するために必要なクラス、メソッド、プロパティについても説明します。
C# を使用して PDF ファイルを変更する手順
- 編集に Aspose.PDF for .NET を使用するように IDE を構成します
- PdfContentEditor クラスのオブジェクトを作成して、ファイル全体のテキストを置き換えます
- 前景色とフォント サイズの設定とともに他のテキストを置換し、メモリ ストリームに保存します
- ストリームを PdfFileMend オブジェクトにロードしてテキストを追加し、メモリ ストリームに保存し直します
- テキストを含むページを追加するために、結果のメモリ ストリームを Document クラスにロードします。
- 上記のすべての変更を加えて、最終的な PDF ファイルをディスクに保存します。
これらの手順では、C# を使用して PDF ドキュメントを変更するプロセスを示します。 PdfContentEditor オブジェクトを使用して、ファイル全体のテキストを置き換えたり、テキストを異なる前景色やフォントで置き換えたりできます。 PdfFileMend クラスは既存のページにテキストを追加するために使用され、Document クラスは新しいページを追加してそのページにテキストをレンダリングするために使用されます。
C# を使用して PDF ファイルを変更するコード
using Aspose.Pdf; | |
using Aspose.Pdf.Facades; | |
using Aspose.Pdf.Text; | |
class Program | |
{ | |
static void Main(string[] args) // Edit PDF in C# | |
{ | |
new License().SetLicense("License.lic"); | |
// Create PdfFileEditor object | |
PdfContentEditor editor = new PdfContentEditor(); | |
editor.BindPdf("Input.pdf"); | |
// Replace some text in the entire file | |
while (true) | |
if (editor.ReplaceText("scenario", "situation") == false) | |
break; | |
// Replace some text and change its font and color | |
TextState textState = new TextState(); | |
textState.ForegroundColor = Color.Red; | |
textState.FontSize = 14; | |
while (true) | |
if (editor.ReplaceText("attack", "fight", textState) == false) | |
break; | |
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); | |
editor.Save(memoryStream); | |
// Add text to an existing page | |
PdfFileMend mendor = new PdfFileMend(); | |
memoryStream.Position = 0; | |
mendor.BindPdf(memoryStream); | |
FormattedText message = new FormattedText("Test message on the page"); | |
mendor.AddText(message, 2, 60, 300); | |
mendor.Save(memoryStream); | |
// Add a paragraph with some text on a new page | |
memoryStream.Position = 0; | |
Document document = new Document(memoryStream); | |
Page page = document.Pages.Add(); | |
page.Paragraphs.Add(new TextFragment("New paragraph is added")); | |
// Save the output | |
document.Save("output.pdf"); | |
System.Console.WriteLine("Done"); | |
} | |
} |
このコードは、C#* を使用した *PDF 修飾子の開発を示しています。さまざまなクラスを使用して内容を変更し、中間結果を一時メモリ ストリームに保存して、別のタスクを実行するために次のクラスにロードします。 PDF ファイルの変更には、PdfPageEditor、PdfFileStamp、PdfFileSignature、PdfConverter、PdfBookmarkEditor など、さまざまなクラスが使用できます。
この記事では、C#* を使用したソフトウェア *PDF エディターの動作について説明しました。 PDF ファイルの背景を変更するプロセスについて知りたい場合は、C#を使用してPDFの背景色を編集する方法 の記事を参照してください。