C#でPDFを作成する方法

このハウツーガイドでは、** C#でPDFを作成する方法について説明しています**。 .NETベースのアプリケーションのいくつかのAPI呼び出しで以下に示す簡単な手順に従うことで、** C#からPDFを生成**できます。

C#でPDFを作成する手順

  1. NuGetパッケージマネージャーを使用してAspose.PDF for .NETをインストールします
  2. アプリケーションにAspose.PDF参照を含める
  3. Documentクラスのインスタンスを作成して、空のPDFを作成します
  4. TextFragmentクラスのインスタンスを作成して、テキストとそのプロパティを追加します
  5. 最後に、ディスクに保存してC#を使用してPDFを作成します

次の例は、* C#でPDFを生成する方法を説明しています。 * Document Class を使用して空白のPDFを作成し、その中にページを追加する方法を説明します。次に、 TextBuilderクラスを使用してテキストを追加し、それぞれのプロパティを設定します。最後に、テキストがPDFに追加されます。

C#からPDFを作成するためのコード

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Aspose.Pdf;
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Devices;
using Aspose.Pdf.Facades;
using Aspose.Pdf.Forms;
using Aspose.Pdf.Text;
namespace TestPDF
{
class Program
{
static void Main(string[] args)
{
// Applying product license to create PDF in C#
License lic = new License();
lic.SetLicense("Total.Product.Family.lic");
// Initialize document object generate PDF from C#
Document document = new Document();
// Insert page in PDF
Page pdfPage = document.Pages.Add();
// Create instance of Text fragment
TextFragment textFragment = new TextFragment("Knowledgebase Text");
// Set textual properties
textFragment.Position = new Position(100, 600);
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
// Initialize TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);
// Append added fragment to the PDF page
textBuilder.AppendText(textFragment);
// Create PDF using C#
document.Save("Generated_out.pdf");
}
}
}

上記のサンプルコードでは、Adobe PDFやその他のAPIに依存せずにC#でPDFファイルを作成する方法を観察しました。いくつかの簡単なAPI呼び出しを使用して、テキストを追加し、それぞれのプロパティを設定することで、PDFを最初から作成しました。

前のトピックでは、C#でPDFを読むの方法を学びました。一方、上記の説明と* C#のサンプルコードは、プログラムでPDFファイル*を作成します。

 日本語