Explanation:
Here i used Microsoft.Office.Interop.word dll. This one in-built dll while installing Visual studio 2008 & above.
Also How to convert aspx to word and word to PDF.
And also after converted our pdf document to send e-mail with pdf attachment through GMAIL.
For converting to PDF & Send Mail u must follow the steps:
This is the 2007 Microsoft Office Add-in: Microsoft Save as PDF.
we should download this add-in and install to our machine.
https://amzn.to/2tanXtf
It's must for Installed on where pdf coding is running.
http://www.microsoft.com/en-us/download/confirmation.aspx?id=9943
And also
do in Page Directive that EnableEventValidation="false" It's must. because here i used RendorControl. It throws error. after that declaration of this that error won't come again.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PDF._Default" EnableEventValidation="false" %>
Step 1:
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PDF._Default" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0" width="1258px" align="center">
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="btn_PDF" runat="server" Text="PDF" OnClick="btn_PDF_Click" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False"
ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country Code">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Ccode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State Code">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("SCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City Code">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("CyCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:Image ID="Img1" runat="server" ImageUrl="../Images/Koala.jpg" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:Image ID="Img2" runat="server" ImageUrl="../Images/Tulips.jpg" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 2:
Right click on PDF project --> Add Reference and do instructions given by followed Images:
Step 3:
Default.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Data.SqlClient;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;
using System.Diagnostics;
using System.IO;
using System.Net.Mail;
using System.Net;
namespace PDF
{
public partial class _Default : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["testCon"].ConnectionString;
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter sqlda;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
}
private void FillGrid()
{
using (con = new SqlConnection(conStr))
{
con.Open();
string selQry = string.Format("select top 5 ID,name,Ccode,SCode,CyCode from test_Details");
using (cmd = new SqlCommand(selQry, con))
{
sqlda = new SqlDataAdapter(cmd);
dt = new DataTable();
sqlda.Fill(dt);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btn_PDF_Click(object sender, EventArgs e)
{
string MailId = string.Empty; //Given Mail Id by user
string EmailId = string.Empty; //Constant Mail ID
string Name = string.Empty;
string Sub_Name = string.Empty;
//Convert aspx to word
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.Page.RenderControl(htw);
string htmtags = sw.ToString();
byte[] b = new byte[htmtags.Length];
b = System.Text.Encoding.UTF8.GetBytes(htmtags);
MemoryStream ms = new MemoryStream(b, true);
string renderedPage = sw.ToString();
//End Word Convert
//Start Get Task Manager Process, if already working the WINWORD.exe process then close that process
Process[] processes = Process.GetProcessesByName("WINWORD");
foreach (Process process in processes)
{
process.Close();
process.Dispose();
}
//Defining a path with File Time
//object wordpath = HttpContext.Current.Server.MapPath(@"C:\Users\Admin\Documents\Visual Studio 2008\Projects\PDF\PDF\DOC-PDF\DOC-PDF\Test_" + DateTime.Now.ToFileTime().ToString() + ".doc");
//object pdfpath = HttpContext.Current.Server.MapPath(@"C:\Users\Admin\Documents\Visual Studio 2008\Projects\PDF\PDF\DOC-PDF\DOC-PDF\Test_" + DateTime.Now.ToFileTime().ToString() + ".pdf");
object wordpath = @"C:\Users\Admin\Documents\Visual Studio 2008\Projects\PDF\PDF\DOC-PDF\Test_" + DateTime.Now.ToFileTime().ToString() + ".doc";
object pdfpath = @"C:\Users\Admin\Documents\Visual Studio 2008\Projects\PDF\PDF\DOC-PDF\Test_" + DateTime.Now.ToFileTime().ToString() + ".pdf";
File.WriteAllText(wordpath.ToString(), renderedPage);
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
Object oMissing = System.Reflection.Missing.Value;
wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
word.Visible = false;
object filepath = wordpath;
Object confirmconversion = System.Reflection.Missing.Value;
Object readOnly = false;
Object oallowsubstitution = System.Reflection.Missing.Value;
wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
word.Visible = false;
//To find out the WINWORD.exe process and set RealTime Priority to WINWORD.exe
//This is used to quick process
Process[] proces = Process.GetProcessesByName("WINWORD");
foreach (Process proc in proces)
{
proc.PriorityClass = ProcessPriorityClass.RealTime;
}
object fileFormat = WdSaveFormat.wdFormatPDF;
Object saveto = pdfpath;
//Converting Word Doc to PDF
wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing,
ref oMissing);
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
//To terminate the alredy opend process
Process[] processe = Process.GetProcessesByName("WINWORD");
foreach (Process proce in processe)
{
proce.Close();
proce.Dispose();
}
//Start to send Mail Process [GMAIL]
MailId = "venkadeshbabu.gr@gmail.com";
Name = "VENKADESH BABU G R";
Sub_Name = "Babu";
EmailId = "veb.610@gmail.com";
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("test@gmail.com", Name);
if (MailId.Length > 0)
{
msg.To.Add(EmailId + ',' + MailId);
}
else
{
msg.To.Add(EmailId);
}
msg.Subject = Sub_Name;
msg.Body = "Attached document";
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 587;
// smtp.Port=25; //While use in Server
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
NetworkCredential credential = new NetworkCredential("test@gmail.com", "ur password");
smtp.Credentials = credential;
System.Net.Mail.Attachment Attachment = new System.Net.Mail.Attachment(pdfpath.ToString());
//Attachment.Name = "WebQuotes.pdf"; // set attachment name here
msg.Attachments.Add(Attachment);
smtp.Send(msg);
msg.Dispose();
File.Delete(wordpath.ToString());
File.Delete(pdfpath.ToString());
}
}
}
Output:
This image describes after converted aspx to word and word to pdf in specified folder.
Here i used Microsoft.Office.Interop.word dll. This one in-built dll while installing Visual studio 2008 & above.
Also How to convert aspx to word and word to PDF.
And also after converted our pdf document to send e-mail with pdf attachment through GMAIL.
For converting to PDF & Send Mail u must follow the steps:
This is the 2007 Microsoft Office Add-in: Microsoft Save as PDF.
we should download this add-in and install to our machine.
https://amzn.to/2tanXtf
It's must for Installed on where pdf coding is running.
http://www.microsoft.com/en-us/download/confirmation.aspx?id=9943
And also
do in Page Directive that EnableEventValidation="false" It's must. because here i used RendorControl. It throws error. after that declaration of this that error won't come again.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PDF._Default" EnableEventValidation="false" %>
Step 1:
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PDF._Default" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0" width="1258px" align="center">
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="btn_PDF" runat="server" Text="PDF" OnClick="btn_PDF_Click" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False"
ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country Code">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Ccode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State Code">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("SCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City Code">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("CyCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:Image ID="Img1" runat="server" ImageUrl="../Images/Koala.jpg" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td align="center">
<asp:Image ID="Img2" runat="server" ImageUrl="../Images/Tulips.jpg" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 2:
Right click on PDF project --> Add Reference and do instructions given by followed Images:
Step 3:
Default.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Data.SqlClient;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;
using System.Diagnostics;
using System.IO;
using System.Net.Mail;
using System.Net;
namespace PDF
{
public partial class _Default : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["testCon"].ConnectionString;
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter sqlda;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
}
private void FillGrid()
{
using (con = new SqlConnection(conStr))
{
con.Open();
string selQry = string.Format("select top 5 ID,name,Ccode,SCode,CyCode from test_Details");
using (cmd = new SqlCommand(selQry, con))
{
sqlda = new SqlDataAdapter(cmd);
dt = new DataTable();
sqlda.Fill(dt);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btn_PDF_Click(object sender, EventArgs e)
{
string MailId = string.Empty; //Given Mail Id by user
string EmailId = string.Empty; //Constant Mail ID
string Name = string.Empty;
string Sub_Name = string.Empty;
//Convert aspx to word
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.Page.RenderControl(htw);
string htmtags = sw.ToString();
byte[] b = new byte[htmtags.Length];
b = System.Text.Encoding.UTF8.GetBytes(htmtags);
MemoryStream ms = new MemoryStream(b, true);
string renderedPage = sw.ToString();
//End Word Convert
//Start Get Task Manager Process, if already working the WINWORD.exe process then close that process
Process[] processes = Process.GetProcessesByName("WINWORD");
foreach (Process process in processes)
{
process.Close();
process.Dispose();
}
//Defining a path with File Time
//object wordpath = HttpContext.Current.Server.MapPath(@"C:\Users\Admin\Documents\Visual Studio 2008\Projects\PDF\PDF\DOC-PDF\DOC-PDF\Test_" + DateTime.Now.ToFileTime().ToString() + ".doc");
//object pdfpath = HttpContext.Current.Server.MapPath(@"C:\Users\Admin\Documents\Visual Studio 2008\Projects\PDF\PDF\DOC-PDF\DOC-PDF\Test_" + DateTime.Now.ToFileTime().ToString() + ".pdf");
object wordpath = @"C:\Users\Admin\Documents\Visual Studio 2008\Projects\PDF\PDF\DOC-PDF\Test_" + DateTime.Now.ToFileTime().ToString() + ".doc";
object pdfpath = @"C:\Users\Admin\Documents\Visual Studio 2008\Projects\PDF\PDF\DOC-PDF\Test_" + DateTime.Now.ToFileTime().ToString() + ".pdf";
File.WriteAllText(wordpath.ToString(), renderedPage);
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
Object oMissing = System.Reflection.Missing.Value;
wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
word.Visible = false;
object filepath = wordpath;
Object confirmconversion = System.Reflection.Missing.Value;
Object readOnly = false;
Object oallowsubstitution = System.Reflection.Missing.Value;
wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
word.Visible = false;
//To find out the WINWORD.exe process and set RealTime Priority to WINWORD.exe
//This is used to quick process
Process[] proces = Process.GetProcessesByName("WINWORD");
foreach (Process proc in proces)
{
proc.PriorityClass = ProcessPriorityClass.RealTime;
}
object fileFormat = WdSaveFormat.wdFormatPDF;
Object saveto = pdfpath;
//Converting Word Doc to PDF
wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing,
ref oMissing);
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
//To terminate the alredy opend process
Process[] processe = Process.GetProcessesByName("WINWORD");
foreach (Process proce in processe)
{
proce.Close();
proce.Dispose();
}
//Start to send Mail Process [GMAIL]
MailId = "venkadeshbabu.gr@gmail.com";
Name = "VENKADESH BABU G R";
Sub_Name = "Babu";
EmailId = "veb.610@gmail.com";
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress("test@gmail.com", Name);
if (MailId.Length > 0)
{
msg.To.Add(EmailId + ',' + MailId);
}
else
{
msg.To.Add(EmailId);
}
msg.Subject = Sub_Name;
msg.Body = "Attached document";
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 587;
// smtp.Port=25; //While use in Server
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
NetworkCredential credential = new NetworkCredential("test@gmail.com", "ur password");
smtp.Credentials = credential;
System.Net.Mail.Attachment Attachment = new System.Net.Mail.Attachment(pdfpath.ToString());
//Attachment.Name = "WebQuotes.pdf"; // set attachment name here
msg.Attachments.Add(Attachment);
smtp.Send(msg);
msg.Dispose();
File.Delete(wordpath.ToString());
File.Delete(pdfpath.ToString());
}
}
}
Output:
This image describes after converted aspx to word and word to pdf in specified folder.
After send mail files are deleted that shows followed by image:
This image describes after sent mail successfully and mail show in Inbox..
After Download and Open PDF files are as shown as follows:
Please help me, i am having problem to set height and width of image ,it works in word but when converts word to pdf it can not able to set height and width of the image.. please help me.. my email is prateek.bhardwaj60@gmail.com
ReplyDeleteUse 'img src="image.png" height="100" width="400"' tag on the code
DeleteI have done Convert Ms Word to pdf, but On Web Server I have uploaded then Not convert and send mail, so please help me how can i Send mail With that attachment on web server? Help me???????????
ReplyDeleteIn Local host It's work , but On web sever it's not work, so help me how It's work?
ReplyDeleteHi sujit,
DeleteI think u download and install the following link on server as well as.
"It's must for Installed on where pdf coding is running.
http://www.microsoft.com/en-us/download/confirmation.aspx?id=9943"
also regarding send mail not working means make sure your server has provide permission for sending and receiving gmail.
try it.. both my conversion and send mail was successfully worked on my server..
Thanks,
Venkadesh Babu
please send vb,net code with asp.net
ReplyDeleteHI i am able to convert aspx to pdf but css are missing and images are not displaying plz help
ReplyDeleteHi Venkadesh ,
ReplyDeleteWhen I host my application to IIS i am getting Error like this:-
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied.
Please help me how to fixed this issue?
Please tell me how can i convert image when i got image through image handler.
ReplyDeleteConvert Word to PDF in C#, without need office word interop installed in the project computer, you can convert word to pdf free in your server with asp.net application https://www.iditect.com/tutorial/docx-to-pdf/
ReplyDelete