Parsing a custom BizTalk pipeline for POP3 PDF Attachment Error

I have a BizTalk pipeline component where I parse a PDF attachment using itexsharp into a custom model. The pipeline is connected to the POP3 receive port.

In the newly created post, if I return the attachment stream ( outputMessage.GetPart("Body").Data = ms

) it looks good in the BizTalk admin console. I was able to save the post here manually and it was parsed correctly using the same parsing method as in the pipeline.

When parsing PDF directly in the pipeline, I get the following error: Rebuild failed: trailer not found; Original message: subkey xref not found in file pointer 1620729

If I remove the default XMLDisassembler component from the pipeline then the parsing error is gone but in the console the message body is empty even though 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;
            }

      

+3


source to share


1 answer


In pipelines, when you read or write a Stream, you need to rewind the stream back to the beginning if something else will use it (especially the last message you expect from BizTalk).



+1


source







All Articles