<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.Net Bits and Pieces &#187; Document Generation</title>
	<atom:link href="http://www.dotnetbitsandpieces.com/?feed=rss2&#038;tag=document-generation" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetbitsandpieces.com</link>
	<description>What ever I find interesting to tell about .Net</description>
	<lastBuildDate>Sat, 06 Apr 2013 21:31:17 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>Document Generation with OOXML Part 2</title>
		<link>http://www.dotnetbitsandpieces.com/?p=83</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=83#comments</comments>
		<pubDate>Thu, 04 Apr 2013 20:47:49 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Document Generation]]></category>
		<category><![CDATA[OOMXL]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=83</guid>
		<description><![CDATA[When creating a document from Word templates are used when creating documents. These can be default templates or templates created by users. It is their for essential that these templates can be used when generating OOXML documents. With the Document Generator library templates can be loaded when a new document is created. //Load a template [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small">When creating a document from Word templates are used when creating documents. These can be default templates or templates created by users. It is their for essential that these templates can be used when generating OOXML documents. With the Document Generator library templates can be loaded when a new document is created.</span></p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Load a template from file. document is the name of the new document.</span><br />
<span style="color: #008080; font-style: italic;">//template is the name and full path of the template file</span><br />
<span style="color: #008080; font-style: italic;">//true indicates that the document can override documents with the same name.</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>document, template, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Load a template from a stream</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var stream <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FileStream<span style="color: #008000;">&#40;</span>templateFile, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span>, FileAccess<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var reader <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//stream is the stream of the template.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p><span style="font-size: small">Once the template is loaded the styles defined in the template can be used. As long as the styles are the default for the header and the paragraphs only the level needs to be defined.</span></p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//This adds a new paragraph with the paragraph text and heading text.</span><br />
<span style="color: #008080; font-style: italic;">//The styles used are Heading2 and Normal2. When the styles cannot be found then</span><br />
<span style="color: #008080; font-style: italic;">//Heading1 and normal will be used as styles.</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Paragraph Text&quot;</span>, <span style="color: #666666;">&quot;Heading Text&quot;</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p><span style="font-size: small">Templates can also define custom styles. When the name of the style in the template is known the style can be used with the Document Generator library.<br />
The style can be applied to paragraphs and headers</p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Creates a paragraph and assigns it the MyParagraphStyle.</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;This is the paragraph&quot;</span>, <span style="color: #666666;">&quot;MyParagraphStyle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Creates a paragraph and assigns it the MyParagraphStyle.</span><br />
<span style="color: #008080; font-style: italic;">//and gives it a header with the style MyHeaderStyle</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;This is the paragraph&quot;</span>, <span style="color: #666666;">&quot;Header for Paragraph&quot;</span>, <span style="color: #666666;">&quot;MyParagraphStyle&quot;</span>, <span style="color: #666666;">&quot;MyHeaderStyle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p><span style="font-size: small">In this post I showed you how you can use a template when creating documents and how to use custom styles in the template.<br />
In the next part I will show how you can replace content in documents using Content Controls.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=83</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Document Generation with OOXML Part 1</title>
		<link>http://www.dotnetbitsandpieces.com/?p=33</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=33#comments</comments>
		<pubDate>Sun, 31 Mar 2013 08:27:57 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Document Generation]]></category>
		<category><![CDATA[OOMXL]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=33</guid>
		<description><![CDATA[For a while I have been working on a private project called Document Generator. Document Generator is a .Net library for creating text documents in OOXML and in ODF. With the library you can create an outline for a document with paragraphs, image, etc and generate an OOXML or ODF document. I recently thought it [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small">For a while I have been working on a private project called Document Generator. Document Generator is a .Net library for creating text documents in OOXML and in ODF. With the library you can create an outline for a document with paragraphs, image, etc and generate an OOXML or ODF document. I recently thought it was mature enough to make it a final release. You can find the project at <a title="here" href="http://docgenerator.codeplex.com/" target="_blank">Codeplex </a>.</span></p>
<p><span style="font-size: small">To explain the library I decided to write a serie of articles and this is the first part. It will explain how to get started with the library.</span></p>
<h2>Pre-requisites</h2>
<p><span style="font-size: small">The library is using OOXML SDK 2.5 for creating OOXML documents and this requires .Net Framework 4.0.</span></p>
<h2>Installation</h2>
<p><span style="font-size: small">Document generator does not require any installation. Download the <a href="http://docgenerator.codeplex.com/releases/view/99752" target="_blank">zip</a> file with the latest version and extract the contents to a directory of choice. The zip file contains:</span></p>
<ul><span style="font-size: small"></p>
<li>DocumentGenerator.dll</li>
<li>DocumentFormat.OpenXml.dll (<em>This is the dll for the OOXML SDK 2.5</em>)</li>
<li>ICSharpCode.SharpZipLib.dll (<em>This dll is required for creating ODF files</em>)</li>
<p></span>
</ul>
<h2>Setup the project</h2>
<ol>
<span style="font-size: small"></p>
<li>Create a new console project in Visual Studio</li>
<li>Add a reference to the dll’s for the document generation:
<ul>
<li>DocumentGenerator.dll</li>
<li>DocumentFormat.OpenXml.dll</li>
<li>ICSharpCode.SharpZipLib.dll</li>
</ul>
</li>
<p></span>
</ol>
<h2> Generate Documents</h2>
<ol>
<span style="font-size: small"></p>
<li>The first step in generating a document is to create a Textdocument object. This is the container of the document to generate and is the same for OOXML and ODF documents. For OOXML documents it is also possible to start from a template (.dotx) or an existing document (.docx).</li>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Create a new text document</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Create a new text document with a file name (including the file location)</span><br />
<span style="color: #008080; font-style: italic;">//The Boolean indicates whether the file can be overridden if a file</span><br />
<span style="color: #008080; font-style: italic;">//with the same name is found.</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyFilename&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Create a new text document based on a template</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyFilename&quot;</span>,<span style="color: #666666;">&quot;myTemplate&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Open an existing ooxml document.</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyFile&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<li>When the text document is created headers and paragraphs can be added.</li>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Add a paragraph to the document. Level is an integer used for the style.</span><br />
<span style="color: #008080; font-style: italic;">//of the paragraph, e.g. level &quot;0&quot; set style &quot;Normal&quot; </span><br />
<span style="color: #008080; font-style: italic;">//and level &quot;1&quot; style &quot;Normal1&quot;</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;The paragraph text&quot;</span>, level<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Add a paragraph and a header. Level is an integer is used for the style.</span><br />
<span style="color: #008080; font-style: italic;">//E.g. level &quot;1&quot; applies &quot;Header1&quot; level &quot;2&quot; applies &quot;Header2&quot;</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;The header&quot;</span>,<span style="color: #666666;">&quot;The paragraph text&quot;</span>, level<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Add only a header. Leaving the text for the paragraph empty</span><br />
<span style="color: #008080; font-style: italic;">//will add only a header.</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;The header&quot;</span>,<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span>, level<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<li>Finally the document can be save in OOXML or ODF format.</li>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//First set a filename (including the file location), if not already set</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">Filename</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;MyFile&quot;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Save the document in OOXML format.</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>DocumentType<span style="color: #008000;">.</span><span style="color: #0000FF;">OOXMLTextDocument</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Save the document in ODF format.</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>DocumentType<span style="color: #008000;">.</span><span style="color: #0000FF;">ODFTextDocument</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p></span>
</ol>
<p><span style="font-size: small">In this post I showed you how you can create a document, add contents to the document and save this document in either OOXML or ODF format.<br />
I also showed how you can use a dotx template or an existing ooxml document to create new documents.</span></p>
<p><span style="font-size: small">In the next post I will go through some more advanced features such as creating tables and lists and replace content in OOXML documents.<br />
If you cannot wait for these posts you can have a look a the DocumentGeneratorTest project in Codeplex to see the use of these features.<br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=33</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
