Thursday, May 14, 2009

Create Word Document in .Net

Hello

You can create word document with page number in footer
using below code.

using System.Reflection;
using Microsoft.Office.Interop.Word;
private void CreateDocument()
{


object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

//Start Word and create a new document.
Microsoft.Office.Interop.Word._Application oWord;

Microsoft.Office.Interop.Word._Document oDoc;


oWord = new Microsoft.Office.Interop.Word.Application();

oWord.Visible = false;

oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

//Add Border
oDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleDot;
oDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleDot;
oDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleDot;
oDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleDot;


oDoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;
oDoc.PageSetup.LayoutMode = WdLayoutMode.wdLayoutModeLineGrid;
oDoc.PageSetup.PageWidth = oWord.InchesToPoints(7F);
oDoc.PageSetup.PageHeight = oWord.InchesToPoints(14F);

//Insert a paragraph at the beginning of the document.
Microsoft.Office.Interop.Word.Paragraph oPara1;
object oRngHed;
oRngHed = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
//Assign text
oPara1.Range.Text = "This is test for create word document";
oPara1.Format.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
oPara1.Range.Font.Bold = 10;
oPara1.Range.Font.Size = 18;
//oPara1.Format.SpaceAfter = 2; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();




string strFileName;
strFileName = Guid.NewGuid().ToString() + ".doc";
object o_filename = Server.MapPath("NewFile/") + strFileName;
object o_format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatEncodedText;
object o_endings = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF;
object o_null = Missing.Value;

oDoc.PageSetup.TopMargin = oWord.MillimetersToPoints(14.17F);
oDoc.PageSetup.BottomMargin = oWord.MillimetersToPoints(14.17F);
oDoc.PageSetup.RightMargin = oWord.MillimetersToPoints(14.17F);
oDoc.PageSetup.LeftMargin = oWord.MillimetersToPoints(14.17F);



oWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;

//ENTERING A PARAGRAPH BREAK "ENTER"

oWord.Selection.TypeParagraph();



//INSERTING THE PAGE NUMBERS CENTRALLY ALIGNED IN THE PAGE FOOTER

//string docNumber = "1";

//string revisionNumber = "0";

oWord.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;

oWord.ActiveWindow.Selection.Font.Name = "Arial";

oWord.ActiveWindow.Selection.Font.Size = 8;

// oWord.ActiveWindow.Selection.TypeText("Document #: " + docNumber + " " + revisionNumber);



//INSERTING TAB CHARACTERS


oWord.ActiveWindow.Selection.TypeText("");

oWord.ActiveWindow.Selection.TypeText("");



oWord.ActiveWindow.Selection.TypeText("Page ");

object CurrentPage = WdFieldType.wdFieldPage;

string str;
str = oWord.Selection.HeaderFooter.PageNumbers.Count.ToString();
oWord.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);

oWord.ActiveWindow.Selection.TypeText(" of ");

object TotalPages = WdFieldType.wdFieldNumPages;

// oWord.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, ref TotalPages, ref oMissing, ref oMissing);



//SETTING FOCUES BACK TO DOCUMENT

oWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
oWord.Selection.TypeParagraph();

oWord.Selection.Font.Bold = 1;

oWord.Selection.Font.Color = WdColor.wdColorAqua;

oWord.Selection.Font.Italic = 1;

oWord.Selection.Font.Underline = WdUnderline.wdUnderlineDashHeavy;
oWord.Selection.ClearFormatting();



oDoc.SaveAs(ref o_filename, ref o_format, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_endings, ref o_null);

//Assign null value to created object
oDoc = null;
oWord.Quit(ref o_null, ref o_null, ref o_null);
//For Open file
Response.Redirect("NewFile/" + strFileName);
}

if u have any query than send it.

Thanks & Regards
Jignesh Patel

No comments:

Post a Comment