zainale.net Open in urlscan Pro
2a06:98c1:3121::7  Public Scan

URL: http://zainale.net/?qa=734548/
Submission: On March 10 via manual from GB — Scanned from GB

Form analysis 8 forms found in the DOM

POST ./?qa=734548

<form method="post" action="./?qa=734548"> Welcome to ZaiNaLe Developer Community-Open, Learning and Share <input name="notice_visitor" onclick="return qa_notice_click(this);" type="submit" value="X" class="qa-notice-close-button">
  <input name="code" type="hidden" value="0-1646941414-0c7d612f454ed676d65fa2da5b5d73a5665ef3a2">
</form>

POST ./?qa=login&to=%3Fqa%3D734548

<form action="./?qa=login&amp;to=%3Fqa%3D734548" method="post">
  <input type="text" name="emailhandle" dir="auto" placeholder="Email or Username">
  <input type="password" name="password" dir="auto" placeholder="Password">
  <div><input type="checkbox" name="remember" id="qam-rememberme" value="1">
    <label for="qam-rememberme">Remember</label>
  </div>
  <input type="hidden" name="code" value="0-1646941414-50bc4f348b475375c83a750aaf601090d7a0df5b">
  <input type="submit" value="Login" class="qa-form-tall-button qa-form-tall-button-login" name="dologin">
</form>

GET ./?qa=search

<form method="get" action="./?qa=search">
  <input type="hidden" name="qa" value="search">
  <input type="text" placeholder="Search..." name="q" value="" class="qa-search-field">
  <input type="submit" value="Search" class="qa-search-button">
</form>

POST ./?qa=734548

<form method="post" action="./?qa=734548">
  <div class="qa-q-view-stats">
    <div class="qa-voting qa-voting-net" id="voting_734548">
      <div class="qa-vote-buttons qa-vote-buttons-net">
        <input title="Click to vote up" name="vote_734548_1_q734548" onclick="return qa_vote_click(this);" type="submit" value="+" class="qa-vote-first-button qa-vote-up-button">
        <input title="Click to vote down" name="vote_734548_-1_q734548" onclick="return qa_vote_click(this);" type="submit" value="–" class="qa-vote-second-button qa-vote-down-button">
      </div>
      <div class="qa-vote-count qa-vote-count-net">
        <span class="qa-netvote-count">
          <span class="qa-netvote-count-data">0</span><span class="qa-netvote-count-pad"> votes
            <meta itemprop="upvoteCount" content="0">
          </span>
        </span>
      </div>
      <div class="qa-vote-clear">
      </div>
    </div>
    <div>
      <span class="qa-a-count qa-a-count-view">
        <span class="qa-a-count-data">61</span><span class="qa-a-count-pad"> views</span>
      </span>
    </div>
    <div>
    </div>
  </div>
  <input name="code" type="hidden" value="0-1646941414-4d74fd211c89613e9ffa824b59df60e3b05f6408">
</form>

POST ./?qa=734548

<form method="post" action="./?qa=734548">
  <div class="qa-q-view-avatar-meta">
    <span class="qa-q-view-avatar">
      <a href="./?qa=user/%E6%B7%B1%E8%93%9D" class="qa-avatar-link"><img src="./?qa=image&amp;qa_blobid=9497455313653366137&amp;qa_size=50" width="50" height="50" class="qa-avatar-image" alt=""></a>
    </span>
    <span class="qa-q-view-meta">
      <a href="./?qa=734548/" class="qa-q-view-what" itemprop="url">asked</a>
      <span class="qa-q-view-when">
        <span class="qa-q-view-when-data"><time itemprop="dateCreated" datetime="2021-10-17T02:58:08+0000" title="2021-10-17T02:58:08+0000">Oct 17, 2021</time></span>
      </span>
      <span class="qa-q-view-who">
        <span class="qa-q-view-who-pad">by </span>
        <span class="qa-q-view-who-data"><span itemprop="author" itemscope="" itemtype="https://schema.org/Person"><a href="./?qa=user/%E6%B7%B1%E8%93%9D" class="qa-user-link" itemprop="url"><span itemprop="name">深蓝</span></a></span></span>
        <span class="qa-q-view-who-points">
          <span class="qa-q-view-who-points-pad">(</span><span class="qa-q-view-who-points-data">31.9m</span><span class="qa-q-view-who-points-pad"> points)</span>
        </span>
      </span>
    </span>
  </div>
  <h3>
    <span itemprop="name"> pdf - Getting PdfStamper to work with MemoryStreams (c#, itextsharp) </span>
  </h3>
  <div class="qa-q-view-content qa-post-content">
    <a name="734548"></a>
    <div itemprop="text">
      <p>It came to me to rework old code which signs PDF files into new one, which signs MemoryStreams (byte arrays) that come and are sent by web services. Simple, right? Well, that was yesterday. Today I just can't get it to work.</p>
      <p>This is the old code, which uses FileStreams and it works:</p>
      <pre><code>    public static string OldPdfSigner(PdfReader pdfReader, string destination, string password, string reason, string location, string pathToPfx)
    {
        using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
        {
            ...

            using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, new FileStream(destination, FileMode.Create, FileAccess.Write), ''))
            {
                PdfSignatureAppearance sap = st.SignatureAppearance;
                sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
                sap.Reason = reason;
                sap.Location = location;
                return destination;
            }
        }
    }
</code></pre>
      <p>Below is what I've redone myself which throws System.ObjectDisposedException: Cannot access a closed Stream.</p>
      <pre><code>    public static byte[] PdfSigner(PdfReader pdfReader, string password, string reason, string location, string pathToPfx)
    {
        using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
        {
            ...

            MemoryStream outputStream = new MemoryStream();
            using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, outputStream, ''))
            {
                st.Writer.CloseStream = false;
                PdfSignatureAppearance sap = st.SignatureAppearance;
                sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
                sap.Reason = reason;
                sap.Location = location;
                st.Close();
                outputStream.Position = 0;
                return outputStream.ToArray();
            }
        }
    }
</code></pre>
      <p>and if I comment out</p>
      <pre><code>st.Close();
</code></pre>
      <p>it creates an empty document. What am I doing wrong?</p> See Question&amp;Answers more
      detail:<a rel="nofollow" href="https://stackoverflow.com/questions/27188593/getting-pdfstamper-to-work-with-memorystreams-c-itextsharp" target="_blank">os</a>
    </div><br>
    <div class="signature" style="font-size:14px;overflow:hidden;margin-bottom:10px;padding-top:20px;line-height:1.6em;background:url(https://www.dismall.com/static/image/common/sigline.gif) no-repeat 0 0;">与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…</div>
  </div>
  <div class="qa-q-view-tags">
    <ul class="qa-q-view-tag-list">
      <li class="qa-q-view-tag-item"><a href="./?qa=tag/%09pdf" rel="tag" class="qa-tag-link">	pdf</a></li>
    </ul>
  </div>
  <div class="qa-q-view-buttons">
    <input name="q_doanswer" id="q_doanswer" onclick="return qa_toggle_element('anew')" value="answer" title="Answer this question" type="submit" class="qa-form-light-button qa-form-light-button-answer">
    <input name="q_docomment" onclick="return qa_toggle_element('c734548')" value="comment" title="Add a comment on this question" type="submit" class="qa-form-light-button qa-form-light-button-comment">
  </div>
  <input name="code" type="hidden" value="0-1646941414-a8e1a17abe5966098c43bbf4b640eae82c95cf97">
  <input name="qa_click" type="hidden" value="">
</form>

POST ./?qa=734548

<form method="post" action="./?qa=734548">
  <div class="qa-voting qa-voting-net" id="voting_734549">
    <div class="qa-vote-buttons qa-vote-buttons-net">
      <input title="Click to vote up" name="vote_734549_1_a734549" onclick="return qa_vote_click(this);" type="submit" value="+" class="qa-vote-first-button qa-vote-up-button">
      <input title="Click to vote down" name="vote_734549_-1_a734549" onclick="return qa_vote_click(this);" type="submit" value="–" class="qa-vote-second-button qa-vote-down-button">
    </div>
    <div class="qa-vote-count qa-vote-count-net">
      <span class="qa-netvote-count">
        <span class="qa-netvote-count-data">0</span><span class="qa-netvote-count-pad"> votes
          <meta itemprop="upvoteCount" content="0">
        </span>
      </span>
    </div>
    <div class="qa-vote-clear">
    </div>
  </div>
  <input name="code" type="hidden" value="0-1646941414-4d74fd211c89613e9ffa824b59df60e3b05f6408">
</form>

POST ./?qa=734548

