This is because PdfStamper removes the stream, although it should not. He did not create it and does not own it, so he does not have a business managing it.
Your code really created the thread, and it really owns it, so itβs quite natural that it should dispose of it. If PdfStamper has not disposed of the stream inappropriately, everything will be okay with your enclosed needs.
Your first step should probably be to write a bug report / function request that removes the pdfstamper stream, or at least can be avoided. Once you do this, you can safely prevent a violation of CA2202, since a dual MemoryStream layout will not be detrimental.
BTW, PdfStamper.Dispose () call PdfStamper.Close () (at least in version 5.4.0) so that you can remove the call to PdfStamper.Close ().
source share