0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#/VB.NETでPDFをマージする

Posted at

複数のPDFファイルを1つのファイルに結合することは、関連する文書、レポート、または資料の組織と管理において大きな利点を持っています。それは可読性を向上させるだけでなく、情報のシームレスな共有を促進します。PDFファイルを一緒に結合することで、複数のファイルを繰り返し開閉する必要がなくなり、時間と労力を節約できます。さらに、このプロセスにより、ファイルの総数が減少し、ファイルの保存とバックアップが簡素化されます。ファイル結合にはソフトウェアを使用する以外に、プログラミングによっても実行できる優れた方法です。この記事では、C#およびVB.NETを使用して複数のPDFファイルを1つのファイルに結合する方法を紹介します。

プログラム環境

DLLファイルのインポート

  • Visual Studioを開いて新しいプロジェクトを作成します。
  • 「Solution Explorer」で「References」を右クリックします。
  • 「NuGet Manage Packages」を選択します。
  • Free Spire.PDF for .NETを検索してインストールします。
    または、このリンクから直接ダウンロードすることもできます。
  • Visual Studioを開いて新しいプロジェクトを作成します。
  • 「Solution Explorer」で「References」を右クリックします。
  • 「Add Reference」を選択します。
  • 「Browse」をクリックします。
  • DLLを検索してインストールしてプロジェクトにインポートします。

コード

C#:

using System;
using Spire.Pdf;

namespace MergePDFs
{
    class Program
    {
        static void Main(string[] args)
        {

            //ドキュメントのパスを取得する
            String[] files = new String[] {
                "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-1.pdf",
                "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-2.pdf",
                "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-3.pdf"};

            //ドキュメントをマージしてPdfDocumentBaseのオブジェクトを返す
            PdfDocumentBase doc = PdfDocument.MergeFiles(files);

            //結果をPDFファイルに保存する
            doc.Save("Output\\result.pdf", FileFormat.PDF);
        }
    }
}

VB.NET:

Imports System
Imports Spire.Pdf
 
Namespace MergePDFs
    Class Program
        Shared  Sub Main(ByVal args() As String)

            'ドキュメントのパスを取得する
            Dim files() As String =  New String() {"C:\\Users\\Administrator\\Desktop\\PDFs\\sample-1.pdf","C:\\Users\\Administrator\\Desktop\\PDFs\\sample-2.pdf","C:\\Users\\Administrator\\Desktop\\PDFs\\sample-3.pdf"}

            'ドキュメントをマージしてPdfDocumentBaseのオブジェクトを返す
            Dim doc As PdfDocumentBase =  PdfDocument.MergeFiles(files) 
 
            '結果をPDFファイルに保存する
            doc.Save("Output\\result.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

image.png
まず、入力パスを取得します。 次に、PdfDocument.MergeFiles() メソッドを呼び出して、これらのファイルをマージします。 最後に、PdfDocumentBase.Save() メソッドを使用して、結果を PDF ドキュメントに保存します。
上記の方法を参照して、このライブラリを通じてPDFを圧縮し、PDFに画像の透かしテキストの透かしを挿入することもできます。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?