皆さん、お元気でいらっしゃいますか?寒さ厳しい折、どうぞご自愛ください。
さて、今日はPDFファイルについての話です。Spire.PDFというライブラリを通じて、PDFを結合・分割する方法を紹介することにして、どうのようにMergeFiles ()とSplit()というメソッドでこれを実現するか見せてあげましょう。
では、始めましょう
下準備
1.E-iceblueの公式サイトからFree Spire.PDF無料版をダウンロードしてください。
2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. PDF.dllを参照に追加してください。
(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→PDF.dll”というようです。)
PDFの結合
MergeFiles () といstaticメソッドで実現することができます。このメソッドには三つのオーバーロードがあります。
//Stringから
public static PdfDocumentBase MergeFiles(string[] InputFiles);
//指定するパスから
public static PdfDocumentBase MergeFiles(string firstInputFile, string secInputFile);
<p style="margin: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, 'ヒラギノ角ゴ Pro W3', 'Hiragino Kaku Gothic Pro', メイリオ, Meiryo, 'MS Pゴシック', 'MS PGothic', sans-serif; line-height: 1.6; color: #000001; overflow-wrap: break-word; padding: 0px;"><strong style="font-weight: bold; margin: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, 'ヒラギノ角ゴ Pro W3', 'Hiragino Kaku Gothic Pro', メイリオ, Meiryo, 'MS Pゴシック', 'MS PGothic', sans-serif; line-height: 1.6; color: #000001; overflow-wrap: break-word;">結合前</strong></p>
<p style="margin: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, 'ヒラギノ角ゴ Pro W3', 'Hiragino Kaku Gothic Pro', メイリオ, Meiryo, 'MS Pゴシック', 'MS PGothic', sans-serif; line-height: 1.6; color: #000001; overflow-wrap: break-word; padding: 0px;"> </p>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20201124/20201124150652.png" alt="f:id:lendoris:20201124150652p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20201124/20201124150705.png" alt="f:id:lendoris:20201124150705p:plain" title="" class="hatena-fotolife" itemprop="image" /> </p>
<p style="margin: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, 'ヒラギノ角ゴ Pro W3', 'Hiragino Kaku Gothic Pro', メイリオ, Meiryo, 'MS Pゴシック', 'MS PGothic', sans-serif; line-height: 1.6; color: #000001; overflow-wrap: break-word; padding: 0px;"><strong style="font-weight: bold; margin: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, 'ヒラギノ角ゴ Pro W3', 'Hiragino Kaku Gothic Pro', メイリオ, Meiryo, 'MS Pゴシック', 'MS PGothic', sans-serif; line-height: 1.6; color: #000001; overflow-wrap: break-word;">サンプルコード</strong> </p>
```C#
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);
}
}
}
結合後
PDFの分割
PdfDocument doc = new PdfDocument("結合1.pdf");
String pattern = "分割{0}.pdf";
doc.Split(pattern);
doc.Close();
以上です。
ここまで読んでくれてありがとうございます。