Quantcast
Channel: WordprocessingML - Recent Threads
Viewing all articles
Browse latest Browse all 1417

How to Insert multi-line text in table cell using document assembler of powertools

$
0
0

Hi 

I am evaluating open XML and power tools to generate documents. The tool is great and does a lot of tasks which I need in my project.  I tried the DocumentAssembler sample and it worked. I have a couple of requirement which I don't know how to achieve it.

 

1. I have a table in my template and then I want to insert multiple blank lines inside the column before replacing with the column with the xml data. I tried inserting \r\n inside the xml data, but it didn't work. The code is given below. Please suggest a way to achieve this. 

For example, consider a table which has 2 columns, employee name, and employee address.The address column has multi-line data separated by new lines.  But, the new lines are being ignored and the data appears as a single text.

Please help.

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using System.Xml;

namespace DocuGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
           
            var n = DateTime.Now;
            var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));
            tempDi.Create();

            FileInfo templateDoc = new FileInfo("../../TemplateDocument.docx");
            FileInfo dataFile = new FileInfo(Path.Combine(tempDi.FullName, "Data.xml"));

            // The following method generates a large data file with random data.
            // In a real world scenario, this is where you would query your data source and produce XML that will drive your document generation process.
            XElement data = GenerateDataFromDataSource(dataFile);

            WmlDocument wmlDoc = new WmlDocument(templateDoc.FullName);
            int count = 1;
            foreach (var customer in data.Elements("Customer"))
            {
                FileInfo assembledDoc = new FileInfo(Path.Combine(tempDi.FullName, string.Format("Letter-{0:0000}.docx", count++)));
                Console.WriteLine(assembledDoc.Name);
                bool templateError;
                WmlDocument wmlAssembledDoc = DocumentAssembler.AssembleDocument(wmlDoc, customer, out templateError);
                if (templateError)
                {
                    Console.WriteLine("Errors in template.");
                    Console.WriteLine("See {0} to determine the errors in the template.", assembledDoc.Name);
                }
                wmlAssembledDoc.SaveAs(assembledDoc.FullName);
            }
        }

        private static string[] s_productNames = new[] {
            "Unicycle","Bicycle","Tricycle","Skateboard","Roller Blades","Hang Glider",
        };

        private static XElement GenerateDataFromDataSource(FileInfo dataFi)
        {
            int numberOfDocumentsToGenerate = 500;
            var customers = new XElement("Customers");
            Random r = new Random();
            for (int i = 0; i < numberOfDocumentsToGenerate; ++i)
            {
                var customer = new XElement("Customer",
                    new XElement("CustomerID", i + 1 +"\r\n"+44),
                    new XElement("Name", "Sample name"),
                    new XElement("HighValueCustomer", r.Next(2) == 0 ? "True" : "False"),
                    new XElement("Orders"));
                var orders = customer.Element("Orders");
                int numberOfOrders = r.Next(10) + 1;
                for (int j = 0; j < numberOfOrders; j++)
                {
                    var order = new XElement("Order",
                        new XAttribute("Number", j + 1),
                        new XElement("ProductDescription", "ssss"+j+"\r\n" +s_productNames[r.Next(s_productNames.Length)]),//
                        new XElement("Quantity", r.Next(10) + "\r\n" + 33),
                        new XElement("OrderDate", "September 26, 2015"));
                    orders.Add(order);
                }
                customers.Add(customer);
            }
            customers.Save(dataFi.FullName);
            return customers;
        }
        public static void createdoc(string docName)
        {
            // Create a Wordprocessing document. 
            using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document))
            {
                // Add a new main document part. 
                package.AddMainDocumentPart();

                // Create the Document DOM. 
                package.MainDocumentPart.Document =
                  new Document(
                    new Body(
                      new Paragraph(
                        new Run(
                          new Text("Hello World!")))));

                // Save changes to the main document part. 
                package.MainDocumentPart.Document.Save();
            }
        }
        
        

        private static void SaveXDocument(WordprocessingDocument myDoc,
       XDocument mainDocumentXDoc)
        {
            // Serialize the XDocument back into the part
            using (Stream str = myDoc.MainDocumentPart.GetStream(
                FileMode.Create, FileAccess.Write))
            using (System.Xml.XmlWriter xw = XmlWriter.Create(str))
                mainDocumentXDoc.Save(xw);
        }

        private static XDocument GetXDocument(WordprocessingDocument myDoc)
        {
            // Load the main document part into an XDocument
            XDocument mainDocumentXDoc;
            using (Stream str = myDoc.MainDocumentPart.GetStream())
            using (XmlReader xr = XmlReader.Create(str))
                mainDocumentXDoc = XDocument.Load(xr);
            return mainDocumentXDoc;
        }
    }
}

 


Viewing all articles
Browse latest Browse all 1417

Trending Articles