public byte[] GetPDF(string pHTML) { byte[] bPDF = null; MemoryStream ms = new MemoryStream(); TextReader txtReader = new StringReader(pHTML); // 1: create object of a itextsharp document class Document doc = new Document(PageSize.A4, 10, 10, 10, 5); //era tudo 25 // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms); // 3: we create a worker parse the document HTMLWorker htmlWorker = new HTMLWorker(doc); // 4: we open document and start the worker on the document doc.Open(); htmlWorker.StartDocument(); // 5: parse the html into the document htmlWorker.Parse(txtReader); // 6: close the document and the worker htmlWorker.EndDocument(); htmlWorker.Close(); doc.Close(); bPDF = ms.ToArray(); return bPDF; }
private MemoryStream createPDF(string html) { MemoryStream msOutput = new MemoryStream(); TextReader reader = new StringReader(html); // step 1: creation of a document-object Document document = new Document(PageSize.A4, 30, 30, 30, 30); // step 2: // we create a writer that listens to the document // and directs a XML-stream to a file PdfWriter writer = PdfWriter.GetInstance(document, msOutput); // step 3: we create a worker parse the document HTMLWorker worker = new HTMLWorker(document); // step 4: we open document and start the worker on the document document.Open(); worker.StartDocument(); // step 5: parse the html into the document worker.Parse(reader); // step 6: close the document and the worker worker.EndDocument(); worker.Close(); document.Close(); return msOutput; }
public static MemoryStream RenderHtmlToPdfStream(string html) { var memoryStream = new MemoryStream(); var reader = new StringReader(html); using (var document = new Document(PageSize.A4, 30, 30, 30, 30)) { using (var worker = new HTMLWorker(document)) { using (var writer = PdfWriter.GetInstance(document, memoryStream)) { writer.CloseStream = false; document.Open(); worker.StartDocument(); worker.Parse(reader); worker.EndDocument(); worker.Close(); document.Close(); } } } memoryStream.Seek(0, 0); return memoryStream; }
public byte[] RenderFromHtml(string html) { var output = new MemoryStream (); using (var reader = new StringReader (html)) { var document = new Document (); var worker = new HTMLWorker (document); document.Open (); worker.StartDocument (); worker.Parse (reader); worker.EndDocument (); worker.Close (); document.Close (); return output.ToArray (); } }
private MemoryStream createPDF() { string html = BdyTextBox.Text; MemoryStream msOutput = new MemoryStream(); TextReader reader = new StringReader(html); Document document = new Document(PageSize.A4, 30, 30, 30, 30); PdfWriter writer = PdfWriter.GetInstance(document, msOutput); HTMLWorker worker = new HTMLWorker(document); document.Open(); worker.StartDocument(); worker.Parse(reader); worker.EndDocument(); worker.Close(); document.Close(); return msOutput; }
public void RelatorioPDF(IEnumerable<PROCESSOS> proc) { byte[] bPDF = null; MemoryStream ms = new MemoryStream(); TextReader txtReader = new StringReader(""); // 1: create object of a itextsharp document class Document doc = new Document(PageSize.A4, 5, 5, 25, 70); // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms); oPdfWriter.PageEvent = new Paginacao(); // 3: we create a worker parse the document HTMLWorker htmlWorker = new HTMLWorker(doc); // 4: we open document and start the worker on the document doc.Open(); //doc.NewPage(); doc.Add(new Paginacao().Paragrafo("Gerenciamento Eletrônico de Documentos \n", 18f)); doc.Add(new Paginacao().Paragrafo("Sesconti Serviços \n", 18f)); /* var paragrafo = new Paragraph("RELATÓRIO LICITAÇÃO \n", new Font(Font.NORMAL, 24)); paragrafo.Alignment = Element.ALIGN_CENTER; paragrafo.Font = new Font(Font.FontFamily.HELVETICA); doc.Add(paragrafo);*/ var _flt = (Filtro)Session["Filtro"]; /*var municipio = new Paragraph("Municipio: " + new Entidade().NomeEntidade() + "\n", new Font(Font.NORMAL, 10f)); municipio.Alignment = Element.ALIGN_CENTER; municipio.Font = new Font(Font.FontFamily.HELVETICA); doc.Add(municipio);*/ doc.Add(new Paginacao().Paragrafo("Município: " + new Entidade().NomeEntidade() + "\n", 10f)); if (_flt.UnidadeGestora > 0) { doc.Add(new Paginacao().Paragrafo("Unidade Gestora: " + _flt.CodigoNomeUnidadeGestora + "\n", 10f)); } if (_flt.DataRefDoc != "") { doc.Add(new Paginacao().Paragrafo("Referente MÊS/ANO: " + new Funcoes().MMAAAABARRA(_flt.DataRefDoc) + "\n", 10f)); } if (_flt.DataInicio != "") { var periodo = new Paragraph("Período: " + new Funcoes().DDMMAAAA(_flt.DataInicio) + " à " + new Funcoes().DDMMAAAA(_flt.DataFim) + "\n", new Font(Font.NORMAL, 10f)); periodo.Alignment = Element.ALIGN_CENTER; periodo.Font = new Font(Font.FontFamily.HELVETICA); doc.Add(periodo); } if (_flt.CodigoUnidadeGestora != "") { doc.Add(new Paginacao().Paragrafo("Unidade Gestora: " + _flt.CodigoNomeUnidadeGestora + "\n", 10f)); } if (_flt.Conta != "") { doc.Add(new Paginacao().Paragrafo("Conta: " + _flt.Conta + "\n", 10f)); } if (_flt.Credor != "") { doc.Add(new Paginacao().Paragrafo("Credor: " + _flt.Credor + "\n", 10f)); } if (_flt.DocCaixa != "") { doc.Add(new Paginacao().Paragrafo("Doc. Caixa: " + _flt.DocCaixa + "\n", 10f)); } if (_flt.NotaEmp != "") { doc.Add(new Paginacao().Paragrafo("Nota Empenho: " + _flt.NotaEmp + "\n", 10f)); } //if (_flt.Tipo != 0) //{ doc.Add(new Paginacao().Paragrafo("Documentos: " + new Retorno().TipoDocumento(_flt.Tipo) + "\n", 10f)); //} doc.Add(new Paginacao().Paragrafo("___________________________________________________________________________________________ \n", 10f)); PdfPTable tbl = new PdfPTable(7); //3 é quantidade de colunas float[] anchoDeColumnas = new float[] { 50f, 50f, 200f, 50f, 40f, 45f, 50f }; tbl.SetWidths(anchoDeColumnas); tbl.DefaultCell.Border = Rectangle.NO_BORDER; Filtro flt = (Filtro)Session["Filtro"]; if (flt == null) throw new Exception("Nenhum registro encontrado, tente novamente..."); if (flt != null) { var lista = new Processo().ListaAnoMes(flt.DataRefDoc, flt.Tipo, flt.DocCaixa, flt.NotaEmp, flt.DataInicio, flt.DataFim, flt.Credor, flt.Conta, flt.CodigoUnidadeGestora); tbl.AddCell(new Paginacao().ColCabecalho("Doc.caixa")); tbl.AddCell(new Paginacao().ColCabecalho("Empenho")); tbl.AddCell(new Paginacao().ColCabecalho("Credor")); tbl.AddCell(new Paginacao().ColCabecalho("Data Pagam",Alinhar.ESQUERDA)); tbl.AddCell(new Paginacao().ColCabecalho("Ung")); tbl.AddCell(new Paginacao().ColCabecalho("Valor",Alinhar.DIREITA)); tbl.AddCell(new Paginacao().ColCabecalho("Qtd.Docs",Alinhar.DIREITA)); int totDocs = 0; int reg = 0; var lstDocumentos = db.DOCUMENTOS.ToList(); foreach (var itm in lista) { string doccaixa = new Funcoes().NaoNull(itm.PRC_DOCCAIXA); string notaemp = new Funcoes().NaoNull(itm.PRC_NOTAEMP); string credor = new Funcoes().NaoNull(itm.PRC_CREDOR); string dtpagam = new Funcoes().NaoNull(itm.PRC_DTPAGAM); string undgest = new Funcoes().NaoNull(itm.PRC_UND_GEST); string vrpagam = new Funcoes().NaoNullDec(itm.PRC_VRPAGAM); //string totdocumento = new Documento().TotalDocumento(itm.PRC_ID).ToString(); string totdocumento = lstDocumentos.Where(x => x.DOC_PROC_ID == itm.PRC_ID).Count().ToString(); tbl.AddCell(new Paginacao().ColValor(doccaixa)); tbl.AddCell(new Paginacao().ColValor(notaemp)); tbl.AddCell(new Paginacao().ColValor(credor)); tbl.AddCell(new Paginacao().ColValor(dtpagam,Alinhar.ESQUERDA)); tbl.AddCell(new Paginacao().ColValor(undgest)); tbl.AddCell(new Paginacao().ColValor(vrpagam,Alinhar.DIREITA)); tbl.AddCell(new Paginacao().ColValor(totdocumento,Alinhar.CENTRO)); totDocs += int.Parse(totdocumento); reg++; } doc.Add(tbl); doc.Add(new Paginacao().Paragrafo("___________________________________________________________________________________________ \n\n", 10f)); doc.Add(new Paginacao().Paragrafo("Total de processos listados: " + reg.ToString() + " Total Documentos: " + totDocs.ToString() + "\n", 10f)); doc.NewPage(); htmlWorker.StartDocument(); // 5: parse the html into the document htmlWorker.Parse(txtReader); // 6: close the document and the worker htmlWorker.EndDocument(); htmlWorker.Close(); doc.Close(); bPDF = ms.ToArray(); Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=" + "RelProcessos.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(bPDF); Response.End(); } else { TempData["MsgErro"] = "Sem registros para listar em relatório..."; } //return RedirectToAction("Index"); }
public static void ConvertHtmlToPdf(string documentFilename, string HTMLCode, PaperSize paperSize) { Rectangle pageSize = PageSize.A4; switch (paperSize) { case PaperSize.A4: pageSize = PageSize.A4; break; case PaperSize.A4_LANDSCAPE: pageSize = PageSize.A4_LANDSCAPE; break; case PaperSize.LETTER: pageSize = PageSize.LETTER; break; case PaperSize.LETTER_LANDSCAPE: pageSize = PageSize.LETTER_LANDSCAPE; break; } HttpContext context = HttpContext.Current; //Render PlaceHolder to temporary stream StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); string postContent = getImage(HTMLCode); StringReader reader = new StringReader(postContent); //Create PDF document Document doc = new Document(pageSize); HTMLWorker parser = new HTMLWorker(doc); PdfWriter.GetInstance(doc, new FileStream(documentFilename, FileMode.Create)); doc.Open(); try { parser.Parse(reader); parser.EndDocument(); } catch (Exception ex) { Logger.Instance.Write(LogLevel.Error, ex, "[Util.ConvertHtmlToPdf]"); // Display parser errors in PDF. Paragraph paragraph = new Paragraph("Error!" + ex.Message); Chunk text = paragraph.Chunks[0] as Chunk; if (text != null) text.Font.Color = BaseColor.RED; doc.Add(paragraph); } finally { parser.Close(); doc.Close(); } }
public void RelatorioLista() { byte[] bPDF = null; MemoryStream ms = new MemoryStream(); TextReader txtReader = new StringReader(""); // 1: create object of a itextsharp document class Document doc = new Document(PageSize.A4, 5, 5, 25, 70); // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms); oPdfWriter.PageEvent = new Paginacao(); // 3: we create a worker parse the document HTMLWorker htmlWorker = new HTMLWorker(doc); // 4: we open document and start the worker on the document doc.Open(); //doc.NewPage(); //doc.Add(new Paragraph("CAPA RELATORIO PROCESSOS"); /*var paragrafo = new Paragraph("CAPA RELATORIO PROCESSOS", new Font(Font.NORMAL, 24)); //etipulando o alinhamneto paragrafo.Alignment = Element.ALIGN_CENTER; paragrafo.Font = new Font(Font.FontFamily.HELVETICA); doc.Add(paragrafo);*/ /*var paragrafo2 = new Paragraph(doc.PageNumber.ToString(), new Font(Font.NORMAL, 8)); paragrafo2.Alignment = Element.ALIGN_CENTER; paragrafo2.Font = new Font(Font.FontFamily.HELVETICA); doc.Add(paragrafo2);*/ //doc.NewPage(); doc.Add(new Paginacao().Paragrafo("Gerenciamento Eletrônico de Documentos \n", 18f)); doc.Add(new Paginacao().Paragrafo("Sesconti Servicos \n", 18f)); /* var paragrafo = new Paragraph("RELATÓRIO LICITAÇÃO \n", new Font(Font.NORMAL, 24)); paragrafo.Alignment = Element.ALIGN_CENTER; paragrafo.Font = new Font(Font.FontFamily.HELVETICA); doc.Add(paragrafo);*/ var _flt = (Filtro)Session["Filtro"]; /*var municipio = new Paragraph("Município: " + new Entidade().NomeEntidade()+"\n", new Font(Font.NORMAL, 10f)); municipio.Alignment = Element.ALIGN_CENTER; municipio.Font = new Font(Font.FontFamily.HELVETICA); doc.Add(municipio);*/ doc.Add(new Paginacao().Paragrafo("Município: " + new Entidade().NomeEntidade() + "\n", 10f)); if (_flt.UnidadeGestora > 0) { doc.Add(new Paginacao().Paragrafo("Unidade Gestora: "+ _flt.CodigoNomeUnidadeGestora+"\n", 10f)); } if (_flt.DataRefDoc!="") { doc.Add(new Paginacao().Paragrafo("Referente MESANO: " + new Funcoes().MMAAAA(_flt.DataRefDoc) + "\n", 10f)); } if (_flt.DataInicio != "") { var periodo = new Paragraph("Período: " + new Funcoes().DDMMAAAA(_flt.DataInicio) + " à " + new Funcoes().DDMMAAAA(_flt.DataFim) + "\n", new Font(Font.NORMAL, 10f)); periodo.Alignment = Element.ALIGN_CENTER; periodo.Font = new Font(Font.FontFamily.HELVETICA); doc.Add(periodo); } if (_flt.Tipo != 0) { doc.Add(new Paginacao().Paragrafo("Documentos: " + new Retorno().TipoDocumento(_flt.Tipo) + "\n", 10f)); } doc.Add(new Paginacao().Paragrafo("___________________________________________________________________________________________ \n", 10f)); PdfPTable tbl = new PdfPTable(6); //3 é quantidade de colunas float[] anchoDeColumnas = new float[] { 50f, 60f, 40f, 160f, 55f, 45f }; tbl.SetWidths(anchoDeColumnas); tbl.DefaultCell.Border = Rectangle.NO_BORDER; /*tbl.AddCell("TESTE 1"); tbl.AddCell("TESTE 2"); tbl.AddCell("Grupo Teste da Silva Rodrigues LDTA"); tbl.AddCell("TESTE 4"); tbl.AddCell("TESTE 5"); tbl.AddCell("TESTE 6");*/ Filtro flt = (Filtro)Session["Filtro"]; if (flt != null) { var lista = new DAO.Licitacao().ListaLicitacaoFiltro(flt); tbl.AddCell(new Paginacao().ColCabecalho("Num.Proc.")); tbl.AddCell(new Paginacao().ColCabecalho("Modalidade")); tbl.AddCell(new Paginacao().ColCabecalho("Dt.Real")); tbl.AddCell(new Paginacao().ColCabecalho("Descrição do Objeto")); tbl.AddCell(new Paginacao().ColCabecalho("Unid.Gestora")); tbl.AddCell(new Paginacao().ColCabecalho("Quant.Doc")); int totDocs = 0; int reg = 0; foreach (var lst in lista) { PdfPCell cell1 = new PdfPCell(new Phrase(lst.NUMPROC.ToString(), new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL))); cell1.Border = 0; tbl.AddCell(cell1); PdfPCell cell2 = new PdfPCell(new Phrase(lst.MOLMODALIDADE.ToString(), new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL))); cell2.Border = 0; tbl.AddCell(cell2); PdfPCell cell3 = new PdfPCell(new Phrase(lst.DTREAL.ToString(), new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL))); cell3.Border = 0; tbl.AddCell(cell3); PdfPCell cell4 = new PdfPCell(new Phrase(lst.DESCOBJ.ToString(), new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL))); cell4.Border = 0; tbl.AddCell(cell4); string ungs = new Licitacao().RetornaUnidadeGestora(lst.ID).ToString(); PdfPCell cell5 = new PdfPCell(new Phrase(ungs, new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL))); cell5.Border = 0; tbl.AddCell(cell5); string qtdimg = lst.QTDIMG.ToString(); PdfPCell cell6 = new PdfPCell(new Phrase(qtdimg, new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL))); cell6.Border = 0; tbl.AddCell(cell6); totDocs += int.Parse(qtdimg); reg++; //PdfPCell cell = new PdfPCell(new Phrase("TESTE 7", new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL, BaseColor.BLACK))); //cell.Border = 0; //tbl.AddCell(cell); } doc.Add(tbl); doc.Add(new Paginacao().Paragrafo("\n\n", 10f)); doc.Add(new Paginacao().Paragrafo("___________________________________________________________________________________________ \n\n", 10f)); doc.Add(new Paginacao().Paragrafo("Total de processos listados: " + reg.ToString() + " Total Documentos: " + totDocs.ToString() + "\n", 10f)); doc.NewPage(); htmlWorker.StartDocument(); // 5: parse the html into the document htmlWorker.Parse(txtReader); // 6: close the document and the worker htmlWorker.EndDocument(); htmlWorker.Close(); doc.Close(); bPDF = ms.ToArray(); Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=" + "RelLicitacao.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(bPDF); Response.End(); } else { TempData["MsgErro"] = "Sem registros para listar em relatório..."; } //return RedirectToAction("Index"); }