question

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 asked MalamMalam-4042 answered

Save pdf to a Network folder - iTextSharp

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();
    
     }
 }


dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

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();



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered YijingSun-MSFT commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I assume the error is due to mistakenly placing the code after calling memoryStream.Close(); and not reviewing the code for correctness. Unfortunately, you didn't share the updates so I have no idea what you've changed.

Frankly, the code is very sloppy which masks the intent.

0 Votes 0 ·

Hi @MalamMalam-4042 ,
Please check your codes if you write below this before stream writing. Please put it after writing.

 memoryStream.Close();

Best regards,
Yijing Sun

0 Votes 0 ·
MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered

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();

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 answered

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);***


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.