Friday, May 22, 2009
Add blank page in word document using C#.Net
U can add blank page at particular page number in word document using below code.
object missing = System.Reflection.Missing.Value;
object fileName = @"D:\TestFile.docx";
object readOnly = false;
object isVisible = true;
//Start Word and open a document.
_Application oWord;
_Document oDoc;
oWord = new Application();
oWord.Visible = true;
oDoc = oWord.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing);
//Goto some specific page and insert a blank page or page break
object gotoPage = WdGoToItem.wdGoToPage;
object gotoNext = WdGoToDirection.wdGoToNext;
object gotoCount = null;
//Page Numer
object gotoName = "4";
oWord.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);
//Insert a blank page
oWord.Selection.InsertNewPage();
//Insert a page break
object breakPage = WdBreakType.wdPageBreak;
oWord.Selection.InsertBreak(ref breakPage);
Thanks & Regards
Jignesh Patel
Tuesday, May 19, 2009
Create Web Service in .Net
Here sample code for how to create web service and consume int in C#.Net.
Step 1 : Add new Web Service(.asmx) file using add New Item
Step 2 : Suppose i have add Operation.asmx file
Step 3 :Add namespace of web service
///
/// Summary description for Operation
///
[WebService(Namespace = "http://localhost:3870/ConnectMySql/Operation.asmx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
Step 4 : Create Function using [WebMethod]
//[WebMethod]
public int Addition(int a,int b)
{
int c;
c = a + b;
return c;
}
Step 5: Now build the web site and web service is created.
Now for Consume Web Service Step
Step 6: Add web service referance to another site using right click on project and add web referance.
Enter web service url http://localhost:3870/ConnectMySql/Operation.asmx and add namespace name for exa. "localhost".
Step 7 : Now add Namespace in page by
using localhost
Step 8 : Create object of Operation Class
Operation obj = new Operation();
int a = obj.Addition(5,2)
Response.Write(a.ToString());
Thanks & Regards
Jignesh Patel
Friday, May 15, 2009
Create Random number in .Net
public string CreateRandomCode()
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < 6; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(36);
if (temp != -1 && temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
Thanks & Regards
Jignesh Patel
Bind Treeview in .Net
Here the code for bind treeview up to four level.In this example i have bind Subject-->SubSubject-->Chapter-->Topic.
#region BIND TREE VIEW NODE
#region BIND Subject
private void BindMaterial()
{
strQuery = "SELECT Subject,SubjectId FROM V_GetStudentMaterial GROUP BY Subject,SubjectId ";
DataTable dtMaterial;
dtMaterial = ObjB.SelectMaterial(strQuery);
if(dtMaterial.Rows.Count>0)
{
tvMaterail.Nodes.Clear();
for (int iTreeSubject = 0; iTreeSubject < dtMaterial.Rows.Count; iTreeSubject++)
{
TreeNode SubjectNode = new TreeNode();
SubjectNode.Text = FormatStrDecode(dtMaterial.Rows[iTreeSubject]["Subject"].ToString());
SubjectNode.Value = dtMaterial.Rows[iTreeSubject]["SubjectId"].ToString();
SubjectNode.ToolTip = FormatStrDecode(dtMaterial.Rows[iTreeSubject]["Subject"].ToString());
SubjectNode.Expand();
//Bind Subject
tvMaterail.Nodes.Add(SubjectNode);
int intTreeSubjectId = int.Parse(SubjectNode.Value.ToString());
BindSubSubjectNodes(SubjectNode, intTreeSubjectId);
}
trMaterial.Visible = true;
lblMessage.Text = "";
}
else
{
lblMessage.Text = ReadXMLFile_Admin(FormName.Topic, Task.Materialnotfound);
trMaterial.Visible = false;
}
}
#endregion
#region BIND SUB SUBJECT NODE
private void BindSubSubjectNodes(TreeNode ParentSubjectNode, int intTreeSubjectId)
{
try
{
strQuery = "";
strQuery = "SELECT Subject,SubjectId,SubSubject,SubSubjectId FROM V_GetStudentMaterial WHERE SubjectId=" + intTreeSubjectId + " GROUP BY Subject,SubjectId,SubSubject,SubSubjectId";
DataTable dtSubSubject;
dtSubSubject = ObjB.SelectMaterial(strQuery);
if (dtSubSubject.Rows.Count > 0)
{
for (int intiSubSubject = 0; intiSubSubject < dtSubSubject.Rows.Count; intiSubSubject++)
{
TreeNode SubSubjectNode = new TreeNode();
SubSubjectNode.Text = dtSubSubject.Rows[intiSubSubject]["SubSubject"].ToString();
SubSubjectNode.Value = dtSubSubject.Rows[intiSubSubject]["SubSubjectId"].ToString();
SubSubjectNode.ToolTip = FormatStrDecode(dtSubSubject.Rows[intiSubSubject]["SubSubject"].ToString());
//Bind Sub Subject
ParentSubjectNode.ChildNodes.Add(SubSubjectNode);
int intSubSubjectId = int.Parse(SubSubjectNode.Value.ToString());
BindChildChapter(SubSubjectNode, intSubSubjectId);
ParentSubjectNode.Collapse();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region BIND SUB SUBJECT CHAPTER NODE
private void BindChildChapter(TreeNode ParentSubSubjectNode, int intTreeSubSubjectId)
{
try
{
strQuery = "";
strQuery = "SELECT Subject,SubjectId,SubSubject,SubSubjectId,ChapterId,ChapterName FROM V_GetStudentMaterial WHERE SubSubjectId=" + intTreeSubSubjectId + " GROUP BY Subject,SubjectId,SubSubject,SubSubjectId,ChapterId,ChapterName";
DataTable dtChapter;
dtChapter = ObjB.SelectMaterial(strQuery);
if (dtChapter.Rows.Count > 0)
{
for (int intiChapter = 0; intiChapter < dtChapter.Rows.Count; intiChapter++)
{
TreeNode ChapterNode = new TreeNode();
ChapterNode.Text = dtChapter.Rows[intiChapter]["ChapterName"].ToString();
ChapterNode.Value = dtChapter.Rows[intiChapter]["ChapterId"].ToString();
ChapterNode.ToolTip = FormatStrDecode(dtChapter.Rows[intiChapter]["ChapterName"].ToString());
//Bind Chapter
ParentSubSubjectNode.ChildNodes.Add(ChapterNode);
int intChapterId = int.Parse(ChapterNode.Value.ToString());
BindChildChapterTopic(ChapterNode, intChapterId);
ParentSubSubjectNode.Collapse();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region BIND CHAPTER TOPIC NODE
private void BindChildChapterTopic(TreeNode ParentChapterNode, int intTreeChapterId)
{
try
{
strQuery = "";
strQuery = "SELECT Subject,SubjectId,SubSubject,SubSubjectId,ChapterId,ChapterName,TopicId,Topic FROM V_GetStudentMaterial WHERE ChapterId=" + intTreeChapterId + " GROUP BY Subject,SubjectId,SubSubject,SubSubjectId,ChapterId,ChapterName,TopicId,Topic";
DataTable dtTopic;
dtTopic = ObjB.SelectMaterial(strQuery);
if (dtTopic.Rows.Count > 0)
{
for (int intiTopic = 0; intiTopic < dtTopic.Rows.Count; intiTopic++)
{
TreeNode ChapterNode = new TreeNode();
ChapterNode.Text = dtTopic.Rows[intiTopic]["Topic"].ToString();
ChapterNode.Value = dtTopic.Rows[intiTopic]["TopicId"].ToString();
ChapterNode.ToolTip = "Click here to view " + FormatStrDecode(dtTopic.Rows[intiTopic]["Topic"].ToString());
ChapterNode.NavigateUrl = "ContentDetail.aspx?TopicId=" + dtTopic.Rows[intiTopic]["TopicId"].ToString();
//Bind Topic
ParentChapterNode.ChildNodes.Add(ChapterNode);
ParentChapterNode.Collapse();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#endregion
Thanks & Regards
Jignesh Patel
Thursday, May 14, 2009
Add an Image in Word Document using .Net
Using the sample code u can add image in word document.
//Insert Image
string strSiteURL ="www.url.com"
object oRngHeader;
Microsoft.Office.Interop.Word.Paragraph oParaHeader;
oRngHeader = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oParaHeader = oDoc.Content.Paragraphs.Add(ref oRngHeader);
wrdSelection = oWord.Selection;
wrdSelection.TypeText(oParaHeader.Range.Text);
string strObjHeaderFile = strSiteURL + "images/Logo.jpg";
Object oAnsHeader = oParaHeader.Range;
//oParaHeader.Format.SpaceAfter = 1;
oParaHeader.Format.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
oParaHeader.Application.Selection.InlineShapes.AddPicture(strObjHeaderFile, ref o_missvalue, ref o_missvalue, ref oRngHeader);
Thanks & Regards
Jignesh Patel
Create Word Document in .Net
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
