AntonioGanci

Il blog di Antonio Ganci
posts - 147, comments - 334, trackbacks - 22

Esempio di codice per generare un pdf tramite la libreria ITextSharp

In questi giorni un mio amico mi ha chiesto un softwarino (chissà perchè quando si chiede un favore si usa il diminutivo ) che partendo da una tabella di Access generasse una serie di Moduli F24 compilati.

Il modulo dell'F24 da compilare si può scaricare dal sito dell' agenzia delle entrate

A questo punto mi manca una libreria, possibilmente open source, per aggiungere le informazioni necessarie.

Dopo un paio di ricerche ho provato iTextSharp porting della libreria Java iText

Riporto una parte del codice che ho scritto per la generazione del pdf:

    public class ReportF24

    {

        BaseFont m_baseFont;

        PdfContentByte m_contentByte;

        Document m_document;

        PdfImportedPage m_templatePage;

 

        public void Open(string templateFileName, string fileName)

        {

            PdfReader reader = new PdfReader(templateFileName);

            Rectangle pageSize = reader.GetPageSize(1);

            m_document = new Document(pageSize, 50, 50, 50, 50);

            PdfWriter writer = PdfWriter.GetInstance(m_document, new FileStream(fileName, FileMode.Create));

            m_document.Open();

            m_contentByte = writer.DirectContent;

            m_templatePage = writer.GetImportedPage(reader, 1);

            m_baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

        }

 

        public void AddF24(ModuloF24 f24)

        {

            m_document.NewPage();

            m_contentByte.AddTemplate(m_templatePage, 0, 0);

 

            m_contentByte.BeginText();

            m_contentByte.SetFontAndSize(m_baseFont, 12);

 

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, f24.CodiceFiscale, 125, 720, 0);

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, f24.Denominazione, 125, 700, 0);

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, f24.Comune, 125, 650, 0);

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, f24.Provincia, 330, 650, 0);

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, f24.Indirizzo, 370, 650, 0);

 

            float yRigo = 613;

            foreach (RigoErario rigoErario in f24.RighiErario)

            {

                m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT,

rigoErario.CodiceTributo, 160, yRigo, 0);

                m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT,

string.Format("{0:0000}", rigoErario.Mese), 230, yRigo, 0);

                m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT,

string.Format("{0:0000}", rigoErario.Anno), 280, yRigo, 0);

                m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,

string.Format("{0:0.00}", rigoErario.Importo), 400, yRigo, 0);

                yRigo -= 12;

            }

 

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,

string.Format("{0:0.00}", f24.Totale), 400, 540, 0);

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,

string.Format("{0:0.00}", f24.Totale), 575, 540, 0);

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,

string.Format("{0:0.00}", f24.Totale), 575, 120, 0);

 

            string giorno = f24.DataPagamento.Day.ToString("00");

            string mese = f24.DataPagamento.Month.ToString("00");

            string anno = f24.DataPagamento.Year.ToString();

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, giorno, 30, 50, 0);

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, mese, 60, 50, 0);

            m_contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, anno, 90, 50, 0);

 

            m_contentByte.EndText();

        }

 

        public void Close()

        {

            m_document.Close();

        }

  }

 

Il metodo Open Legge il contenuto del pdf del modulo da compilare e crea anche il PdfWriter in cui verranno aggiunte le pagine compilate.

La classe ModuloF24 (qui non riportata) contiene le informazioni necessarie per riempire il modulo.

 

Print | posted on mercoledì 4 giugno 2008 14.30 | Filed Under [ Tips ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 4 and type the answer here:

Powered by: