Open Generated pdf file through code directly with

2020-06-06 02:20发布

问题:

I use Sharepoint 2010 and I am developing a web part where on a button click event, a pdf file needs to be generated and opened directly. Should not be saving onto the disk. I tried the below code

 protected void Button1_OnClick(object sender, EventArgs e)
    {
        Document myDoc = new Document(PageSize.A4.Rotate());
        try
        {
            PdfWriter.GetInstance(myDoc, new FileStream(@"C:\Directory\Test.pdf", FileMode.Create));
            myDoc.Open();
            myDoc.Add(new Paragraph("Hello World"));
        }
        catch (DocumentException ex)
        {
            Console.Error.WriteLine(ex.Message);
        }
        myDoc.Close();
    }

I also tried the below code which also generates the file on the Server which I dont want.

  Document document = new Document(PageSize.A4);
        PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("~/Test.pdf"), FileMode.Create));
        document.Open();
        var WelcomePara = new Paragraph("Hello World");
        document.Add(WelcomePara);
        document.Close();

This one creates the pdf file on the desktop, I need it to be opened in the pdf format.Can someone help me please.

回答1:

Almost every time that something accepts a FileStream is actually really accepts a generic System.IO.Stream object which FileStream is a subclass of. This means that you can instead use its cousin System.IO.MemoryStream which is what you are looking for:

        byte[] bytes;
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
            using (iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate())) {
                using (iTextSharp.text.pdf.PdfWriter w = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, ms)) {
                    doc.Open();
                    doc.NewPage();
                    doc.Add(new iTextSharp.text.Paragraph("Hello world"));
                    doc.Close();
                    bytes = ms.ToArray();
                }
            }
        }
        //Do whatever you want with the byte array here

You don't have to create the byte array if you don't want, I was just showing how to create a PDF and give you something ".net-like" for you to work with.



回答2:

I was able to get it work finally.

  using (var ms = new MemoryStream())
      {
          using (var document = new Document(PageSize.A4,50,50,15,15))
          {
              PdfWriter.GetInstance(document, ms); 
              document.Open();
              document.Add(new Paragraph("HelloWorld"));
              document.Close();
          }
          Response.Clear();
          //Response.ContentType = "application/pdf";
          Response.ContentType = "application/octet-stream";
          Response.AddHeader("content-disposition", "attachment;filename= Test.pdf");
          Response.Buffer = true; 
          Response.Clear();
          var bytes = ms.ToArray();
          Response.OutputStream.Write(bytes, 0, bytes.Length);
          Response.OutputStream.Flush();
      } 


回答3:

This Works for me.

using (var ms = new MemoryStream())
  {

      using (var document = new Document(PageSize.A4,50,50,15,15))
      {

        // step 2
        PdfWriter writer = PdfWriter.GetInstance(document, ms);


        // step 3
        document.Open();

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.Parse(new StringReader(--Your HTML--));


        // step 5
        document.Close();
      }  
      Byte[] FileBuffer = ms.ToArray();
      if (FileBuffer != null)
      {
          Response.ContentType = "application/pdf";
          Response.AddHeader("content-length", FileBuffer.Length.ToString());
          Response.BinaryWrite(FileBuffer);
      }

  }