Example #1
0
        internal void GerarPdf(Document doc, PdfWriter wrt, bool isUsePageEvent, PdfPTable legenda)
        {
            PdfReader reader = new PdfReader("");

            PdfContentByte  cb;
            PdfImportedPage page;
            Rectangle       psize;
            Rectangle       psizeOrg = doc.PageSize;

            float         TopMargin    = doc.TopMargin;
            float         BottomMargin = doc.BottomMargin;
            float         LeftMargin   = doc.LeftMargin;
            float         RightMargin  = doc.RightMargin;
            IPdfPageEvent pageEvent    = wrt.PageEvent;

            doc.SetMargins(0, 0, 0, 0);

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                psize = reader.GetPageSize(i);

                doc.SetPageSize(psize);
                doc.NewPage();

                if (!isUsePageEvent)
                {
                    wrt.PageEvent = null;
                }

                cb = wrt.DirectContent;
                cb.SaveState();

                page = wrt.GetImportedPage(reader, i);

                cb.AddTemplate(page, 0, 0);

                legenda.TotalWidth = (doc.Right - doc.Left) - 85;
                legenda.WriteSelectedRows(0, -1, doc.Left + 42.5f, doc.Bottom + 212, cb);

                cb.RestoreState();
            }

            doc.SetPageSize(psizeOrg);
            doc.SetMargins(LeftMargin, RightMargin, TopMargin, BottomMargin);

            wrt.PageEvent = pageEvent;
        }
Example #2
0
 /**
  * Add a page eventa to the forwarder.
  * @param eventa an eventa that has to be added to the forwarder.
  */
 virtual public void AddPageEvent(IPdfPageEvent eventa)
 {
     events.Add(eventa);
 }
 /** 
 * Add a page eventa to the forwarder.
 * @param eventa an eventa that has to be added to the forwarder.
 */
 virtual public void AddPageEvent(IPdfPageEvent eventa) {
     events.Add(eventa);
 }
 /// <summary>
 /// Add a page eventa to the forwarder.
 /// </summary>
 /// <param name="eventa">an eventa that has to be added to the forwarder.</param>
 public void AddPageEvent(IPdfPageEvent eventa)
 {
     Events.Add(eventa);
 }
Example #5
0
        public byte[] PdfFromHtml(string html, float width, float height, float marginLeft = 50, float marginRight = 50, float marginTop = 10, float marginBottom = 10, IPdfPageEvent pdfPageEvent = null)
        {
            var effectivePageSize = new Rectangle(width, height);

            byte[] buffer = null;
            using (var memoryStream = new MemoryStream()) {
                using (var document = new Document(effectivePageSize, marginLeft, marginRight, marginTop, marginBottom)) {
                    using (var writer = PdfWriter.GetInstance(document, memoryStream)) {
                        writer.PageEvent = pdfPageEvent;
                        int totalfonts = FontFactory.RegisterDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Fonts));
                        using (var sr = new StringReader(html)) {
                            document.Open();
                            XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);
                            document.Close();
                            buffer = memoryStream.ToArray();
                        }
                    }
                }
            }
            return(buffer);
        }
Example #6
0
        public byte[] PdfFromHtml(string html, string pageSize = "A4", float marginLeft = 50, float marginRight = 50, float marginTop = 10, float marginBottom = 10, bool landscape = false, IPdfPageEvent pdfPageEvent = null)
        {
            var effectivePageSize = PageSize.A4;

            switch (pageSize.ToUpper())
            {
            case "A5":
                effectivePageSize = PageSize.A5;
                break;

            case "A4":
                effectivePageSize = PageSize.A4;
                break;

            case "A3":
                effectivePageSize = PageSize.A3;
                break;

            case "A2":
                effectivePageSize = PageSize.A2;
                break;

            case "A1":
                effectivePageSize = PageSize.A1;
                break;

            case "A0":
                effectivePageSize = PageSize.A0;
                break;
            }
            if (landscape)
            {
                effectivePageSize = effectivePageSize.Rotate();
            }
            return(PdfFromHtml(html, effectivePageSize.Width, effectivePageSize.Height, marginLeft, marginRight, marginTop, marginBottom, pdfPageEvent));
        }
