本文整理汇总了C#中Template.AddVariable方法的典型用法代码示例。如果您正苦于以下问题:C# Template.AddVariable方法的具体用法?C# Template.AddVariable怎么用?C# Template.AddVariable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Template
的用法示例。
在下文中一共展示了Template.AddVariable方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateFromMHTTemplate
/// <summary>
/// Generate a document report from an MHT template.
/// </summary>
/// <param name="templateFileBytes"></param>
/// <param name="persistentObject"></param>
/// <param name="applicationSettingsObject"></param>
/// <param name="outputFormat"></param>
/// <returns></returns>
public static byte[] GenerateFromMHTTemplate(byte[] templateFileBytes, object persistentObject, object applicationSettingsObject, int? outputFormat)
{
MemoryStream mem = new MemoryStream(templateFileBytes);
StringBuilder build = new StringBuilder();
//using (StreamReader sr = new StreamReader(mem, Encoding.GetEncoding("windows-1252")))
//Temporary we use the system default encoding, to avoid conversion problem. If any problem with conversion, we need to find a way to find the encoding of the file
using (StreamReader sr = new StreamReader(mem, Encoding.Default))
{
build.Append(sr.ReadToEnd());
mem.Close();
}
string content = build.ToString();
// In the MHT document, long lines are always broken up
// by Microsoft Word using the = sign followed by
// a carriage return, so we have to unbreak those lines
// so that tags that are broken up are not merged.
//
content = content.Replace("=\r\n", "").Replace("=\n", "").Replace("=\r", "");
// Most MHT documents do not have the view layout set to
// Print mode. If that is the case, insert an XML tag
// to force the document to open in Print view.
//
if (!content.Contains("<w:View>Print</w:View>"))
content = content.Replace("<w:WordDocument>", "<w:WordDocument><w:View>Print</w:View>");
content = Regex.Replace(content, @"<span[\s\r\n]+class=SpellE>(?<text>[\s\S]*?)</span>", "${text}");
Template t = new Template(content, true);
//t.RemoveSpellingErrorTags();
t.AddVariable("obj", persistentObject);
t.AddVariable("applicationSettings", applicationSettingsObject);
content = t.Generate();
// Because we are outputing an MHT file, we
// must encode all unicode characters.
// So this is what we are going to do here:
//
StringBuilder sb = new StringBuilder();
foreach (char c in content)
{
if ((int)c > 255)
sb.Append("&#" + ((int)c).ToString() + ";");
else
sb.Append(c);
}
// Use to output a HTML page to PDF
//
if (outputFormat == DocumentOutputFormat.AcrobatPDF)
{
string guid = Guid.NewGuid().ToString();
string pdfFilePath = "";
if (MhtProcessor.IsMhtContent(content))
{
// Hack the content to add a <div> for page break, because the HTML2PDF
// is unable to recognize the CSS page breaks placed within the <br> tags (which is
// what Microsoft Word does in the MHT files). It only recognizes it if it is in as
// <div> tag.
//
content = content.Replace("page-break-before:always'>\r\n</span>", "page-break-before:always'>\r\n</span><div style='page-break-before:always' />");
// Split up the MHT files into smaller files and save it into the temp folder.
// before passing it through the HTML2PDF program.
//
string outputPath = ConfigurationManager.AppSettings["ReportTempFolder"] + guid + "\\";
Directory.CreateDirectory(outputPath);
string filePath = MhtProcessor.SplitMhtContent(content, outputPath);
if (filePath != "")
pdfFilePath = WKHtmlToPdf(filePath);
//Directory.Delete(outputPath, true);
}
else
{
// Just write out the HTML file as is, and pass it through the
// HTML2PDF program.
//
string filePath = ConfigurationManager.AppSettings["ReportTempFolder"] + guid + ".htm";
StreamWriter fileWriter = new StreamWriter(filePath, false, Encoding.UTF8);
content = content.Replace("=\r\n", "").Replace("=\n", "").Replace("=\r", "").Replace("=3D", "=");
try { fileWriter.Write(content); }
finally { fileWriter.Close(); }
pdfFilePath = WKHtmlToPdf(filePath);
File.Delete(filePath);
}
// We then read the file and return it to the caller.
//
//.........这里部分代码省略.........