64 lines
1.6 KiB
C#
64 lines
1.6 KiB
C#
using PdfSharp.Pdf;
|
|
using PdfSharp.Pdf.IO;
|
|
using System.Collections.Generic;
|
|
|
|
namespace SteamWare
|
|
{
|
|
/// <summary>
|
|
/// Utilities gestione PDF tramite PdfSharp
|
|
/// </summary>
|
|
public class pdfUtils
|
|
{
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
public pdfUtils()
|
|
{
|
|
}
|
|
/// <summary>
|
|
/// Effettua il merge di più files pdf in un UNICO file pdf di output, restituendo NUMERO di files effettivamente trovati e mergiati...
|
|
/// </summary>
|
|
/// <param name="pdfOutFilename">Nome del file da creare (full path)</param>
|
|
/// <param name="pdfInFileList">Elenco files pdf da includere (full path)</param>
|
|
/// <returns></returns>
|
|
public static int mergePdfFiles(string pdfOutFilename, List<string> pdfInFileList)
|
|
{
|
|
int answ = 0;
|
|
PdfDocument currPdfIn;
|
|
|
|
// merge!
|
|
using (PdfDocument outPdf = new PdfDocument())
|
|
{
|
|
// copio ogni SINGOLA pagina
|
|
foreach (var fileName in pdfInFileList)
|
|
{
|
|
try
|
|
{
|
|
currPdfIn = PdfReader.Open(fileName, PdfDocumentOpenMode.Import);
|
|
CopyPages(currPdfIn, outPdf);
|
|
answ++;
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
// salvo!
|
|
outPdf.Save(pdfOutFilename);
|
|
}
|
|
//restituisco!
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// Effettua copia di ogni singola pagina pdf (from --> to)
|
|
/// </summary>
|
|
/// <param name="from"></param>
|
|
/// <param name="to"></param>
|
|
protected static void CopyPages(PdfDocument from, PdfDocument to)
|
|
{
|
|
for (int i = 0; i < from.PageCount; i++)
|
|
{
|
|
to.AddPage(from.Pages[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|