0

how can we Replace pdf files in server folder using asp.net with c#.? when we are generating Pdf that time is a not problem but pdf Updation is a not working this problem we are faced in at the time of sending mail attachment via. can u help me.i have tried this one?

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 = "";
mkl
  • 90,588
  • 15
  • 125
  • 265
user3259635
  • 51
  • 1
  • 12
  • sir see above i given in my source code – user3259635 Feb 03 '14 at 07:29
  • 1
    *pdf Updation is a not working* - Do you try to update using this code by replacing the existing file by a newly generated one? *this problem we are faced in at the time of sending mail attachment via* - what actually **is** the problem? Is there an exception? Is nothing sent? Do you get a blue screen? Does the moon hit earth? – mkl Feb 03 '14 at 10:30
  • s sir newly generated Pdf ex:Q1.pdf fisrt time genreated that Pdf again user modified that quotation they want again same Quotation Q1.pdf at the time i done know the coding of Replace pdf Can u help Me....sir – user3259635 Feb 04 '14 at 07:25
  • Your existing code should already do that, shouldn't it? After all it starts by replacing the original file (though there is some unnecessary variable duplication). As you ask here about it, your code seems to fail in some respect. Thus, what actually **is** the problem? Is there an exception? Is nothing sent? Do you get a blue screen? Does the moon hit earth? – mkl Feb 04 '14 at 07:56
  • sir i got this exception error when im replacing Pdf file from server folder..'.pdf file can be used by another person' can u help ? – user3259635 Feb 04 '14 at 09:25
  • *exception error when im replacing* - That sounds like the existing PDF is in use by other code at the time you try to delete it. You have to find out who else uses that PDF. It might be some code which opened the PDF file but did not close it, counting on garbage collection to eventually close it. – mkl Feb 04 '14 at 09:33
  • sir i have one doubt how we can add Pdf default header and footer pdf Generating time can u help? – user3259635 Feb 04 '14 at 11:56
  • Have a look at the `MovieHistory2` sample referenced in the answer to [itextsharp adding header and footer, displayed inside the page margin not outside the page margin.](http://stackoverflow.com/questions/17739364/itextsharp-adding-header-and-footer-displayed-inside-the-page-margin-not-outsid/17741016#17741016) and the `Stationery` sample referenced in the answer to [How to add image background to pdf for every page?](http://stackoverflow.com/questions/14812955/how-to-add-image-background-to-pdf-for-every-page/14813581#14813581). If unclear points remain, create a new stackoverflow question. – mkl Feb 04 '14 at 12:42
  • sir how can we add page count option pdf creation time – user3259635 Feb 05 '14 at 06:42
  • Look at the [iText in Action — 2nd Edition](http://itextpdf.com/book/) samples referenced for the [KEYWORD: PAGE NUMBERS > PAGE X OF Y](http://itextpdf.com/themes/keyword.php?id=223); the corresponding C# samples can be found at [this site](http://kuujinbo.info/iTextInAction2Ed/). – mkl Feb 05 '14 at 07:34

0 Answers0