Updates from September, 2009

  • Java Server Faces sucks as much as ASP.NET webforms?

    alex 9:14 am on September 14, 2009 | 0 Permalink | Reply
    Tags: , java, jsf, webforms

    My classes have started again and like the last year, it seems everyone likes Java here.
    Although I have no experience with Java, JSF and ASP.NET Webforms look very similar. I hope I am wrong, because they are “forcing us” to use it for a project because “it is cool”.

     
  • Merge PDF files with iTextDotNet and .NET

    alex 10:09 am on July 19, 2007 | 5 Permalink | Reply
    Tags: , dotnet, itextdotnet, , , ,

    In the previous post I showed a class to merge PDF files using iText#, but it seems that there’s a better approach using iTextDotNet:

    csharp:
    using com.lowagie.tools;

    class Program
    {
        static void Main(string[] args)
        {
            string[] lista = new string[] { "Meijer.pdf", "P330.pdf", "result.pdf" };
            concat_pdf.main(lista);
        }
    }

    As you can see this is a cleaner and simpler way to merge pdf files. You can download the iTextDotNet .NET Library from this blog or the official Website.

     
  • Merge PDF files with iText# and .NET

    alex 7:32 pm on July 18, 2007 | 11 Permalink | Reply
    Tags: , itext#, , , ,

    I’ve made a simple class to merge PDF files using iText#:

    csharp:
    using System;
    using System.Collections.Generic;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;

    public class PdfMerge
    {
        private BaseFont baseFont;
        private bool enablePagination = false;
        private readonly List<PdfReader> documents;
        private int totalPages;

        public BaseFont BaseFont
        {
            get { return baseFont; }
            set { baseFont = value; }
        }

        public bool EnablePagination
        {
            get { return enablePagination; }
            set
            {
                enablePagination = value;
                if (value && baseFont == null)
                    baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            }
        }

        public List<PdfReader> Documents
        {
            get { return documents; }
        }

        public void AddDocument(string filename)
        {
            documents.Add(new PdfReader(filename));
        }
        public void AddDocument(Stream pdfStream)
        {
            documents.Add(new PdfReader(pdfStream));
        }
        public void AddDocument(byte[] pdfContents)
        {
            documents.Add(new PdfReader(pdfContents));
        }
        public void AddDocument(PdfReader pdfDocument)
        {
            documents.Add(pdfDocument);
        }

        public void Merge(string outputFilename)
        {
            Merge(new FileStream(outputFilename, FileMode.Create));
        }
        public void Merge(Stream outputStream)
        {
            if (outputStream == null || !outputStream.CanWrite)
                throw new Exception("OutputStream es nulo o no se puede escribir en éste.");

            Document newDocument = null;
            try
            {
                newDocument = new Document();
                PdfWriter pdfWriter = PdfWriter.GetInstance(newDocument, outputStream);

                newDocument.Open();
                PdfContentByte pdfContentByte = pdfWriter.DirectContent;

                if (EnablePagination)
                    documents.ForEach(delegate(PdfReader doc)
                                      {
                                          totalPages += doc.NumberOfPages;
                                      });

                int currentPage = 1;
                foreach (PdfReader pdfReader in documents)
                {
                    for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                    {
                        newDocument.NewPage();
                        PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, page);
                        pdfContentByte.AddTemplate(importedPage, 0, 0);

                        if (EnablePagination)
                        {
                            pdfContentByte.BeginText();
                            pdfContentByte.SetFontAndSize(baseFont, 9);
                            pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
                                string.Format("{0} de {1}", currentPage++, totalPages), 520, 5, 0);
                            pdfContentByte.EndText();
                        }
                    }
                }
            }
            finally
            {
                outputStream.Flush();
                if (newDocument != null)
                    newDocument.Close();
                outputStream.Close();
            }
        }

        public PdfMerge()
        {
            documents = new List<PdfReader>();
        }
    }

    Usage:

    csharp:
    string basePath = "c:\\pdf";

    PdfMerge demo = new PdfMerge();

    demo.AddDocument(Path.Combine(basePath, "static-dynamic-typing-meijer.pdf"));
    demo.AddDocument(Path.Combine(basePath, "composable-memory-transactions.pdf"));

    demo.Merge("mergedPapers.pdf");

    Console.WriteLine("Archivo generado en: {0}", Path.GetFullPath("mergedPapers.pdf"));

    If you want the source code, you can download the Visual Studio 2005 solution.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel