直接通过代码打开生成的pdf文件,无需将其保存到磁盘上

发布于 2024-12-09 21:22:16 字数 1011 浏览 0 评论 0原文

我使用 Sharepoint 2010,正在开发一个 Web 部件,在按钮单击事件中,需要生成并直接打开 pdf 文件。不应保存到磁盘上。 我尝试了下面的代码

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

我也尝试了下面的代码,它也在服务器上生成了我不想要的文件。

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

这个在桌面上创建了pdf文件,我需要以pdf格式打开它。有人可以帮助我吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等往事风中吹 2024-12-16 21:22:16

几乎每次接受FileStream的东西实际上确实接受了一个通用的System.IO.Stream对象,该对象< code>FileStream 是 的子类。这意味着您可以使用它的表兄弟 System.IO.MemoryStream 这就是您正在寻找的:

        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

如果您不想,则不必创建字节数组,我只是在展示如何创建 PDF 并为您提供“类似 .net”的内容供您使用。

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.

南城旧梦 2024-12-16 21:22:16

我终于能够让它工作了。

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

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();
      } 
葬﹪忆之殇 2024-12-16 21:22:16

这对我有用。

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

  }

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

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文