Template system & book
Home › Forums › Open-Xml-Sdk › Template system & book
This topic contains 3 replies, has 2 voices, and was last updated by herve 8 years, 8 months ago.
-
AuthorPosts
-
July 29, 2016 at 2:15 pm #3589
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
July 29, 2016 at 2:40 pm #3592Hello 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
September 22, 2016 at 12:54 pm #3815Hello 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 documentHere 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é -
AuthorPosts
You must be logged in to reply to this topic.