<form method="post" action="./?qa=734548">
  <div class="qa-a-item-avatar-meta">
    <span class="qa-a-item-avatar">
      <a href="./?qa=user/%E6%B7%B1%E8%93%9D" class="qa-avatar-link"><img src="./?qa=image&amp;qa_blobid=9497455313653366137&amp;qa_size=40" width="40" height="40" class="qa-avatar-image" alt=""></a>
    </span>
    <span class="qa-a-item-meta">
      <a href="./?qa=734548/&amp;show=734549#a734549" class="qa-a-item-what" itemprop="url">answered</a>
      <span class="qa-a-item-when">
        <span class="qa-a-item-when-data"><time itemprop="dateCreated" datetime="2021-10-17T02:58:08+0000" title="2021-10-17T02:58:08+0000">Oct 17, 2021</time></span>
      </span>
      <span class="qa-a-item-who">
        <span class="qa-a-item-who-pad">by </span>
        <span class="qa-a-item-who-data"><span itemprop="author" itemscope="" itemtype="https://schema.org/Person"><a href="./?qa=user/%E6%B7%B1%E8%93%9D" class="qa-user-link" itemprop="url"><span itemprop="name">深蓝</span></a></span></span>
        <span class="qa-a-item-who-points">
          <span class="qa-a-item-who-points-pad">(</span><span class="qa-a-item-who-points-data">31.9m</span><span class="qa-a-item-who-points-pad"> points)</span>
        </span>
      </span>
    </span>
  </div>
  <div class="qa-a-selection">
  </div>
  <div class="qa-a-item-content qa-post-content">
    <a name="734549"></a>
    <div itemprop="text">
      <p>Not specific to your signing code, but when working with <code>MemoryStream</code> and <code>PdfStamper</code>, follow this general pattern:</p>
      <pre><code>using (MemoryStream ms = new MemoryStream()) {
  using (PdfStamper stamper = new PdfStamper(reader, ms, '', true)) {
// do stuff      
  }    
  return ms.ToArray();
}
</code></pre>
      <ul>
        <li><code>MemoryStream</code> implements <code>IDisposable</code>, so include a <code>using</code> statement.</li>
        <li>The <code>PdfStamper</code> <code>using</code> statement takes care of disposing the object, so you don't need to call <code>Close()</code>, and don't need to set the <code>CloseStream</code> property.</li>
        <li>Your code snippet is returning the byte array <strong>too soon</strong>, inside the <code>PdfStamper</code> <code>using</code> statement, so your <code>MemoryStream</code> is effectively a no-op. Return the byte array
          <strong>outside</strong> of the <code>PdfStamper</code> <code>using</code> statement, and <strong>inside</strong> the <code>MemoryStream</code> <code>using</code> statement.</li>
        <li>Generally there's no need to reset the <code>MemoryStream</code> <code>Position</code> property.</li>
        <li>Ignore the <code>PdfStamper</code> constructor above - it's from some test code I had for filling forms, and use whatever constructor/method you need to do your signing.</li>
      </ul>
    </div><br>
    <div class="signature" style="font-size:14px;overflow:hidden;margin-bottom:10px;padding-top:20px;line-height:1.6em;background:url(https://www.dismall.com/static/image/common/sigline.gif) no-repeat 0 0;">与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…</div>
  </div>
  <div class="qa-a-item-buttons">
    <input name="a734549_dofollow" value="ask related question" title="Ask a new question relating to this answer" type="submit" class="qa-form-light-button qa-form-light-button-follow">
    <input name="a734549_docomment" onclick="return qa_toggle_element('c734549')" value="comment" title="Add a comment on this answer" type="submit" class="qa-form-light-button qa-form-light-button-comment">
  </div>
  <input name="code" type="hidden" value="0-1646941414-d788d9174a5403c5d47be85e6960083d0bd1cdf4">
  <input name="qa_click" type="hidden" value="">
</form>

GET ./?qa=search

<form method="get" action="./?qa=search">
  <input type="hidden" name="qa" value="search">
  <input type="text" placeholder="Search..." name="q" value="" class="qa-search-field">
  <input type="submit" value="Search" class="qa-search-button">
</form>

Text Content

Welcome to ZaiNaLe Developer Community-Open, Learning and Share
Login
Remember
 * Register



 * All Activity
 * Q&A
 * Hot!
 * Unanswered
 * Tags
 * Categories
 * Users
 * Ask a Question
 * Tools


Ask a Question




CATEGORIES

All categories
Topic[话题] (3)
Life[生活] (4)
Technique[技术] (86.6k)
Idea[创意] (2)
Jobs[工作] (0)
Others[杂七杂八] (10)


