Guest User

Untitled

a guest
Feb 21st, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. I’ve made a simple class to merge PDF files using iText#:
  2.  
  3. csharp:
  4. using System;
  5.  using System.Collections.Generic;
  6.  using System.IO;
  7.  using iTextSharp.text;
  8.  using iTextSharp.text.pdf;
  9.  
  10. public class PdfMerge
  11.  {
  12.      private BaseFont baseFont;
  13.      private bool enablePagination = false;
  14.      private readonly List<PdfReader> documents;
  15.      private int totalPages;
  16.  
  17.     public BaseFont BaseFont
  18.      {
  19.          get { return baseFont; }
  20.          set { baseFont = value; }
  21.      }
  22.  
  23.     public bool EnablePagination
  24.      {
  25.          get { return enablePagination; }
  26.          set
  27.          {
  28.              enablePagination = value;
  29.              if (value && baseFont == null)
  30.                  baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
  31.          }
  32.      }
  33.  
  34.     public List<PdfReader> Documents
  35.      {
  36.          get { return documents; }
  37.      }
  38.  
  39.     public void AddDocument(string filename)
  40.      {
  41.          documents.Add(new PdfReader(filename));
  42.      }
  43.      public void AddDocument(Stream pdfStream)
  44.      {
  45.          documents.Add(new PdfReader(pdfStream));
  46.      }
  47.      public void AddDocument(byte[] pdfContents)
  48.      {
  49.          documents.Add(new PdfReader(pdfContents));
  50.      }
  51.      public void AddDocument(PdfReader pdfDocument)
  52.      {
  53.          documents.Add(pdfDocument);
  54.      }
  55.  
  56.     public void Merge(string outputFilename)
  57.      {
  58.          Merge(new FileStream(outputFilename, FileMode.Create));
  59.      }
  60.      public void Merge(Stream outputStream)
  61.      {
  62.          if (outputStream == null || !outputStream.CanWrite)
  63.              throw new Exception("OutputStream es nulo o no se puede escribir en éste.");
  64.  
  65.         Document newDocument = null;
  66.          try
  67.          {
  68.              newDocument = new Document();
  69.              PdfWriter pdfWriter = PdfWriter.GetInstance(newDocument, outputStream);
  70.  
  71.             newDocument.Open();
  72.              PdfContentByte pdfContentByte = pdfWriter.DirectContent;
  73.  
  74.             if (EnablePagination)
  75.                  documents.ForEach(delegate(PdfReader doc)
  76.                                    {
  77.                                        totalPages += doc.NumberOfPages;
  78.                                    });
  79.  
  80.             int currentPage = 1;
  81.              foreach (PdfReader pdfReader in documents)
  82.              {
  83.                  for (int page = 1; page <= pdfReader.NumberOfPages; page++)
  84.                  {
  85.                      newDocument.NewPage();
  86.                      PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, page);
  87.                      pdfContentByte.AddTemplate(importedPage, 0, 0);
  88.  
  89.                     if (EnablePagination)
  90.                      {
  91.                          pdfContentByte.BeginText();
  92.                          pdfContentByte.SetFontAndSize(baseFont, 9);
  93.                          pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
  94.                              string.Format("{0} de {1}", currentPage++, totalPages), 520, 5, 0);
  95.                          pdfContentByte.EndText();
  96.                      }
  97.                  }
  98.              }
  99.          }
  100.          finally
  101.          {
  102.              outputStream.Flush();
  103.              if (newDocument != null)
  104.                  newDocument.Close();
  105.              outputStream.Close();
  106.          }
  107.      }
  108.  
  109.     public PdfMerge()
  110.      {
  111.          documents = new List<PdfReader>();
  112.      }
  113.  }
  114.  
  115. Usage:
  116.  
  117. csharp:
  118. string basePath = "c:\\pdf";
  119.  
  120. PdfMerge demo = new PdfMerge();
  121.  
  122. demo.AddDocument(Path.Combine(basePath, "static-dynamic-typing-meijer.pdf"));
  123.  demo.AddDocument(Path.Combine(basePath, "composable-memory-transactions.pdf"));
  124.  
  125. demo.Merge("mergedPapers.pdf");
  126.  
  127. Console.WriteLine("Archivo generado en: {0}", Path.GetFullPath("mergedPapers.pdf"));
Advertisement
Add Comment
Please, Sign In to add comment