1. 製品
  2.   PDF
  3.   .NET
  4.   PDFsharp

PDFsharp

 
 

PDF 処理用のオープンソース .NET API

無料の C# .NET ライブラリを介して PDF ドキュメントを作成、操作、変換、処理します。

PDFシャープとは?

PDF ファイルのテキストまたはメタデータの作成、レンダリング、マージ、分割、変更、印刷、および抽出に使用できるオープン ソース .NET ライブラリ。 PDFsharp API は、任意の .NET 言語からオンザフライで PDF ドキュメントを作成することをサポートしています。また、XML ファイルまたは直接インターフェイスを介してさまざまなソースからデータをインポートすることもサポートしています。ページ レイアウト、テキストの書式設定、およびドキュメント デザインの多数のオプションをサポートしています。

PDFsharp は、GDI+ または WPF に基づくグラフィカルな実装を提供します。 API は、1 つのソース コードを使用して PDF ページだけでなく、ウィンドウやプリンターにも描画できる機能を提供することで、開発者の作業を容易にします。 PDF の変更、PDF のマージまたは分割、XPS から PDF への変換、PDF レンダリング、PDF データ抽出、フォントの埋め込みとサブセット化、Unicode サポートなど、PDF ファイルを処理するためのいくつかの重要な機能をサポートしています。

Previous Next

PDFsharp を使ってみる

PDFsharp は、AGPL/商用ソフトウェアとしてデュアル ライセンスを取得しています。 AGPL は、フリー/オープンソースのソフトウェア ライセンスです。

NuGet を使用して PDFsharp をプロジェクトに追加することを強くお勧めします。

NuGet コマンド

 Install-Package PdfSharp

Visual Studio では、NuGet パッケージ マネージャーをインストールして、NuGet パッケージに簡単にアクセスできます。 VS 2012 Express だけでなく、VS 2013 および VS 2015 のコミュニティ エディションでも動作します。Visual Studio で [ツール] => に移動します。 「拡張機能と更新プログラム...」をクリックして、NuGet パッケージ マネージャーをまだインストールしていない場合はインストールします。 NuGet パッケージ マネージャーがパッケージをダウンロードしてインストールし、プロジェクトへの参照を追加します。

無料の .NET API を介して PDF ドキュメントを生成および変更する

ソフトウェア開発者は、PDFsharp API を使用して、独自の .NET アプリケーション内で新しい PDF ドキュメントを作成できます。ドキュメントが作成されたら、空のページを追加したり、グラフィックやテキストを簡単に挿入したりできます。また、開発者が必要に応じて既存のドキュメントを変更し、選択した名前で保存することも容易になります。次の手順を使用して、C# で PDF ドキュメントを生成および操作できます。

  1. PdfDocument の初期化
  2. ページを追加
  3. 描画用の XGraphics オブジェクトを取得する
  4. フォントを作成する
  5. テキストを追加
  6. ドキュメントを保存

C# を使用して PDF を作成する

// Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
// Create an empty page
PdfPage pdfPage = pdfDocument.AddPage();
// Get an XGraphics object for drawing
XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage);
// Create a font
XFont xFont = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
xGraphics.DrawString("File Format Developer Guide", xFont, XBrushes.Black,
    new XRect(0, 0, pdfPage.Width, pdfPage.Height),
    XStringFormats.Center);
// Save the document...
pdfDocument.Save("fileformat.pdf");
    

.NET API 経由で PDF 注釈を作成

注釈を使用すると、ユーザーは PDF ページにカスタム コンテンツを追加できます。 PDF アプリケーションでは、通常、テキスト、線、メモ、図形など、さまざまな種類の注釈を作成および変更できます。PDFsharp を使用すると、プログラマは、独自のアプリケーション内でさまざまな種類の PDF 注釈を作成できます。ライブラリは、テキスト注釈、リンク、およびゴム印の注釈の作成をサポートしています。

C# を使用して PDF テキスト注釈を作成する

 // Create a PDF text annotation
PdfTextAnnotation textAnnot = new PdfTextAnnotation();
textAnnot.Title = "This is the title";
textAnnot.Subject = "This is the subject";
textAnnot.Contents = "This is the contents of the annotation.\rThis is the 2nd line.";
textAnnot.Icon = PdfTextAnnotationIcon.Note;
gfx.DrawString("The first text annotation", font, XBrushes.Black, 30, 50, XStringFormats.Default);
// Convert rectangle from world space to page space. This is necessary because the annotation is
// placed relative to the bottom left corner of the page with units measured in point.
XRect rect = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 60), new XSize(30, 30)));
textAnnot.Rectangle = new PdfRectangle(rect);
// Add the annotation to the page
page.Annotations.Add(textAnnot);

.NET 経由で複数の PDF ドキュメントを結合する

1 つの大きなドキュメントに結合する必要がある多数の PDF ドキュメントがありますか? PDFsharp API は、数行のコードで複数の PDF ファイルを 1 つに結合する機能を提供します。開発者は、既存の PDF ファイルから新しいドキュメントを簡単に作成できます。これは、視覚的な比較やその他のいくつかの重要なタスクに役立つ場合があります。

Java 経由でドキュメントを結合する

 // Open the input files
PdfDocument inputDocument1 = PdfReader.Open(filename1, PdfDocumentOpenMode.Import);
PdfDocument inputDocument2 = PdfReader.Open(filename2, PdfDocumentOpenMode.Import);
// Create the output document
PdfDocument outputDocument = new PdfDocument();
// Show consecutive pages facing. Requires Acrobat 5 or higher.
outputDocument.PageLayout = PdfPageLayout.TwoColumnLeft;
XFont font = new XFont("Verdana", 10, XFontStyle.Bold);
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Center;
format.LineAlignment = XLineAlignment.Far;
XGraphics gfx;
XRect box;
int count = Math.Max(inputDocument1.PageCount, inputDocument2.PageCount);
for (int idx = 0; idx < count; idx++)
{
// Get page from 1st document
PdfPage page1 = inputDocument1.PageCount > idx ?
inputDocument1.Pages[idx] : new PdfPage();
// Get page from 2nd document
PdfPage page2 = inputDocument2.PageCount > idx ?
inputDocument2.Pages[idx] : new PdfPage();
// Add both pages to the output document
page1 = outputDocument.AddPage(page1);
page2 = outputDocument.AddPage(page2);
// Write document file name and page number on each page
gfx = XGraphics.FromPdfPage(page1);
box = page1.MediaBox.ToXRect();
box.Inflate(0, -10);
gfx.DrawString(String.Format("{0} • {1}", filename1, idx + 1),
font, XBrushes.Red, box, format);
gfx = XGraphics.FromPdfPage(page2);
box = page2.MediaBox.ToXRect();
box.Inflate(0, -10);
gfx.DrawString(String.Format("{0} • {1}", filename2, idx + 1),
font, XBrushes.Red, box, format);
}
// Save the document...
const string filename = "CompareDocument1_tempfile.pdf";
outputDocument.Save(filename);
 日本