Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Serial Number in SSRS Articles
How to Print a Crystal Report direct to printer Articles
Get Started Developing for Android Apps with Eclipse Articles
Razor View Engine Interview Questions Answers Interview Questions
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
SPIRE PDF Library Articles
Infosys Interview Questions Interview Questions
Html5 interview questions and answers Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
SharePoint 2010 interview Questions Answers Interview Questions
Submit Articles | More Articles..

Create word document with headers and footers and save it as xps in C#

Posted By: sarkerishwar On:9/29/2014 11:31:58 PM in:Articles Category:.NET Framework Hits:3475
In this article I will use an instance of creating a word document with header and footer to introduce the detail method to use the docx library to generate the docx files and save it as xps file via the free spire.doc.

Introduction 

Docx library is an exceptionally useful OSS library for working with MS office word file. When you use the library to programmatically manipulate your word files, you haven't to have word install on local machine such as the MS office which is extra consuming your application. In this article I will use an instance of creating a word document with header and footer to introduce the detail method to use the docx library to generate the docx files and save it as xps file via the free spire.doc.


Application Overview

Firstly, create a word document with headers and footers, then save the word file via Docx.

Secondly, load the word file and save it as xps file by using spire.doc.

Before starting your project you need download the docx.dll and the free spire.doc to your computer and then reference them.

There are main steps of the method:

Step1:  create a word file with headers and footers and then save the word file via Docx.

 using (DocX document = DocX.Create(@"docs\HeadersAndFooters.docx"))

            {

                // Add Headers and Footers to this document.

                document.AddHeaders();

                document.AddFooters();

                // Force the first page to have a different Header and Footer.

document.PageHeight = 500;

                document.DifferentFirstPage = true;

                // Get the first Headers for this document.

                Header header_first = document.Headers.first;

                // Get the first Footer for this document.

                Footer footer_first = document.Footers.first;

                Paragraph p0 = header_first.InsertParagraph("Hello First Header.", false, formatting);

                Paragraph p1 = footer_first.InsertParagraph();

                P1.Append("Hello First Footer.").Alignment = Alignment.right;

                Paragraph p2 = document.InsertParagraph();

                P2.AppendLine("Hello First page.");

                p6.InsertPageBreakAfterSelf();

                document.InsertSection();

                document.Save();

 

                      }

Effective screenshot of the word document with header and footer

Step2:

Load the word document and save it as xps. The Docx library don't support convert file format. So we need use the free Spire.Doc which support convert word document to other format file, such as pdf, html, rtf and xps.

The below code show you how to convert word document to xps file via spire.doc.

//load word file          

    Document doc = new Document();

    doc.LoadFromFile(@"docs\HeadersAndFooters.docx");

//save word document as xps file

    doc.SaveToFile(@"docs\HeadersAndFooters.xps",FileFormat.XPS);

Effective screenshot of saving word document as xps file:



Conclusion

DocX is an open source to easily read and write word 2007-2010, but it not compatible with word 93-97 and word 2013. Spire.Doc supports to work with these versions of word documents, but it's Free version is limited to 100 paragraphs and 5 tables. For personal use, both of them are enough. 

comments powered by Disqus
User Profile
Sarker Ishwar
Software Engineer
NA , NA
Email :You must Log In to access the contact details.
Latest Post from :sarkerishwar
Create word document with headers and footers and save it as xps in C#
View: 3475 | Submitted on: 9/29/2014 11:31:58 PM
Submit Articles | All Post of This User..


Advertise About Us Private Policy Terms of use
All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher