Posts
45
Comments
79
Trackbacks
12
Concatenare file pdf con iTextSharp

In riferimento al mio post precedente, dato che ho trovato parecchia difficoltà (*) a far funzionare PdfTk per unire più file pdf in un unico file, quando i file di partenza erano parecchi, ho provato ad ottenere lo stesso risultato utilizzando la tanto bistrattata (a me ha risolto parecchi problemi in passato ) libreria iTextSharp:

public class Utils
{
 public static void ConcatPdfFile(ArrayList pdfs, string filename)
 {
  Document document =
new Document();
  PdfWriter writer = PdfWriter.GetInstance(document,
new FileStream(filename, FileMode.Create));
  document.Open();
  PdfContentByte cb = writer.DirectContent;

  int numPdf = pdfs.Count;
  for ( int i=0; i<numPdf; i++)
  {
   PdfReader reader =
new PdfReader(( string )pdfs[i]);
   int pages = reader.NumberOfPages;
   for ( int j=0; j<pages; j++)
   {
    document.SetPageSize(reader.GetPageSizeWithRotation(j+1));
    document.NewPage();
    cb.AddTemplate(writer.GetImportedPage(reader, j+1),0,0);
   }
  }

  document.Close();
 }

}

(*)A dir la verità PdfTk riusciva a concatenare tutti i file lanciando il comando dal prompt, ma non riuscivo a farlo funzionare, o meglio non funzionava sempre, creando, da codice, un nuovo processo tramite la classe System.Diagnostics.Process. Non avendo tempo per indagare in merito al problema l'ho aggirato (soluzione rapida ma non permette di andare a fondo ai problemi )...
Per adesso accontentiamoci di questo, in attesa di buone nuove...e soprattutto di un po'di tempo e magari di un po'di vacanze!!!

Ciao a tutti

...you probably don't write any code until you can build a picture in your mind of what the code does and how the pieces of the code interact..."

powered by IMHO 1.3

posted on venerdì 28 luglio 2006 16.36 Print
Comments
Gravatar
# re: Concatenare file pdf con iTextSharp
lo_stile
15/07/2008 1.16
  
graaaaaaaaaaaaaaaaande!!! ... funziona me stavo scervella pe cerca il modo de fa sto merge! ... Bella pe te!

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 8 and 4 and type the answer here:
News
Habemus Logo...

Habemus Logo...il logo del mio blog



Il mio profilo su LinkedIn

View Alessandro Melchiori's profile on LinkedIn



i like it...

Xobni outlook add-in for your inbox