LoginSignup
0
3

More than 3 years have passed since last update.

C# PDFの結合・分割

Posted at

皆さん、お元気でいらっしゃいますか?寒さ厳しい折、どうぞご自愛ください。
さて、今日はPDFファイルについての話です。Spire.PDFというライブラリを通じて、PDFを結合・分割する方法を紹介することにして、どうのようにMergeFiles ()とSplit()というメソッドでこれを実現するか見せてあげましょう。
では、始めましょう

下準備
1.E-iceblueの公式サイトからFree Spire.PDF無料版をダウンロードしてください。

f:id:lendoris:20201124150127p:plain


2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. PDF.dllを参照に追加してください。
(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→PDF.dll”というようです。)

f:id:lendoris:20201124150156p:plain

PDFの結合
MergeFiles () といstaticメソッドで実現することができます。このメソッドには三つのオーバーロードがあります。
 

// Streamから
public static PdfDocumentBase MergeFiles(Stream[] streams);

//Stringから
public static PdfDocumentBase MergeFiles(string[] InputFiles);

//指定するパスから
public static PdfDocumentBase MergeFiles(string firstInputFile, string secInputFile);

結合前

 

f:id:lendoris:20201124150652p:plain

f:id:lendoris:20201124150705p:plain 

サンプルコード 

using Spire.Pdf;
using System;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
String[] files = new String[] { "ファイル1.pdf", "ファイル2.pdf", };
PdfDocumentBase doc = PdfDocument.MergeFiles(files);
doc.Save("結合1.pdf", FileFormat.PDF);
}
 }
  }

結合後

 

f:id:lendoris:20201124150738p:plain

PDFの分割 

PdfDocument doc = new PdfDocument("結合1.pdf");
String pattern = "分割{0}.pdf";
doc.Split(pattern);
doc.Close();

以上です。
ここまで読んでくれてありがとうございます。

 

 

 

0
3
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
3