Template system & book

Home Forums Open-Xml-Sdk Template system & book

Tagged: , ,

This topic contains 3 replies, has 2 voices, and was last updated by  herve 7 years, 6 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #3587

    herve
    Participant

    Hello,

    I don’t know if it’s the right place to post my questions, may be it will be better in Open-Xml-PowerTools (.Net) ..??

    First question:
    Do you know a book to recommand on Open Xml please (did you made one yourself)?

    Second question:
    I would like to know if it’s possible to do something, imagine a Word (docx) document with only one page. On this page I have a table with 2 rows and 3 columns (total of 6 cells).
    Each cell can contains some text (a header title for example).

    In each cell I want to insert the content of a document (docx) and generate a new document.
    Each time I insert the content of one of the 6 files, I don’t want to loose formating (bold, italic, lists, hyperlinks etc)

    I forgot to mention something, in this document I have, for each cell, a search text (a pattern) that matches the name of the file to put the content from.

    For example, the first cell will contain the word “introduction” and I will have to replace this word with the content of the file “introduction.docx”

    Is that possible (I’m using C#)?

    Thank you bye advance.

    Bye,
    Hervé

    • This topic was modified 7 years, 8 months ago by  herve.
    #3589

    Eric White
    Keymaster

    Hi Hervé,

    I have not yet written a book, although I have thought a lot about it. What I have done instead is record many screen-casts/videos. The screen-cast series links are in the side bar on this blog, to the right. In particular, watch the screen-cast series on Introduction to Open XML, also watch introduction to WordprocessingML.

    As far as templating systems goes, I am now recommending the approach that I take in the DocumentAssembler module. Please watch these two screen-casts:

    http://www.ericwhite.com/blog/blog/documentassembler-developer-center/

    DocumentBuilder may also be relevant to you. In particular, watch screen-cast #6 in this series:

    http://www.ericwhite.com/blog/blog/documentbuilder-developer-center/

    Best, Eric

    #3592

    herve
    Participant

    Hello Eric,

    Thank you very much for your help, I will have a look to the videos.
    Concerning the book, I will be very interested.

    Thanks again.

    Bye

    #3815

    herve
    Participant

    Hello Eric,

    My project was just accepted so I’m starting to work on it but I’m completly lost and stuck on the code… A book will be really usefull.

    I have a template document (modele.docx) with some content and let say, 3 special texts inside, like this :
    {document1}
    {document2}
    {document3}

    They represent the name of the file I have to insert content from (document1.docx, documen2.docx and document3.docx)

    With the help of the video you suggested me (http://www.ericwhite.com/blog/advanced-use-of-documentbuilder), Im trying to :
    1) Open the model
    2) Search for everything that starts with { and ends with }
    3) Replace it with an Id on the form “document1”, “document2” and “document3”
    4) From a list of sources (document1.docx etc) create and generate a new document

    Here is the code :

    WmlDocument doc1 = new WmlDocument(@"C:\Winston\models\modele.docx");
    using (MemoryStream mem = new MemoryStream())
    {
    	mem.Write(doc1.DocumentByteArray, 0, doc1.DocumentByteArray.Length);
    	using (WordprocessingDocument doc = WordprocessingDocument.Open(mem, true))
    	{
    		XDocument xDoc = doc.MainDocumentPart.GetXDocument();
    		foreach (XElement element in xDoc.Root.Element(W.body).Elements(W.p))
    		{
    			if (element == null)
    				continue;
    
    			if(element.Name == W.p)
    			{
    				string content = element.Value;
    				if (content == null || content.Trim().Equals(""))
    					continue;
    
    				content = content.Trim();
    
    				if(content.StartsWith("{") && content.EndsWith("}"))
    				{
    					string idName = content.Replace("{", string.Empty).Replace("}", string.Empty);
    					MessageBox.Show(idName);
    					element.ReplaceWith(new XElement(PtOpenXml.Insert, new XAttribute("Id", idName)));
    				}
    			}
    		}
    		doc.MainDocumentPart.PutXDocument();
    	}
    	doc1.DocumentByteArray = mem.ToArray();
    }
    
    string outFileName = @"C:\Winston\output\output.docx";
    List<Source> sources = new List<Source>()
    {
    	new Source(doc1, true),
    	new Source(new WmlDocument(@"C:\Winston\data\document1.docx"), "document1"),
    	new Source(new WmlDocument(@"C:\Winston\data\document2.docx"), "document2"),
    	new Source(new WmlDocument(@"C:\Winston\data\document3.docx"), "document3"),
    };
    DocumentBuilder.BuildDocument(sources, outFileName);
    

    When I comment this line :

    
    element.ReplaceWith(new XElement(PtOpenXml.Insert, new XAttribute("Id", idName)));
    

    The code finds the 3 searched patterns but when I uncomment it, it only see the patterns one time…??
    Do you have an idea of what I am doing wrong?

    Thank you by advance.

    Bye,
    Hervé

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.