Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I am using iTextSharper but the output gets : The process cannot access the file 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\Upload\KingSize_2013-04-29.pdf' because it is being used by another process.
 
 string strFileName =
                    CleanUp(LabelFirstName.Text + "_" + LabelLastName.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd") +
                            "." + DropDownListDownloadCv0.SelectedItem.Text);
 
                if (theEmpl != null)
                {
 

                    //var objfileinfo = new FileInfo(strFileName);

                    var document = new iTextSharp.text.Document(PageSize.A4);
 
                    
                    PdfWriter.GetInstance(document,
                                          new FileStream(Server.MapPath("~/Upload/") + strFileName,
                                                         FileMode.Create));
 
              
 
                    var sb = new StringBuilder();
                    var sw = new StringWriter(sb);
 
                    document.Open();
                    var hTextWriter = new HtmlTextWriter(sw);
 

 
                    hTextWriter.WriteBeginTag("table border=1 cellpadding=15");
 
                    hTextWriter.Write(HtmlTextWriter.TagRightChar);
 
                    hTextWriter.WriteBeginTag("tr");
                    hTextWriter.Write(HtmlTextWriter.TagRightChar);
                    hTextWriter.WriteBeginTag("td");
                    hTextWriter.Write(HtmlTextWriter.TagRightChar);
 
                    var styles = new iTextSharp.text.html.simpleparser.StyleSheet();
 

                    styles.LoadStyle("Loggo", "float-right", "20pt");
 

                    ImagePicture.ImageUrl = theEmpl.image;
                    ImagePicture.ImageAlign = ImageAlign.Left;
                    ImagePicture.RenderControl(hTextWriter);
 

 
                    var s = new Label
                                {
                                    Text = "<br/>"
                                };
 
                    s.RenderControl(hTextWriter);
 
                    hTextWriter.Write("<div class=\"Loggo\">");
                    Loggo.ImageUrl = theEmpl.logo;
                    Loggo.RenderControl(hTextWriter);
 
                    hTextWriter.Write("</div>");
 

                    var posfirstLast = new Label
                                           {
                                               Text =
                                                   "<br/>" + "<p align='left'>" + LabelPosition.Text + " " +
                                                   LabelFirstName.Text + " " + LabelLastName.Text + "</p>" +
                                                   "<br/>"
                                           };
                    posfirstLast.RenderControl(hTextWriter);
                    var starkaabout3 = new Label
                                           {
                                               Text = Label3StrongAbout.Text
                                           };
 
                    starkaabout3.RenderControl(hTextWriter);
 

 
                    SetSkillAndLevelMax3();
                    for (int i = 0; i < RepeaterStrongestSkillsMax2.Controls.Count; i++)
                    {
                        var skill = RepeaterStrongestSkillsMax2.FindControl("skill" + i);
                        var pie = RepeaterStrongestSkillsMax2.FindControl("pie" + i);
                        if (skill != null & pie != null)
                        {
                            pie.RenderControl(hTextWriter);
                            skill.RenderControl(hTextWriter);
                        }
 
                    }
 
                    var lAbout = new Label
                                     {
 
                                         Text = "<br/><br/>" + LabelAbout.Text + "<br/><br/><br/>"
                                     };
 
                    lAbout.RenderControl(hTextWriter);
 

 

                    var back = new Label
                                   {
                                       Text =
                                           "<br/>" + lblbackground.Text + "<br/>" + lbldateofbirth.Text +
                                           " " + LabelDateOfBirth.Text + "<br/>" + lblphonenumber.Text + " " +
                                           LabelPhoneNumber.Text + "<br/>" + lblemail.Text + " " +
                                           LabelEmail.Text + "<br/>"
                                   };
                    back.RenderControl(hTextWriter);
 
                    var lang = new Label
                                   {
                                       Text = "<p align='right'>" + lbllanguages.Text + "</p>"
                                   };
 
                    lang.RenderControl(hTextWriter);
 

                    foreach (var lLang in BulletedListLanguages.Items)
                    {
                        hTextWriter.WriteLine("<p align='right'>" + " - " + lLang + "</p>");
                    }
 
                    var lblPre = new Label
                                     {
                                         Text = "<p align='left'>" + lblprevwork.Text + "</p>"
                                     };
 
                    lblPre.RenderControl(hTextWriter);
 
                    foreach (var lPre in BulletedListPrevWork.Items)
                    {
                        hTextWriter.WriteLine("<p align='left'>" + " - " + lPre + "</p>");
 
                    }
 
                    var lblEduca = new Label
                                       {
                                           Text = "<p align='right'>" + lbleducations.Text + "</p>"
                                       };
 
                    lblEduca.RenderControl(hTextWriter);
 
                    foreach (var lEdu in BulletedListEducation.Items)
                    {
                        hTextWriter.WriteLine("<p align='right'>" + " - " + lEdu + "</p>");
 
                    }
 
                    var lblCours = new Label
                                       {
                                           Text = "<p align='left'>" + lblcourses.Text + "</p>"
                                       };
 
                    lblCours.RenderControl(hTextWriter);
 
                    foreach (var lCour in BulletedListCourses.Items)
                    {
                        hTextWriter.WriteLine("<p align='left'>" + " - " + lCour + "</p>");
 
                    }
 

                    var lblBuss = new Label
                                      {
                                          Text = "<p align='left'>" + lblbusniess.Text + "</p>"
                                      };
 
                    lblBuss.RenderControl(hTextWriter);
 
                    foreach (var lBuss in BulletedListBusiness.Items)
                    {
                        hTextWriter.WriteLine("<p align='left'>" + " - " + lBuss + "</p>");
 
                    }
 

                    var lblCert = new Label
                                      {
 
                                          Text = "<p align='right'>" + lblcert.Text + "</p>"
                                      };
 
                    lblCert.RenderControl(hTextWriter);
 
                    foreach (var lCert in BulletedListCertifications.Items)
                    {
                        hTextWriter.WriteLine("<p align='right'>" + " - " + lCert + "</p>");
 
                    }
 
                    var tech = new Label
                                   {
                                       Text = lbltechnicalskills.Text + "</br>"
                                   };
 
                    tech.RenderControl(hTextWriter);
 
                    var lblDataBas = new Label
                                         {
 
                                             Text = "<p align='left'>" + LabelDataBas.Text + "</p>"
                                         };
 
                    lblDataBas.RenderControl(hTextWriter);
 
                    foreach (var lDataBas in BulletedListDataBase.Items)
                    {
                        hTextWriter.WriteLine("<p align='left'>" + " - " + lDataBas + "</p>");
 
                    }
 
                    var lblProgramming = new Label
                                             {
 
                                                 Text = "<p align='left'>" + lblprogramming.Text + "</p>"
                                             };
 
                    lblProgramming.RenderControl(hTextWriter);
 
                    foreach (var lProgramming in BulletedListProgramming.Items)
                    {
                        hTextWriter.WriteLine("<p align='left'>" + " - " + lProgramming + "</p>");
 
                    }
 
                    var lblMethods = new Label
                                         {
 
                                             Text = "<p align='left'>" + lblmethods.Text + "</p>"
                                         };
 
                    lblMethods.RenderControl(hTextWriter);
 
                    foreach (var lMethods in BulletedListMethods.Items)
                    {
                        hTextWriter.WriteLine("<p align='left'>" + " - " + lMethods + "</p>");
 
                    }
 
                    var lblDevop = new Label
                                       {
 
                                           Text = "<p align='left'>" + lbldevop.Text + "</p>"
                                       };
 
                    lblDevop.RenderControl(hTextWriter);
 
                    foreach (var lDevop in BulletedDevelopmentTools.Items)
                    {
                        hTextWriter.WriteLine("<p align='left'>" + " - " + lDevop + "</p>");
 
                    }
 
                    var lblWeb = new Label
                                     {
 
                                         Text = "<p align='left'>" + lblwebserver.Text + "</p>"
                                     };
 
                    lblWeb.RenderControl(hTextWriter);
 
                    foreach (var lWeb in BulletedListWebServer.Items)
                    {
                        hTextWriter.WriteLine("<p align='left'>" + " - " + lWeb + "</p>");
 
                    }
 
                    hTextWriter.WriteEndTag("td");
                    hTextWriter.WriteEndTag("tr");
                    hTextWriter.WriteEndTag("table");
 
                    var brrr = new Label
                                   {
                                       Text = "<br/>"
                                   };
                    brrr.RenderControl(hTextWriter);
 
                    hTextWriter.WriteBeginTag("table border=1 cellpadding=15");
                    hTextWriter.Write(HtmlTextWriter.TagRightChar);
 
                    hTextWriter.WriteBeginTag("tr");
                    hTextWriter.Write(HtmlTextWriter.TagRightChar);
                    hTextWriter.WriteBeginTag("td");
                    hTextWriter.Write(HtmlTextWriter.TagRightChar);
 
                    if (RadioButtonListChooseLang.SelectedItem.Text == "Swedish")
 
                    {
                        
                        GetAlSwedishlAssigments(theEmpl);
 
                        var lbl = new Label
                                      {
                                          Text = "<h1><br/>" + "Uppdrag" + "<u/></h1>"
                                      };
                        lbl.RenderControl(hTextWriter);
                        for (int i = 0; i < RepeaterAssigments.Controls.Count; i++)
                        {
                            var comp = RepeaterAssigments.FindControl("comp" + i);
                            var sect = RepeaterAssigments.FindControl("sect" + i);
                            var area = RepeaterAssigments.FindControl("are" + i);
                            var from = RepeaterAssigments.FindControl("fro" + i);
                            var to = RepeaterAssigments.FindControl("too" + i);
                            var description = RepeaterAssigments.FindControl("des" + i);
                            var tool = RepeaterAssigments.FindControl("tool" + i);
                            var tec = RepeaterAssigments.FindControl("tec" + i);
                            var reference = RepeaterAssigments.FindControl("ref" + i);
                            var namesectorLit = RepeaterAssigments.FindControl("NamesectorLit" + i);
                            var nameAreaLit = RepeaterAssigments.FindControl("NameAreaLit" + i);
                            var nameTechLit = RepeaterAssigments.FindControl("NameTechLit" + i);
                            var nameToolLite = RepeaterAssigments.FindControl("NameToolLit" + i);
                            var nameRefLit = RepeaterAssigments.FindControl("NameRefLit" + i);
                            var spaceLit = RepeaterAssigments.FindControl("SpaceLit" + i);
                            var bottomBorderLit = RepeaterAssigments.FindControl("BottomBorderLit" + i);
 
                            var manadFran = RepeaterAssigments.FindControl("franmanad" + i);
                            var manadTill = RepeaterAssigments.FindControl("tillmanad" + i);
                            var nyttaKund = RepeaterAssigments.FindControl("kundnyttaSve" + i);
                            var arbetsuppgifterSve = RepeaterAssigments.FindControl("arbetsuppgifterSve" + i);
                            var rollSve = RepeaterAssigments.FindControl("rollSve" + i);
                            var arbetsuppgifterLit = RepeaterAssigments.FindControl("arbetsuppgifter" + i);
                            var rollLit = RepeaterAssigments.FindControl("roll" + i);
                            var uppdragLit = RepeaterAssigments.FindControl("updragSve" + i);
                            var kundnyttaLit = RepeaterAssigments.FindControl("NyttaKundSve" + i);
 
                            if (comp != null && from != null
                                && to != null && description != null && tool != null && tec != null
                                && nameTechLit != null && nameToolLite != null &&
                                spaceLit != null && bottomBorderLit != null &&
                                manadFran != null && manadTill != null && nyttaKund != null
                                && arbetsuppgifterSve != null && rollSve != null && arbetsuppgifterLit != null &&
                                rollLit != null && uppdragLit != null && kundnyttaLit != null)
                            {
 
                                comp.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                rollLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                rollSve.RenderControl(hTextWriter);
 
                                from.RenderControl(hTextWriter);
                                manadFran.RenderControl(hTextWriter);
                                to.RenderControl(hTextWriter);
                                manadTill.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                kundnyttaLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                nyttaKund.RenderControl(hTextWriter);
 
                                arbetsuppgifterLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                arbetsuppgifterSve.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                uppdragLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                description.RenderControl(hTextWriter);
                                nameToolLite.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                tool.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                nameTechLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                tec.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
 

                                spaceLit.RenderControl(hTextWriter);
 

                                
                                var image = new Image
                                                {
                                                    ImageUrl =
                                                        SPContext.Current.Web.Url + "/" +
                                                        SPContext.Current.File.Url +
                                                        "~/_layouts/images/Cv.Knowit/img_line.png"
                                                };
                                image.RenderControl(hTextWriter);
                                bottomBorderLit.RenderControl(hTextWriter);
 
                            }
                            else if(sect != null)
                            {
                                sect.RenderControl(hTextWriter);
                            }
                            else if(namesectorLit!=null)
                            {
                                namesectorLit.RenderControl(hTextWriter);  
                            }
                            
                           
                            if(area != null)
                            {
                                area.RenderControl(hTextWriter);
   
                            }
                            
                            else if(nameAreaLit!=null)
                            {
                                nameAreaLit.RenderControl(hTextWriter); 
                            }
                            
                            else if(reference != null)
                            {
                                reference.RenderControl(hTextWriter);  
                            }
                            
                            if(nameRefLit!=null)
                            {
                                nameRefLit.RenderControl(hTextWriter);   
                            }
                            
                            
                        }
                    }
                    else if (RadioButtonListChooseLang.SelectedItem.Text == "English")
                    {
                        GetAlEnglishAssigments(theEmpl);
                        var lbl = new Label
                        {
                            Text = "<h1><br/>" + "Assigments" + "</h1>"
                        };
                        lbl.RenderControl(hTextWriter);
                        for (int i = 0; i < RepeaterAssigments.Controls.Count; i++)
                        {
                            var comp = RepeaterAssigments.FindControl("comp" + i);
                            var sect = RepeaterAssigments.FindControl("sect" + i);
                            var area = RepeaterAssigments.FindControl("are" + i);
                            var from = RepeaterAssigments.FindControl("fro" + i);
                            var to = RepeaterAssigments.FindControl("too" + i);
                            var description = RepeaterAssigments.FindControl("des" + i);
                            var tool = RepeaterAssigments.FindControl("tool" + i);
                            var tec = RepeaterAssigments.FindControl("tec" + i);
                            var reference = RepeaterAssigments.FindControl("ref" + i);
                            var namesectorLit = RepeaterAssigments.FindControl("NamesectorLit" + i);
                            var nameAreaLit = RepeaterAssigments.FindControl("NameAreaLit" + i);
                            var nameTechLit = RepeaterAssigments.FindControl("NameTechLit" + i);
                            var nameToolLite = RepeaterAssigments.FindControl("NameToolLit" + i);
                            var nameRefLit = RepeaterAssigments.FindControl("NameRefLit" + i);
                            var spaceLit = RepeaterAssigments.FindControl("SpaceLit" + i);
                            var bottomBorderLit = RepeaterAssigments.FindControl("BottomBorderLit" + i);
 

                            if (comp != null && sect != null && area != null && from != null
                                && to != null && description != null && tool != null && tec != null &&
                                reference != null
                                && namesectorLit != null
                                && nameAreaLit != null && nameTechLit != null && nameToolLite != null &&
                                nameRefLit != null && spaceLit != null && bottomBorderLit != null)
                            {
 
                                comp.RenderControl(hTextWriter);
 

 
                                namesectorLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                sect.RenderControl(hTextWriter);
 
                                spaceLit.RenderControl(hTextWriter);
 
                                nameAreaLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                area.RenderControl(hTextWriter);
 

 
                                from.RenderControl(hTextWriter);
                                to.RenderControl(hTextWriter);
                                description.RenderControl(hTextWriter);
                                nameToolLite.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                tool.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                nameTechLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                tec.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
 
                                nameRefLit.RenderControl(hTextWriter);
                                spaceLit.RenderControl(hTextWriter);
                                reference.RenderControl(hTextWriter);
 
                                spaceLit.RenderControl(hTextWriter);
                                var image = new Image
                                {
                                    ImageUrl =
                                        SPContext.Current.Web.Url + "/" +
                                        SPContext.Current.File.Url +
                                        "~/_layouts/images/Cv.Knowit/img_line.png"
                                };
                                image.RenderControl(hTextWriter);
                                bottomBorderLit.RenderControl(hTextWriter);
                            }
 

 
                            hTextWriter.WriteEndTag("td");
                            hTextWriter.WriteEndTag("tr");
                            hTextWriter.WriteEndTag("table");
 
                            String strHtml1 = sw.ToString();
 
                            var hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
                            hw.SetStyleSheet(styles);
                            hw.Parse(new StringReader(strHtml1));
                            document.Close();
 

                            
                        }
 
                    }
                    
                    Response.ContentType = "Application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFileName);
                    Response.TransmitFile(Server.MapPath("~/Upload/") + strFileName);
                    Response.End();
                    
                  
                }
            }
        }
