Tutorial
Tutorial
Version 2.4.0.0
egonl
January 2023
SharpDocx 1
Summary
Generating documents with SharpDocx is a two-step process. First you create a view in
Word. A view is a Word document which also contains C# code. Code can be inserted
anywhere, e.g. 1/30/2023 10:13:19 PM would insert the current date and time.
The next step is to create documents based on this view. This requires two lines of code:
Out of the box SharpDocx supports inserting text, tables, images and more. This tutorial
shows you how.
If you want, you can specify a view model to be used in your view. Then you could write
things like <% foreach (var item in Model.MyList) { % >. See the Model sample.
If you want to do something that's not supported by SharpDocx, you can do so by creating
your own document subclass. See the Inheritance example. This example also shows how to
get an output stream instead of a file.
SharpDocx is inspired by Web technologies like ASP.NET and JSP. Developers familiar with
those technologies should feel right at home. It supports .NET Framework 3.5-4.8 and .NET
Standard 2.0. Since it supports .NET Standard 2.0 it can be used in .NET Core 3.1, .NET 5.0
and .NET 6.0 projects as well.
Generating documents with SharpDocx can be very fast: a slightly modified Model sample
produced 25 documents per second on my modest laptop. That’s 1500 documents per
minute. Single threaded.
1/30/2023 10:13:19 PM
SharpDocx 2
Contents
THE BASICS 3
CONDITIONAL CONTENT 4
LOOPS 5
Nested loops 6
IMAGES 9
REPLACING TEXT 10
Notes 11
THE MAP 13
1/30/2023 10:13:19 PM
SharpDocx 3
The basics
At any point in the text you can insert C# statements. Like right here.
This paragraph
contains two
line breaks.
1/30/2023 10:13:19 PM
SharpDocx 4
Conditional content
You can use an if statement to display conditional content.
In this case, any formatting will be lost because the code parser ignores any formatting.
If you want to conditionally display a paragraph with formatting, use a text block: text
between two code blocks and placed between curly brackets:
If you want, you can span multiple elements in a text block. E.g.
2. Text blocks can’t share paragraphs. That means that you can’t write < % } } %> to end two
text blocks. Instead, use two paragraphs, each containing < % } %>.
3. Also, text blocks in else statements are at the moment not supported by SharpDocx.
Instead, use another if statement.
4. Don’t mix text blocks with the AppendRow or AppendParagraph methods: it just won’t
work. Instead, use the Write method to display conditional content. See also issue #25.
1/30/2023 10:13:19 PM
SharpDocx 5
Loops
You can add repeating text blocks to a document like this:
The value of i is 1.
i squared is 1
The value of i is 2.
i squared is 4
The value of i is 3.
i squared is 9
The value of i is 4.
i squared is 16
The value of i is 5.
i squared is 25
The value of i is 6.
i squared is 36
The value of i is 7.
i squared is 49
The value of i is 8.
i squared is 64
The value of i is 9.
i squared is 81
1/30/2023 10:13:19 PM
SharpDocx 6
Nested loops
Loops can also be nested.
Multiples of 1
1*1=1
1*2=2
1*3=3
Note: 3 is divisible by 3.
Multiples of 2
2*1=2
2*2=4
2*3=6
Note: 6 is divisible by 3.
Multiples of 3
3*1=3
Note: 3 is divisible by 3.
3*2=6
Note: 6 is divisible by 3.
3*3=9
Note: 9 is divisible by 3.
Multiples of 4
4*1=4
4*2=8
4 * 3 = 12
Note: 12 is divisible by 3.
1/30/2023 10:13:19 PM
SharpDocx 7
This text and table do NOT repeat, because we used {!. However, a couple of rows do get
appended to the table by using the AppendRow method.
1/30/2023 10:13:19 PM
SharpDocx 8
Multiples of 1
i j i*j
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3*
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6*
* Divisible by 3
Multiples of 2
i j i*j
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6*
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12*
* Divisible by 3
Multiples of 3
i j i*j
3 * 1 = 3*
3 * 2 = 6*
3 * 3 = 9*
3 * 4 = 12*
3 * 5 = 15*
3 * 6 = 18*
* Divisible by 3
1/30/2023 10:13:19 PM
SharpDocx 9
Images
Insert images using the Image method.
If only a file name is specified, SharpDocx searches this file in a directory specified by the
ImageDirectory property. Right now this property has been set to ‘C:\Projects\SharpDocx\
Samples\SampleProjects\Tutorial\bin\Debug\net48/../../../../../Images’.
The Image method accepts a second optional parameter that specifies the relative size of
the image. Here’s at 15%.
Images that are too wide to be displayed at 100% are automatically scaled back. Here’s an
example:
New methods in SharpDocx 2.4 are ImageFromBase64 and ImageFromUrl. And if you want
you can now also use your own streams using the ImageFromStream(Stream stream,
int percentage = 100, string extension = null)method.
ImageFromBase64 example.
ImageFromUrl example.
SharpDocx supports the following image formats: bmp, gif, jpeg, png, tiff and emf.
1/30/2023 10:13:19 PM
SharpDocx 10
Replacing text
If you want to replace text, you can use the Replace method.
Here’s the replaced text. And here’s some more replaced text.
1
Actually, this will only replace text in the body of the document, and not in headers,
footers, end- or footnotes. So this won’t work as expected. But you can use code here.
1/30/2023 10:13:19 PM
SharpDocx 11
using System.Xml.Linq;
Classic Videogame 'Goldeneye 007' Finally Comes to Nintendo Switch and Xbox
The classic 1997 vidoegame GoldenEye 007 "has finally landed on Xbox and Nintendo
Switch," writes the Verge:
On Xbox, the remaster includes 4K resolution, smoother frame rates, and split-screen
local…
D&D Won't Change Its Original 1.0 OGL License, Reference Document Enters Creative
Commons
An anonymous reader shares a report from PC Gamer:
In a blog post published Friday, Wizards of the Coast announced that it is fully putting
the kibosh on the proposed Open Gaming License (OGL) 1.2 th…
Hackers Demand $10M From Riot Games To Stop Leak of 'League of Legends' Source
Code
An anonymous reader quotes a report from Motherboard: Hackers stole the source code
for League of Legends, and now they're asking for $10 million from developer Riot
Games. Motherboard has obtained a …
GameCube and Wii Games Are Now Easier To Play On Xbox Consoles
The new standalone Dolphin emulator will let you play almost any GameCube or Wii
game on your Xbox console. Windows Central reports: Dolphin Emulator for UWP first
rolled out in beta on December 6, 20…
1/30/2023 10:13:19 PM
SharpDocx 12
From Halo to the Simpsons, Would Fictional Mad Scientists Pass Ethical Review?
From Science magazine:
Cave Johnson is almost ready to start a new study in his secret underground facility. The
founder of the Michigan-based technology company Aperture Science, he's invented a
por…
The First 'Bored Ape' NFT Game Costs $2,300+ For Three Weeks of Play
An anonymous reader quotes a report from Ars Technica: Owners of Yuga Labs'
infamous "Bored Ape" non-fungible tokens (and related crypto tokens) get free access to
a simple endless runner/tunnel racin…
Game Makers Stage Mass Exodus From Dungeons & Dragons' 'Open' License
Following controversial changes to Dungeons & Dragons' decades-old Open Gaming
License (OGL), "many prominent third-party RPG publishers now say they're abandoning
the OGL, regardless of what chan…
Staff at Activision Blizzard-owned video game studio Proletariat — whose name is
a term for the working class — announced the…
1/30/2023 10:13:19 PM
SharpDocx 13
In a real world scenario you wouldn’t fetch data or have this much code in a view. But hey,
this is just an example.
Notes
SharpDocx will automatically reference the calling assembly. So if the view model is declared
in the calling assembly, you can use that model in your document without explicitly
referencing that assembly. However, if the view model is defined in another assembly, you
need to explicitly reference it. If you don't, you'll get compilation errors like:
The tilde represents the directory that contains SharpDocx.dll. Use it when you get errors
like:
Another way to add references and namespaces is by defining your own SharpDocx
document subclass. See the Inheritance example.
1/30/2023 10:13:19 PM
SharpDocx 14
The Map
The Map maps OpenXmlElements to plain text and vice versa. It’s being used internally by
the Replace method and for finding the C# code in views, among other things. At the
moment Map.Text looks something like this:
Version 2.4.0.0
egonl
January 2023
Version 2.4.0.0
egonl
January 2023
SharpDocx
SharpDocx
Summary
Generating documents with SharpDocx is a two-step process. First you create a view in
Word. A view is a Word document which also contains C# code. Code can be inserted
anywhere, e.g. 1/30/2023 10:13:19 PM would insert the current date and time.
The next step is to create documents based on this view. This requires two lines of code:
var document = DocumentFactory.Create("view.cs.docx");
do …
The Map might be handy when you want to search the document for text.
1/30/2023 10:13:19 PM
SharpDocx 15
The samples will by default run in .NET Framework 4.8 in Visual Studio 2022. If you want to
change this, right click on the project file in and select Edit Project File. This will open the
csproj file. The first target named on this line will be used for startup/debugging in Visual
Studio:
<TargetFrameworks>net48;net6.0</TargetFrameworks>
Depending on the SDKs you have installed, you can choose between net35, net40, net45,
net46, net47, net48, netstandard2.0, netcoreapp3.1, net5.0, net6.0 and net7.0.
If you want you can remove the net48 target from all projects on Unix-like systems, because
they will produce Windows executables.
1/30/2023 10:13:19 PM