/// <summary> /// Generate a PDF file /// </summary> /// <param name="printOutputPath">Output filename</param> /// <param name="printingRulesFilePath">Printing rules file</param> /// <param name="replacables">Replace key value pairs in the printing be this map</param> public void Print(string printOutputPath, string printingRulesFilePath, Dictionary <string, string> replacables = null) { var printables = Printables.LoadFromFiles(printingRulesFilePath); if (replacables != null && replacables.Any()) { var texts = printables.Where(printable => printable is PdfText); foreach (PdfText text in texts) { foreach (var replacable in replacables) { text.Text = text.Text.Replace(replacable.Key, replacable.Value); } } } var printer = new Printer(printOutputPath); printer.Print(printables); }
public Printables Get(string printingFilePath) { var result = new Printables(); var printables = TypeExtensions.GetDerivedTypesOf <IPrintable>(); if (File.Exists(printingFilePath)) { var xmlStr = File.ReadAllText(printingFilePath); var doc = XElement.Parse(xmlStr); foreach (XElement childNode in doc.Elements()) { try { var attributes = new Dictionary <string, string>(); foreach (XAttribute attribute in childNode.Attributes()) { attributes.Add(attribute.Name.LocalName, attribute.Value); } var printable = printables.FirstOrDefault(p => childNode.Name == p.Name.Replace("Pdf", "Print")); var createdObject = Activator.CreateInstance(printable, attributes) as IPrintable; result.Add(createdObject); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } } else { MessageBox.Show($"Cannot find printing rules file: {printingFilePath}{Environment.NewLine}You can create one with the Print Page Editor", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } return(result); }