Exemple #1
0
        private static DocX CreateInvoiceFromTemplate(DocX templateDoc)
        {
            // Fill in the document custom properties.
            templateDoc.AddCustomProperty(new CustomProperty("InvoiceNumber", 1355));
            templateDoc.AddCustomProperty(new CustomProperty("InvoiceDate", new DateTime(2016, 10, 15).ToShortDateString()));
            templateDoc.AddCustomProperty(new CustomProperty("CompanyName", "Toms Telecoms"));
            templateDoc.AddCustomProperty(new CustomProperty("CompanySlogan", "Always with you"));
            templateDoc.AddCustomProperty(new CustomProperty("ClientName", "James Doh"));
            templateDoc.AddCustomProperty(new CustomProperty("ClientStreetName", "123 Main street"));
            templateDoc.AddCustomProperty(new CustomProperty("ClientCity", "Springfield, Ohio"));
            templateDoc.AddCustomProperty(new CustomProperty("ClientZipCode", "54789"));
            templateDoc.AddCustomProperty(new CustomProperty("ClientPhone", "438-585-9636"));
            templateDoc.AddCustomProperty(new CustomProperty("ClientMail", "*****@*****.**"));
            templateDoc.AddCustomProperty(new CustomProperty("CompanyStreetName", "1458 Thompson Road"));
            templateDoc.AddCustomProperty(new CustomProperty("CompanyCity", "Los Angeles, California"));
            templateDoc.AddCustomProperty(new CustomProperty("CompanyZipCode", "90210"));
            templateDoc.AddCustomProperty(new CustomProperty("CompanyPhone", "1-965-434-5786"));
            templateDoc.AddCustomProperty(new CustomProperty("CompanySupport", "*****@*****.**"));

            // Remove the default logo and add the new one.
            var paragraphWithDefaultLogo = MiscellaneousSample.GetParagraphContainingPicture(templateDoc);

            if (paragraphWithDefaultLogo != null)
            {
                paragraphWithDefaultLogo.Pictures.First().Remove();
                var newLogo = templateDoc.AddImage(MiscellaneousSample.MiscellaneousSampleResourcesDirectory + @"Phone.png");
                paragraphWithDefaultLogo.AppendPicture(newLogo.CreatePicture(60, 180));
            }

            // Fill the details table.
            MiscellaneousSample.FillDetailsTable(ref templateDoc);

            return(templateDoc);
        }
Exemple #2
0
        /// <summary>
        /// Load a document containing a templated invoice with Custom properties, tables, paragraphs and a picture.
        /// Set the values of those Custom properties, modify the picture, and fill the details table.
        /// </summary>
        public static void CreateInvoice()
        {
            Console.WriteLine("\tCreateInvoice()");

            // Load the templated invoice.
            var templateDoc = DocX.Load(MiscellaneousSample.MiscellaneousSampleResourcesDirectory + @"TemplateInvoice.docx");

            if (templateDoc != null)
            {
                // Create the invoice from the templated invoice.
                var invoice = MiscellaneousSample.CreateInvoiceFromTemplate(templateDoc);

                invoice.SaveAs(MiscellaneousSample.MiscellaneousSampleOutputDirectory + @"CreateInvoice.docx");
                Console.WriteLine("\tCreated: CreateInvoice.docx\n");
            }
        }
Exemple #3
0
        private static void FillDetailsTable(ref DocX templateDoc)
        {
            // The datas that will fill the details table.
            var datas = MiscellaneousSample.GetDetailsData();

            // T he table from the templated invoice.
            var detailsTable = templateDoc.Tables.LastOrDefault();

            if (detailsTable == null)
            {
                return;
            }

            // Remove all rows of the details table, except the header one.
            while (detailsTable.Rows.Count > 1)
            {
                detailsTable.RemoveRow();
            }

            // Loop through each data rows and use them to add new rows in the detailsTable.
            foreach (DataRow data in datas.Rows)
            {
                var newRow = detailsTable.InsertRow();
                newRow.Cells.First().InsertParagraph(data.ItemArray.First().ToString());
                newRow.Cells.Last().InsertParagraph(data.ItemArray.Last().ToString());
            }

            // Calculate the total amount.
            var amountStrings = detailsTable.Rows.Select(r => r.Cells.Last().Paragraphs.Last().Text.Remove(0, 1)).ToList();

            amountStrings.RemoveAt(0); // remove the header
            var totalAmount = amountStrings.Select(s => double.Parse(s)).Sum();

            // Add a Total row in the details table.
            var totalRow = detailsTable.InsertRow();

            totalRow.Cells.First().InsertParagraph("TOTAL:");
            totalRow.Cells.Last().InsertParagraph(string.Format("${0}", totalAmount));
        }