internal override void Render() { RenderFilling(); HtmlFormFormatInfo formatInfo = (HtmlFormFormatInfo)_renderInfo.FormatInfo; Area contentArea = _renderInfo.LayoutInfo.ContentArea; XRect destRect = new XRect(contentArea.X, contentArea.Y, formatInfo.Width, formatInfo.Height); if (formatInfo.Failure == HtmlFormFailure.None) { XImage xImage = null; try { // TODO: Falhar com _form.Content vazio // TODO: Add CropX, CropY, etc... XRect srcRect = new XRect(0, 0, formatInfo.Width, formatInfo.Height); var config = new PdfGenerateConfig { ManualPageSize = new XSize(formatInfo.Width, formatInfo.Height), MarginBottom = 0, MarginTop = 0, MarginLeft = 0, MarginRight = 0 }; using (var htmlPdfDoc = PdfGenerator.GeneratePdf(_form.Content, config)) { using (var pdfStream = new MemoryStream()) { htmlPdfDoc.Save(pdfStream); xImage = XPdfForm.FromStream(pdfStream); _gfx.DrawImage(xImage, destRect, srcRect, XGraphicsUnit.Point); } } } catch (Exception) { RenderFailureImage(destRect); } finally { if (xImage != null) { xImage.Dispose(); } } } else { RenderFailureImage(destRect); } RenderLine(); }
public MemoryStream Message2Pdf(MimeMessage message) { MemoryStream msMail = new MemoryStream(); string html = message.GetTextBody(MimeKit.Text.TextFormat.Html); string txt = message.GetTextBody(MimeKit.Text.TextFormat.Text); HtmlToPdf converter = new HtmlToPdf(); SelectPdf.PdfDocument pdfdok = null; if (html != null) { pdfdok = converter.ConvertHtmlString(html); } else { if (string.IsNullOrEmpty(txt)) { txt = "Tom email"; } pdfdok = converter.ConvertHtmlString(txt); } pdfdok.Save(msMail); msMail.Position = 0; XPdfForm form = XPdfForm.FromStream(msMail); PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument(); XGraphics gfx; int count = form.PageCount; for (int idx = 0; idx < count; idx++) { PdfSharp.Pdf.PdfPage page = outputDocument.AddPage(); if (form.PageCount > idx) { // Get a graphics object for page gfx = XGraphics.FromPdfPage(page); if (idx == 0) { DrawPageHeading(page, gfx, message); // Draw the page like an image gfx.DrawImage(form, new XRect(form.PointWidth * 0.02, form.PointHeight * 0.10, form.PointWidth * 0.90, form.PointHeight * 0.90)); } else { gfx.DrawImage(form, new XRect(form.PointWidth, form.PointHeight, form.PointWidth, form.PointHeight)); } } } msMail = new MemoryStream(); outputDocument.Save(msMail, false); return(msMail); }
public static byte[] ConsultarCertificadoCertame(int certificadoId, string cpf) { var usuario = new BMUsuario().ObterPorCPF(cpf); if (usuario == null) { return(null); } var certificadoUsuario = usuario.ListaUsuarioCertificadoCertame.FirstOrDefault(x => x.CertificadoCertame.ID == certificadoId); if (certificadoUsuario == null) { return(null); } var certificado = certificadoUsuario.CertificadoCertame; // Removendo caracteres especiais que travam no chrome. certificado.Certificado.NomeDoArquivoOriginal = RemoverCaracterEspecial(certificado.Certificado.NomeDoArquivoOriginal); // Create the output document var outputDocument = new PdfDocument { PageLayout = PdfPageLayout.TwoPageLeft }; var font = new XFont("Verdana", 13); var format = new XStringFormat { Alignment = XStringAlignment.Center, LineAlignment = XLineAlignment.Center }; try { var repositorioUpload = ConfiguracaoSistemaUtil.ObterInformacoes(enumConfiguracaoSistema.RepositorioUpload).Registro; var filePath = string.Concat(repositorioUpload, @"\", certificado.Certificado.NomeDoArquivoNoServidor); using (var ms = new MemoryStream()) { using ( var file = new FileStream( filePath, FileMode.Open, FileAccess.Read ) ) { var bytes = new byte[file.Length]; file.Read(bytes, 0, (int)file.Length); ms.Write(bytes, 0, (int)file.Length); } // Por alguma bizarrice, tem que recriar o Stream. var form = XPdfForm.FromStream(new MemoryStream(ms.ToArray())); // Escrever FRENTE do certificado. EscreverFrente(outputDocument, form, certificadoUsuario, font, format); // Escrever VERSO do certificado. EscreverVerso(outputDocument, form, certificadoUsuario, font, format); var streamOutput = new MemoryStream(); outputDocument.Save(streamOutput, false); return(streamOutput.ToArray()); } } catch { return(null); } }