public TemplGraphic Load(DocX doc) { if (!Loaded) { Image = doc.AddImage(Data); Loaded = true; } return this; }
private static void CreateImage(DocX doc) { try { string imgPath = "rpg-game.png"; var pic = doc.AddImage(imgPath).CreatePicture(); var drawingImg = System.Drawing.Image.FromFile(imgPath); int ratio = drawingImg.Width / drawingImg.Height; int newWidth = (int)doc.PageWidth - (int)(doc.MarginLeft + doc.MarginRight); pic.Width = newWidth; pic.Height = newWidth / ratio; doc.InsertParagraph().InsertPicture(pic); } catch (System.IO.FileNotFoundException ex) { System.Console.WriteLine(ex.Message); } }
private static void InsertPicture(DocX doc, string filename, Formatting format) { using (MemoryStream memoryStream = new MemoryStream()) { System.Drawing.Image myImg = System.Drawing.Image.FromFile(filename); myImg.Save(memoryStream, myImg.RawFormat); // Save your picture in a memory stream. memoryStream.Seek(0, SeekOrigin.Begin); Novacode.Image img = doc.AddImage(memoryStream); // Create image. Paragraph p = doc.InsertParagraph("", false); Picture pic1 = img.CreatePicture(); // Create picture. p.InsertPicture(pic1, 0); // Insert picture into paragraph. doc.Save(); } }
// Create an invoice for a factitious company called "The Happy Builder". private static DocX CreateInvoiceFromTemplate(DocX template) { #region Logo // A quick glance at the template shows us that the logo Paragraph is in row zero cell 1. Paragraph logo_paragraph = template.Tables[0].Rows[0].Cells[1].Paragraphs[0]; // Remove the template Picture that is in this Paragraph. logo_paragraph.Pictures[0].Remove(); // Add the Happy Builders logo to this document. RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing rd.Up(2); Image logo = template.AddImage(rd.Path + @"\images\logo_the_happy_builder.png"); // Insert the Happy Builders logo into this Paragraph. logo_paragraph.InsertPicture(logo.CreatePicture()); #endregion #region Set CustomProperty values // Set the value of the custom property 'company_name'. template.AddCustomProperty(new CustomProperty("company_name", "The Happy Builder")); // Set the value of the custom property 'company_slogan'. template.AddCustomProperty(new CustomProperty("company_slogan", "No job too small")); // Set the value of the custom properties 'hired_company_address_line_one', 'hired_company_address_line_two' and 'hired_company_address_line_three'. template.AddCustomProperty(new CustomProperty("hired_company_address_line_one", "The Crooked House,")); template.AddCustomProperty(new CustomProperty("hired_company_address_line_two", "Dublin,")); template.AddCustomProperty(new CustomProperty("hired_company_address_line_three", "12345")); // Set the value of the custom property 'invoice_date'. template.AddCustomProperty(new CustomProperty("invoice_date", DateTime.Today.Date.ToString("d"))); // Set the value of the custom property 'invoice_number'. template.AddCustomProperty(new CustomProperty("invoice_number", 1)); // Set the value of the custom property 'hired_company_details_line_one' and 'hired_company_details_line_two'. template.AddCustomProperty(new CustomProperty("hired_company_details_line_one", "Business Street, Dublin, 12345")); template.AddCustomProperty(new CustomProperty("hired_company_details_line_two", "Phone: 012-345-6789, Fax: 012-345-6789, e-mail: [email protected]")); #endregion /* * InvoiceTemplate.docx contains a blank Table, * we want to replace this with a new Table that * contains all of our invoice data. */ Table t = template.Tables[1]; Table invoice_table = CreateAndInsertInvoiceTableAfter(t, ref template); t.Remove(); // Return the template now that it has been modified to hold all of our custom data. return template; }