PDF - GETTING PDFSTAMPER TO WORK WITH MEMORYSTREAMS (C#, ITEXTSHARP)

0 votes

61 views

asked Oct 17, 2021 by 深蓝 (31.9m points)


PDF - GETTING PDFSTAMPER TO WORK WITH MEMORYSTREAMS (C#, ITEXTSHARP)

It came to me to rework old code which signs PDF files into new one, which signs
MemoryStreams (byte arrays) that come and are sent by web services. Simple,
right? Well, that was yesterday. Today I just can't get it to work.

This is the old code, which uses FileStreams and it works:

    public static string OldPdfSigner(PdfReader pdfReader, string destination, string password, string reason, string location, string pathToPfx)
    {
        using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
        {
            ...

            using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, new FileStream(destination, FileMode.Create, FileAccess.Write), ''))
            {
                PdfSignatureAppearance sap = st.SignatureAppearance;
                sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
                sap.Reason = reason;
                sap.Location = location;
                return destination;
            }
        }
    }


Below is what I've redone myself which throws System.ObjectDisposedException:
Cannot access a closed Stream.

    public static byte[] PdfSigner(PdfReader pdfReader, string password, string reason, string location, string pathToPfx)
    {
        using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
        {
            ...

            MemoryStream outputStream = new MemoryStream();
            using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, outputStream, ''))
            {
                st.Writer.CloseStream = false;
                PdfSignatureAppearance sap = st.SignatureAppearance;
                sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
                sap.Reason = reason;
                sap.Location = location;
                st.Close();
                outputStream.Position = 0;
                return outputStream.ToArray();
            }
        }
    }


and if I comment out

st.Close();


it creates an empty document. What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
 * pdf





PLEASE LOG IN OR REGISTER TO ADD A COMMENT.





PLEASE LOG IN OR REGISTER TO ANSWER THIS QUESTION.


1 ANSWER

0 votes

answered Oct 17, 2021 by 深蓝 (31.9m points)


Not specific to your signing code, but when working with MemoryStream and
PdfStamper, follow this general pattern:

using (MemoryStream ms = new MemoryStream()) {
  using (PdfStamper stamper = new PdfStamper(reader, ms, '', true)) {
// do stuff      
  }    
  return ms.ToArray();
}


 * MemoryStream implements IDisposable, so include a using statement.
 * The PdfStamper using statement takes care of disposing the object, so you
   don't need to call Close(), and don't need to set the CloseStream property.
 * Your code snippet is returning the byte array too soon, inside the PdfStamper
   using statement, so your MemoryStream is effectively a no-op. Return the byte
   array outside of the PdfStamper using statement, and inside the MemoryStream
   using statement.
 * Generally there's no need to reset the MemoryStream Position property.
 * Ignore the PdfStamper constructor above - it's from some test code I had for
   filling forms, and use whatever constructor/method you need to do your
   signing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…




PLEASE LOG IN OR REGISTER TO ADD A COMMENT.



Welcome to ZaiNaLe Developer Community-Open, Learning and Share


JUST BROWSING BROWSING

[1] .net core - Any better way to encrypt ID before binding it to datatable and
open for editing/delete that they cannot see ID in whole view source?
[2] linux - Knowing a file has been archived on ext4 file system
[3] unsupported locale setting Mac python
[4] docker-compose up 镜像失败,报错网络错误
[5] reactjs - Selected option deselecting after onIonChange event
[6] 利用window.location.href下载文件后,要如何返回原页面?
[7] python - How to emulate Postgres Django distinct() with SQLite backend
[8] mysql的left join索引只使用join的表的索引?
[9] 如何通过按钮获取当前值?
[10] echarts重新setOption问题

0 questions

0 answers

0 comments

56.3k users


MOST POPULAR TAGS

javascript python java c# How android c++ php ios html sql r c node.js .net
iphone asp.net css reactjs jquery ruby What Android objective mysql linux Is git
Python windows Why regex angular swift amazon excel google algorithm macos how
Java visual bash Can typescript multithreading PHP Using scala angularjs apache
spring performance postgresql database flutter json rust arrays C# vba dart
django wpf xml vue.js In go Get jQuery xcode jsf http Google mongodb string
shell oop powershell SQL C++ security assembly docker Javascript Android: Does
haskell web Convert azure debugging delphi vb.net Spring datetime pandas oracle
math

站长统计
今日IP[1943]
今日PV[2065]
昨日IP[13241]
昨日PV[14472]
当前在线[115]
 * 深圳家
 * 深圳家
 * 极客中国
 * CC BY-SA 3.0


Powered by Question2Answer
Donate
Theme by Q2A Market&&OStack.cn

...