Posted 29-Apr-13 10:34am
Kurac1747
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is easy to investigate.
 
First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].
 
In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.
 
In same cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].
 
The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].
 
In your case, you use it with file name parameter:
handle.exe <file_name>
 
This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].
 
Good luck,
—SA
  Permalink  
Comments
Kurac1 at 29-Apr-13 16:55pm
   
how should i now which one i should dispose?
i get the error on this line Response.TransmitFile(Server.MapPath("~/Upload/") + strFileName);
Kurac1 at 29-Apr-13 17:01pm
   
i did like on this linke http://www.4guysfromrolla.com/articles/030911-1.aspx were do u se him use dispose(); ? i got it to work before when i hade less columns but then i added more and then i got the problem , dont now why... worked before..
Sergey Alexandrovich Kryukov at 29-Apr-13 17:10pm
   
Use handle.exe on the host reporting this problem; you will see what process holds the file handler...
Transmit file should dispose internally, this is not where you need to dispose anything; the file was left non-disposed elsewhere...
—SA
Kurac1 at 29-Apr-13 17:16pm
   
i have runned the program handlex.Exe it only run and nothing more what should i do '?
Sergey Alexandrovich Kryukov at 29-Apr-13 17:37pm
   
Not true. It always write some text on output. Did you use command line I've shown? It's hard to ask but... did you noticed this is a console application. Did you see the console? Do you know how?
—SA
Kurac1 at 29-Apr-13 17:40pm
   
Yes i did see the console with white text , but it stated and runned true nothing more only that
Sergey Alexandrovich Kryukov at 29-Apr-13 18:05pm
   
No! I just tried (with dummy file):
 
"Handle v3.51
Copyright (C) 1997-2013 Mark Russinovich
Sysinternals - www.sysinternals.com
 
No matching handles found."
 
Did you see it? Look, do you know how to run console? And you need to run is "As Administator" (of course). Which part you don't know?
 
If you do it with the currently open file, it will give you all the information. If you run it without parameters, it gives you a really long output.
 
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

thats because u are creating a filestream object but not closing it kindly use as below
 
FileStream file = new FileStream(stringPath);
 
and after closing your pdfDocument close your stream also
 
file.Close();
 
for ref you can use below link
 
Generating-PDF-using-ItextSharp-with-Footer-in-Csh
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100