【问题标题】:Save pdf file Memorystream in folder C#将pdf文件Memorystream保存在文件夹C#中
【发布时间】:2022-06-29 00:40:57
【问题描述】:

我试图创建一个函数,在其中使用 memorystream 创建文件 pdf,但我不知道如何将此文件 memorystream 保存在文件夹中。我怎样才能做到这一点?

这是我的代码:

        string beneficiarioRelatorio = service.RelatorioBeneficiarioService(numeroProtocolo);

        string matriculaRelatorio = service.RelatorioMatriculaService(numeroProtocolo);

        Document document = new Document(PageSize.A4, 30f, 20f, 50f, 40f);

        MemoryStream stream = new MemoryStream();

         try
        {
            PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);

            pdfWriter.CloseStream = false;

            Paragraph titulo = new Paragraph(\"Comprovante de solicitação de serviço eletrônico\", fontTextBold);
            titulo.Alignment = Element.ALIGN_CENTER;
            titulo.SpacingBefore = 50;
            titulo.IndentationLeft = 65;

            document.Add(titulo);
        }
        catch (DocumentException de)
        {
            Console.Error.WriteLine(de.Message);
        }
        catch (IOException ioe)
        {
            Console.Error.WriteLine(ioe.Message);
        }

        document.Close();

        stream.Flush();
        stream.Position = 0;

        //return File(stream, \"application/pdf\", \"Relatorio_Solicitacao_Servico_Eletronico.pdf\");
        save file stream on folder?...
    
  • 如果要将 PDF 保存到磁盘,只需使用 FileStream 而不是 MemoryStream。
  • 您想将其直接保存到本地文件系统,还是想按照您的代码建议将其作为文件返回到您的端点?
  • 只需将此文件保存在服务器文件夹中。

标签: c# itext memorystream


【解决方案1】:

File.WriteAllBytes("Filename.pdf", stream.ToArray());

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2016-12-15
    • 2021-08-28
    • 1970-01-01
    相关资源
    最近更新 更多