TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sivakumar
NA
551
219.7k
Html styles are not applied to pdf in c#
Apr 28 2016 2:00 AM
Hi ,
This is my c# code :
Byte[] bytes;
int orderID = Convert.ToInt32(e.CommandArgument);
var templateData = ordersBL.GetTemplateDetails(orderID);
// Document document = new Document(PageSize.A4, 10, 10, 10, 10);
using (MemoryStream ms = new MemoryStream())
{
using (Document document = new Document(PageSize.POSTCARD.Rotate()))
{
PdfWriter writer = PdfWriter.GetInstance(document, ms);
foreach (var temp in templateData)
{
string message = temp.Message;
string tempimage = Convert.ToBase64String(temp.logo);
if (!document.IsOpen())
{
//Open the document for writing
document.Open();
}
string body;
body = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns = 'http://www.w3.org/1999/xhtml'> <head>";
body += " <meta http - equiv = 'Content-Type' content = 'text/html; charset=utf-8' /> <title>Template </title> <style type = 'text/css' > ";
body += " ";
body += " ";
body += " .show_msg_pop { ";
body += " border: 6px solid #fd6667; ";
body += " border-radius: 4px; ";
body += " float: left; ";
body += " width: 100%; ";
body += " } ";
body += " .preview_dots { ";
body += " float: left; ";
body += " width: 100%; ";
body += " border: 3px dotted #999; ";
body += " padding: 20px; ";
body += " } ";
body += " ";
body += " </style> ";
body += "</head> ";
body += "<body>";
body += " <table class='show_msg_pop' cellspacing='10'>";
body += " <tr>";
body += " <td>";
body += " <table cellpadding='10' style='width:100%; border-style: dotted !important; border-width:3px; border-color:#999;'>";
body += " <tr>";
body += " <td id='msg' style='width:50%;'>";
body += " <img src='data:image/png;base64," + tempimage + "' width='90' height='90' />";
body += " </td>";
body += " <td class='content_pdf' style='vertical-align:top; top; font-size:10px;'>";
body += " <span style='border:1px dotted #999; float:left; width:100%;'>";
body += " <span style='margin-top:-20px; font-size:10px;'>";
body += temp.Number + ",";
body += " </span>";
body += " <span style='font-size:10px;'>";
body += temp.Street + ",";
body += " </span>";
body += " <span style='font-size:10px;'> ";
body += temp.City + ",";
body += " </span>";
body += " <span style='font-size:10px;'> ";
body += temp.State + ",";
body += " </span>";
body += " </span>";
body += "</td>";
body += "</tr>";
body += "<tr><td colspan='2'>" + message + "</td></tr>";
body += " </table>";
body += " </td>";
body += " </tr>";
body += " </table>";
body += "</ body >";
body += "</ html >";
var tagProcessors = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory();
tagProcessors.RemoveProcessor(HTML.Tag.IMG); // remove the default processor
tagProcessors.AddProcessor(HTML.Tag.IMG, new CustomImageTagProcessor()); // use our new processor
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());
var cssResolver = new StyleAttrCSSResolver(cssFiles);
cssResolver.AddCss(@".code { color:red; border-color: Green; }", true);
//cssResolver.AddCss(@".show_msg_pop { border: 6px solid #fd6667; border - radius: 4px;padding: 10px;float: left;width: 100 %;}", true);
var charset = Encoding.UTF8;
var hpc = new HtmlPipelineContext(new CssAppliersImpl(new XMLWorkerFontProvider()));
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors); // inject the tagProcessors
var htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(document, writer));
var pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
var worker = new XMLWorker(pipeline, true);
var xmlParser = new XMLParser(true, worker, charset);
xmlParser.Parse(new StringReader(body));
document.NewPage();
}
document.Close();
}
bytes = ms.ToArray();
}
Response.Clear();
//string pdfName = "template";
Response.AddHeader("Content-Disposition", "attachment; filename=" + orderID + "_" + "template" + ".pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
html look like this :
But the generated template pdf look like below :
font color is not applied to pdf.
Please give me a solution for that.
Thanks.
Reply
Answers (
4
)
Mail message in Web Application
send email in mvc 3 without network host="smtp.gmail.com"