Home > other >  c# save pdf file to specific path
c# save pdf file to specific path

Time:06-17

I am using Visual Studio 2012. I am converting an aspx page into a PDF file.

It works fine...

the code is like this

string attachment = "attachment; filename="   FileName   ".pdf";
       Response.ClearContent();
       Response.AddHeader("content-disposition", attachment);
       Response.ContentType = "application/pdf";
       StringWriter s_tw = new StringWriter();
       HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
       h_textw.AddStyleAttribute("font-size", "7pt");
       h_textw.AddStyleAttribute("color", "Black");
       pnlPrincipal.RenderControl(h_textw);//Name of the Panel  
       Document doc = new Document();
       doc = new Document(PageSize.A4, 5, 5, 15, 5);
       FontFactory.GetFont("Verdana", 80, iTextSharp.text.BaseColor.RED);
       PdfWriter.GetInstance(doc, Response.OutputStream);
       doc.Open();
       StringReader s_tr = new StringReader(s_tw.ToString());
       HTMLWorker html_worker = new HTMLWorker(doc);
       html_worker.Parse(s_tr);
       doc.Close();
       Response.Write( doc);

The problem I am facing is that it is saved in c:/Users/Download folder.

I want to save the file in an specific folder.

Where can I specified that?

Thanks

CodePudding user response:

You can write a document with

string folder = @"C:\Temp\";
string fileName = "mydocument.txt";
string fullPath = folder   fileName;
string data = "My string to write in a document";
File.WriteAllLines(fullPath, data);

// Read & print in console
string readText = File.ReadAllText(fullPath);  
Console.WriteLine(readText); 

CodePudding user response:

Finally I found the way... Instead of using Response I have to use FileStream

    System.IO.FileStream fs = new FileStream(Server.MapPath("PDFDIR")   "\\"   otd.NumeroOTD.ToString()   ".pdf", FileMode.Create);

        StringWriter s_tw = new StringWriter();
        HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
        h_textw.AddStyleAttribute("font-size", "7pt");
        h_textw.AddStyleAttribute("color", "Black");
        pnlPrincipal.RenderControl(h_textw);//Name of the Panel  

        // Create an instance of the document class which represents the PDF document itself.  
        Document document = new Document(PageSize.A4, 25, 25, 30, 30);
        // Create an instance to the PDF file by creating an instance of the PDF   
        // Writer class using the document and the filestrem in the constructor.  
        FontFactory.GetFont("Verdana", 80, iTextSharp.text.BaseColor.RED);

        PdfWriter writer = PdfWriter.GetInstance(document, fs);

        document.AddCreator("Sample application using iTextSharp");
     
        document.AddSubject("Document subject - Describing the steps creating a PDF document");
        document.AddTitle("The document title - PDF creation using iTextSharp");

        // Open the document to enable you to write to the document  
        document.Open();
        // Add a simple and wellknown phrase to the document in a flow layout manner  
     //   document.Add(new Paragraph("Hello World!"));
        StringReader s_tr = new StringReader(s_tw.ToString());
        HTMLWorker html_worker = new HTMLWorker(document);
        html_worker.Parse(s_tr);
        // Close the document  
        document.Close();
        // Close the writer instance  
        writer.Close();
        // Always close open filehandles explicity  
        fs.Close();  
  • Related