たるきちのあれこれ









  Topプログラミングサンプル ▶ PDFファイル結合

papy様作の「pdftool.dll」を使用させていただきます。
「pdftool.dll」を実行ファイルと同じフォルダに置いて作成します。


PDFファイル結合のサンプルです。

C#

// using System.IO;
// using System.Runtime.InteropServices;

[DllImport("pdftool.dll")]
private static extern IntPtr LoadPDF(string OpenFileName);
[DllImport("pdftool.dll")]
private static extern bool FreePDF(IntPtr pdf);
[DllImport("pdftool.dll")]
private static extern int CombinePDFEx(string PDFfilelistName_txt, string CombinedFileName, IntPtr Form_Handle, bool UseOutLine);

string PdfListFile = Application.StartupPath + "\\PdfList.txt";
using (StreamWriter sw = new StreamWriter(PdfListFile, false, Encoding.GetEncoding("Shift_JIS")))
{
    string str = @"C:\sample1.pdf";
    IntPtr hPDF = LoadPDF(str);
    if ((int)hPDF > -1) sw.WriteLine(str);
    FreePDF(hPDF);
    str = @"C:\sample2.pdf";
    hPDF = LoadPDF(str);
    if ((int)hPDF > -1) sw.WriteLine(str);
    FreePDF(hPDF);
}

CombinePDFEx(PdfListFile, @"C:\sample3.pdf", this.Handle, false);