BizTalk自定义管道解析POP3 PDF附件错误

时间:2017-07-18 15:00:56

标签: itext biztalk biztalk-2013

我有一个BizTalk自定义管道组件,我正在使用 itexsharp 解析PDF附件到自定义模型中。管道绑定到POP3接收端口。

如果我返回附件流(outputMessage.GetPart("Body").Data = ms),则在新创建的消息中,这在BizTalk管理控制台中看起来很好。我已经能够从这里手动保存消息,并使用与管道中相同的解析方法正确解析。

在管道中直接解析PDF时,我收到以下错误:重建失败:未找到预告片。原始消息:在文件指针1620729

中找不到外部参照子部分

如果我从管道中删除默认的XMLDisassembler组件,则解析错误消失,但在控制台中消息Body为空,尽管AttachmentSizeInBytes = 1788

public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
    return ExtractMessagePartToMessage(pContext, pInMsg);
}

private IBaseMessage ExtractMessagePartToMessage(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            if (pInMsg.PartCount <= 1)
            {
                throw new InvalidOperationException("The email had no attachment, apparently.");
            }

            string partName; 
            IBaseMessagePart attachmentPart = pInMsg.GetPartByIndex(1, out partName);
            Stream attachmentPartStream = attachmentPart.GetOriginalDataStream();

            IBaseMessage outputMessage;
            outputMessage = pContext.GetMessageFactory().CreateMessage();
            outputMessage.AddPart("Body", pContext.GetMessageFactory().CreateMessagePart(), true);
            outputMessage.Context = pInMsg.Context;

            var ms = new MemoryStream();
            attachmentPartStream.CopyTo(ms);
            ms.Seek(0L, SeekOrigin.Begin);

            Stream orderStream = PdfFormParser.Parse(ms);

            outputMessage.GetPart("Body").Data = orderStream;
            outputMessage.Context.Write("AttachmentName", "http://schemas.microsoft.com/BizTalk/2003/file-properties", partName);
            outputMessage.Context.Write("AttachmentSizeInBytes", "http://schemas.microsoft.com/BizTalk/2003/file-properties", orderStream.Length.ToString());


            pContext.ResourceTracker.AddResource(ms);
            pContext.ResourceTracker.AddResource(orderStream);

            return outputMessage;
        }

  public static Stream Parse(Stream pdfDocument)
        {
            using (var reader = new PdfReader(pdfDocument))
            {
                var outputStream = new MemoryStream();
                var pdfForm = ParseInternal(reader);
                var xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(pdfForm.Serialize());

                xmlDocument.Save(outputStream);

                return outputStream;
            }

1 个答案:

答案 0 :(得分:1)

在读取或写入Stream时,在管道中,如果有其他东西要使用它,则必须将流回退到开头(特别是您期望BizTalk处理的最终消息)。