Example #7
0
        private static void AnexarArquivos(PdfReader reader, Document doc, PdfWriter wrt)
        {
            try
            {
                PdfContentByte  cb;
                PdfImportedPage page;
                Rectangle       psize;
                Rectangle       psizeOrg = doc.PageSize;

                float TopMargin    = doc.TopMargin;
                float BottomMargin = doc.BottomMargin;
                float LeftMargin   = doc.LeftMargin;
                float RightMargin  = doc.RightMargin;

                doc.SetMargins(0, 0, 0, 0);
                IPdfPageEvent pageEvent = wrt.PageEvent;

                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    psize = reader.GetPageSizeWithRotation(i);

                    doc.SetPageSize(psize);
                    doc.NewPage();
                    wrt.PageEvent = null;

                    cb = wrt.DirectContent;
                    cb.SaveState();

                    page = wrt.GetImportedPage(reader, i);

                    if (psize.Rotation == 0)
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }

                    if (psize.Rotation == 90)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, psize.Height);
                    }

                    if (psize.Rotation == 180)
                    {
                        cb.AddTemplate(page, -1f, 0, 0, -1f, psize.Width, psize.Height);
                    }

                    if (psize.Rotation == 270)
                    {
                        cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, psize.Width, 0);
                    }

                    cb.RestoreState();
                }

                doc.SetPageSize(psizeOrg);
                doc.SetMargins(LeftMargin, RightMargin, TopMargin, BottomMargin);
                doc.NewPage();
                wrt.PageEvent = pageEvent;
                //reader.Close();
            }
            catch (Exception exc)
            {
                throw new Exception("Erro ao gerar anexo de pdf", exc);
            }
        }
Example #8
0
        public static void AnexarPdf(Arquivo.Arquivo arquivo, Document doc, PdfWriter wrt)
        {
            try
            {
                //* Essa validacao nao serve para aquivos temporarios
                //* Desenvolvedor deve garantir a passagem de arquivos temporarios
                //* ou tratar a excessao
                //if (arquivo.Extensao.ToLower() != ".pdf")
                //{
                //    return;
                //}

                PdfReader reader = new PdfReader(File.ReadAllBytes(arquivo.Caminho));

                //if (!reader.IsOpenedWithFullPermissions)
                //{
                //    return;
                //}

                PdfContentByte  cb;
                PdfImportedPage page;
                Rectangle       psize;
                Rectangle       psizeOrg = doc.PageSize;

                float TopMargin    = doc.TopMargin;
                float BottomMargin = doc.BottomMargin;
                float LeftMargin   = doc.LeftMargin;
                float RightMargin  = doc.RightMargin;

                doc.SetMargins(0, 0, 0, 0);
                IPdfPageEvent pageEvent = wrt.PageEvent;

                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    psize = reader.GetPageSizeWithRotation(i);

                    doc.SetPageSize(psize);
                    doc.NewPage();
                    wrt.PageEvent = null;

                    cb = wrt.DirectContent;
                    cb.SaveState();

                    page = wrt.GetImportedPage(reader, i);

                    if (psize.Rotation == 0)
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }

                    if (psize.Rotation == 90)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, psize.Height);
                    }

                    if (psize.Rotation == 180)
                    {
                        cb.AddTemplate(page, -1f, 0, 0, -1f, psize.Width, psize.Height);
                    }

                    if (psize.Rotation == 270)
                    {
                        cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, psize.Width, 0);
                    }

                    cb.RestoreState();
                }

                doc.SetPageSize(psizeOrg);
                doc.SetMargins(LeftMargin, RightMargin, TopMargin, BottomMargin);
                doc.NewPage();
                wrt.PageEvent = pageEvent;
            }
            catch (Exception exc)
            {
                throw new Exception("Erro ao gerar anexo de pdf", exc);
            }
        }