如何使用带有c#的asp.net替换服务器文件夹中的pdf文件? 当我们生成Pdf时,时间不是问题,但pdf更新是一个无法解决的问题,我们在发送邮件附件时遇到了这个问题。 你可以帮我吗。我试过这个吗?
string fileloc = Server.MapPath("~/InvoiceFolder/" + lblprintinvoiceno_nmail.Text + ".pdf");
string newFile = Server.MapPath("~/InvoiceFolder/" + lblprintinvoiceno_nmail.Text + ".pdf");
if (System.IO.File.Exists(fileloc))
{
System.IO.File.Delete(newFile);
}
iTextSharp.text.Table table = new iTextSharp.text.Table(GridView222.Columns.Count);
table.Cellpadding = 5;
//Set the column widths of the table
int[] widths = new int[GridView222.Columns.Count];
for (int x = 0; x < GridView222.Columns.Count; x++)
{
widths[x] = (int)GridView222.Columns[x].ItemStyle.Width.Value;
string cellText = Server.HtmlDecode(GridView222.HeaderRow.Cells[x].Text);
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#DEDEE1"));
table.AddCell(cell);
}
table.SetWidths(widths);
//getting rows from GridView to table
string GridText = "";
for (int i = 0; i < GridView222.Rows.Count; i++)
{
if (GridView222.Rows[i].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < GridView222.Columns.Count; j++)
{
if (j == 0)
{
int slno = i + 1;
GridText = slno.ToString();
}
else
{
GridText = Server.HtmlDecode(GridView222.Rows[i].Cells[j].Text);
}
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(GridText);
table.AddCell(cell);
}
}
}
//Start Create PDF
Document Doc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlconvert1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
PdfWriter.GetInstance(Doc, new FileStream(fileloc, FileMode.Create));
HTMLWorker htmlparser = new HTMLWorker(Doc);
Doc.Open();
htmlparser.Parse(sr);
Doc.Add(table);
StringWriter sw1 = new StringWriter();
HtmlTextWriter hw1 = new HtmlTextWriter(sw1);
pnlconvert3.RenderControl(hw1);
StringReader sr1 = new StringReader(sw1.ToString());
htmlparser.Parse(sr1);
Doc.Close();
//End Create PDF
//Send Mail
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.live.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("user", "ped");
System.Net.Mail.MailMessage oMsg = new System.Net.Mail.MailMessage();
//MailAddress @add = new MailAddress("poobal@foresightgroup.in");
MailAddress @add = new MailAddress(txtinvoicemail.Text);
oMsg.CC.Add(new MailAddress("mail"));
oMsg.From = new MailAddress("mail");
oMsg.To.Add(@add);
oMsg.Subject = "Quotation";
oMsg.Attachments.Add(new Attachment(fileloc));
oMsg.Body = "Sir ," + "<br/>" + "kindly see the attached Invoice for your ready reference.";
oMsg.IsBodyHtml = true;
smtp.Send(oMsg);
oMsg.Attachments.Clear();
txtinvoicemail.Text = "";