Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iTextSharp-组合内存流时损坏的PDF_C#_Itext - Fatal编程技术网

C# iTextSharp-组合内存流时损坏的PDF

C# iTextSharp-组合内存流时损坏的PDF,c#,itext,C#,Itext,我正在生成多个内存流,这些内存流需要使用iTextSharp组合成PDF,然后作为附件发送。但是,打开时电子邮件中附带的PDF显示文件已损坏的错误 以下是我如何组合内存流: using (var ms = new MemoryStream()) { using (var doc = new Document()) { using (PdfCopy copy = new PdfSmartCopy(doc

我正在生成多个内存流,这些内存流需要使用iTextSharp组合成PDF,然后作为附件发送。但是,打开时电子邮件中附带的PDF显示文件已损坏的错误

以下是我如何组合内存流:

using (var ms = new MemoryStream())
        {
            using (var doc = new Document())
            {
                using (PdfCopy copy = new PdfSmartCopy(doc, ms))
                {
                    doc.Open();
                    foreach (var seminar in seminars)
                    {
                        var reportBytes = await ssrsClient.GetReportAsync(_reportPath,
                            _detailedKostenkontrolleReportName, new {SeminarId = seminar.Id}).ConfigureAwait(false);

                        //Loop through each byte arrayforeach (var p in pdfByteContent) {​​​​

                        var reader = new PdfReader(reportBytes);

                        //Add the entire document instead of page-by-page
                        copy.AddDocument(reader);
                    }

                    var attachmentAllSeminarsDetailed = new Attachment(ms, DetailedAttachmentFileName,
                        "application/pdf");

                    attachments.Add(attachmentAllSeminarsDetailed);

                    //Create email body
                    sb = CreateEmailBody(
                        "Hallo,<br/><br/>in der Beilage sind die Berichte für die Hotelkostenkontrolle, die heute an die einzelne Hotels verschickt wurden.", "Dein SPOT - Team");

                    //Send e-mail with all PDF-s
                    MessageService.SendMessage(GetTestMailRecipients(), "Kostenkontrolle", sb.ToString(), attachments, new MailAddress(DefaultMailSender));
                }
            }
        }
使用(var ms=new MemoryStream())
{
使用(var doc=new Document())
{
使用(PdfCopy副本=新PdfSmartCopy(文档,ms))
{
doc.Open();
foreach(研讨会中的var研讨会)
{
var reportBytes=await ssrsClient.GetReportAsync(\u reportPath,
_detailedKostenkontrollereReportName,新的{seminard=seminard.Id});
//循环遍历每个字节数组foreach(pdfByteContent中的变量p){​​​​
var reader=新的PdfReader(reportBytes);
//添加整个文档,而不是逐页添加
副本。添加文件(读卡器);
}
var AttachmentalSeminarsDetailed=新附件(ms,DetailedAttachmentFileName,
"申请表格(pdf格式);;
附件。添加(附件详细说明);
//创建电子邮件正文
sb=主体(
“您好,
; //发送包含所有PDF-s的电子邮件 MessageService.SendMessage(GetTestMailRecipients(),“Kostenkontrolle”,sb.ToString(),附件,新邮件地址(DefaultMailSender)); } } }
PDF的大小为139字节,但在代码中它的大小是正确的,约180 kb。我感觉有些文档或阅读器没有正确关闭,但我无法确定是哪一个


有什么建议吗?

您是否尝试在foreach循环之后倒带流?(
ms.Position=0;
)@Fildor是的,但我在打开PDF时仍然收到有关损坏文件的错误。不确定是否需要关闭文档,首先…确实。必须关闭文档才能完成PDF。如果要进一步使用ms,请将stream closing设置为false