Save pdf to a Network folder - iTextSharp

Malam Malam 246 Reputation points
2022-02-22T16:19:06.65+00:00

The following code save pdf to local app folder; how do I save to a network folder?
I have tried different way by providing network path but it always adds network path string to local drive path like:
C:\WebApplication\WebApplication\FileServer\FileFoldertemp\employee.pdf

protected void Save(object sender, EventArgs e)  
{  
    string sDate = txtDate.Value;  
    string name = txtHeadName.Value;  
  
    Document document = new Document(PageSize.LETTER, 100f, 100f, 10f, 10f);  
    iTextSharp.text.Font NormalFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);  
  
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())  
    {  
        PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);  
        Phrase phrase = null;  
        PdfPCell cell = null;  
        PdfPTable table = null;  
        BaseColor color = null;  
  
        document.Open();  
  
        //Header Table  
        table = new PdfPTable(2);  
        table.TotalWidth = 550f;  
        table.LockedWidth = true;  
        table.SetWidths(new float[] { .3f, 2.3f });  
  
        cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);  
        cell.VerticalAlignment = PdfPCell.ALIGN_TOP;  
        cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;  
        table.AddCell(cell);  
  
        //Separater Line  
        color = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"));  
        document.Add(table);  
  
        table = new PdfPTable(2);  
        table.HorizontalAlignment = Element.ALIGN_LEFT;  
        table.SetWidths(new float[] { 0.3f, 1f });  
        table.SpacingBefore = 10f;  
  
        cell = PhraseCell(new Phrase("Client Record", FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.UNDERLINE, BaseColor.BLACK)), PdfPCell.ALIGN_CENTER);  
        cell.Colspan = 2;  
        table.AddCell(cell);  
        cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);  
        cell.PaddingBottom = 30f;  
        table.AddCell(cell);  
  
        table = new PdfPTable(4);  
        table.TotalWidth = 800f;  
  
        table.SetWidths(new float[] { 4.5f, 4.5f, 2.35f, 1.75f });  
        table.TotalWidth = 440f;    
        table.LockedWidth = true;  
        table.SpacingBefore = 20f;  
        table.HorizontalAlignment = Element.ALIGN_RIGHT;  
  
        table.AddCell(PhraseCell(new Phrase("DATE:", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));  
        table.AddCell(PhraseCell(new Phrase(sDate, FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));  
        cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);  
        cell.Colspan = 4;  
        cell.PaddingBottom = 10f;  
        table.AddCell(cell);  
  
        string strUploadDestination = ConfigurationManager.AppSettings["UploadDestination"].ToString();  
        string sUser = "Temp";  
        string dmzPath = @strUploadDestination + "\\" + sUser + "\\";  
        string sFile = "MyFile";  
  
        document.Add(table);  
        document.Close();  
        byte[] bytes = memoryStream.ToArray();  
        memoryStream.Close();  
        Response.Clear();  
        Response.ContentType = "application/pdf";  
        string strSessVar = dmzPath + "StayInPlace _" + sID + ".pdf";  
        var uncPath = @"\\FileServer\FileFolder$\temp\employee_" + sFile + "_" + ID + ".pdf";  
        //Response.AddHeader("Content-Disposition", "attachment; filename=" + sFile + ";");  
        //Response.ContentType = "application/pdf";  
        //Response.Buffer = true;  
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);  
        //Response.BinaryWrite(bytes);  
        Response.WriteFile(Server.MapPath(uncPath));  
        Response.WriteFile(Server.MapPath(uncPath));  
        Response.End();  
        Response.Close();  
  
    }  
}  


  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. AgaveJoe 28,366 Reputation points
    2022-02-22T20:22:45.14+00:00

    The code is bit confusing. It looks like the code is trying to return an HTTP file stream to an client like a browser. It's not possible to tell a browser where to save the file. If the server has access to the file share then just save the file on the network share.

    Use the following pattern to save a memory stream to a file.

    var uncPath = @"\\FileServer\FileFolder$\temp\employee_" + sFile + "_" + ID + ".pdf";
    FileStream file = new FileStream(uncPath, FileMode.Create, FileAccess.Write);
    memoryStream .WriteTo(file);
    file.Close();
    memoryStream.Close();
    
    0 comments No comments

  2. Malam Malam 246 Reputation points
    2022-02-22T21:29:26.02+00:00

    I get an error:
    Cannot access a closed Stream.
    Line 510: memoryStream.WriteTo(file);


  3. Malam Malam 246 Reputation points
    2022-02-23T12:58:32.007+00:00

    Here is the code and the full error is towards the bottom:

    table.AddCell(PhraseCell(new Phrase("Name:", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));  
    phrase = new Phrase(new Chunk(sName + "\n\n", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));  
    table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));  
    cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);  
    cell.Colspan = 2;  
    cell.PaddingBottom = 10f;  
    table.AddCell(cell);  
      
    document.Add(table);  
    document.Close();  
      
    var uncPath1 = @"\\MyServer\MyFolder$\temp\employee.pdf";  
    FileStream file = new FileStream(uncPath1, FileMode.Create, FileAccess.Write);  
    memoryStream.WriteTo(file);  
    file.Close();  
    memoryStream.Close();  
    

    Server Error in '/' Application.
    Cannot access a closed Stream.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ObjectDisposedException: Cannot access a closed Stream.

    Source Error:

    Line 481: var uncPath1 = @"\MyServer\MyFolder$\temp\employee_" + sFile + "_" + ID + ".pdf";
    Line 482: FileStream file = new FileStream(uncPath1, FileMode.Create, FileAccess.Write);
    Line 483: memoryStream.WriteTo(file);
    Line 484: file.Close();
    Line 485: memoryStream.Close();

    0 comments No comments

  4. Malam Malam 246 Reputation points
    2022-02-23T15:49:52.203+00:00

    I was able to resolve the issue by using the code below:
    table.AddCell(PhraseCell(new Phrase("Name:", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
    phrase = new Phrase(new Chunk(sName + "\n\n", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
    table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
    cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
    cell.Colspan = 2;
    cell.PaddingBottom = 10f;
    table.AddCell(cell);

     document.Add(table);
     document.Close();
    
    ***byte[] bytes = memoryStream.ToArray();
    string uncPath1 = @"\\MyServer\MyFolder$\temp\";
    File.WriteAllBytes(SharedPath + "employee.pdf", bytes);***
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.