title: Numbers not showing in generated PDF on some pages after text replace PDFSharp
body: <p>I'm working on this extremely annoying bug where the number 8 is not showing up in my PDF. 1,2,3,4,5,6,7 and 0 are fine, only 8 is showing a square on SOME of the pages (the portrait ones).</p>
  
  <p>The application works as follows:</p>
  
  <ul>
  <li>Generate PDF on SSRS ( some landscape, some portrait )</li>
  <li>Merge PDF using PDFSharp</li>
  <li>Update page numbers using PDFSharp</li>
  </ul>
  
  <p>Now, on the first part of the overall report (portrait pages), the 8 is not showing. In the second part of the report, the 8 IS showing.</p>
  
  <p>I don't see any differences in the RDL (language, fonts, even size of the box). The whole document is in ANSI encoding, and so the code to write the new page numbers is the same for the whole document. </p>
  
  <p><img src="https://i.stack.imgur.com/CBq5L.png" alt="8 not showing on first couple of pages"></p>
  
  <p><img src="https://i.stack.imgur.com/vZ9MH.png" alt="8 is showing on some of the other pages"></p>
  
  <p>I saw that Aspose had a problem like this (<a href="http://www.aspose.com/community/forums/thread/528718/number-8-missing-in-pdf-file-with-some-viewers.aspx" rel="nofollow noreferrer">http://www.aspose.com/community/forums/thread/528718/number-8-missing-in-pdf-file-with-some-viewers.aspx</a>), but I'm not using Aspose. </p>
  
  <p>I checked appending (char) 0x38, and it doesn't show up. 0x37 and 0x39 are. String encoding is done for both cases like Encoding.GetEncoding(1252).GetBytes() or Encoding.Default.</p>
  
  <p>The code for generating the PDFS through SSRS is identical, except for report name of course. I could not find any encoding information in the RDL itself.</p>
  
  <p>Page numbers are replaced, using PDFSharp Stream.Value = 'newvalue'.</p>
  
  <p>All ideas are VERY much appreciated.</p>
  
  <p><strong>UPDATE:</strong> I ran the number replacemenent through Aspose and the 8 showed up, as expected, on all pages. Using simple pdf.Pages.Accept(textFragmentAbsorber);.</p>
  
  <p><strong>Update II</strong></p>
  
  <p>So after some playing around, I'm pretty sure it has to do with the way I'm replacing the text in the document, and the encoding of the replaced string.</p>
  
  <p>Extraction is as follows:</p>
  
  <pre><code>    public byte[] UpdatePageNumbers(byte[] file, PageNumberingConfigurationBase config)
      {
          var doc = PdfReader.Open(new MemoryStream(file), PdfDocumentOpenMode.Modify);
          for (int i = 0; i &lt; doc.PageCount; i++)
          {
              var pageNr = i + 1;
              var page = doc.Pages[i];
  
              for (int j = 0; j &lt; page.Contents.Elements.Count; j++)
              {
                  var element = page.Contents.Elements.GetDictionary(j);
                  var content = element.AsString();
  
                  if (content.Contains(config.SearchTemplate))
                  {
                      var newContent = content.Replace(
                          config.SearchTemplate,
                          config.GetReplacementTextForPage(pageNr, doc.PageCount));
  
                      element.Stream.Value = newContent.AsByteArray();
                  }
              }
          }
  
          return doc.AsByteArray();
      }
  </code></pre>
  
  <p>With helper class:</p>
  
  <pre><code>public static class ElementExtensions
  {
      public static string AsString(this PdfDictionary dict)
      {
          return GetString(dict.Stream.Value);
      }
  
      public static byte[] AsByteArray(this string stream)
      {
          return GetBytes(stream);
      }
  
      static byte[] GetBytes(string str)
      {
          return Encoding.GetEncoding(1252).GetBytes(str);
      }
  
      static string GetString(byte[] bytes)
      {
          return Encoding.GetEncoding(1252).GetString(bytes);
      }
  }
  </code></pre>
  
  <p>The document encoding inside the PDF is:</p>
  
  <pre><code>/Encoding /WinAnsiEncoding
  </code></pre>
  
  <p>Here's how the documents are merged:</p>
  
  <pre><code>    public byte[] MergePdf(params byte[][] pdfs)
      {
          var outputDocument = new PdfDocument();
  
          for (int i = 0; i &lt; pdfs.Count(); i++)
          {
              var document = PdfReader.Open(new MemoryStream(pdfs[i]), PdfDocumentOpenMode.Import);
  
              // Create the output document
              foreach (PdfPage pdfPage in document.Pages)
              {
                  outputDocument.Pages.Add(pdfPage);
              }
          }
  
          return outputDocument.AsByteArray();
      }
  </code></pre>
  
  <p><strong>Sample files</strong></p>
  
  <p>So here are the sample files:</p>
  
  <p>This is one report, generated 3 times, then merged, then page numbers updated.
  <a href="https://www.dropbox.com/s/yxzqw0y2tvu3v9a/before_update.pdf" rel="nofollow noreferrer">https://www.dropbox.com/s/yxzqw0y2tvu3v9a/before_update.pdf</a>
  <a href="https://www.dropbox.com/s/ui26l0qsunhcune/after_update.pdf" rel="nofollow noreferrer">https://www.dropbox.com/s/ui26l0qsunhcune/after_update.pdf</a></p>
  
  <p>Please note that now ALL the numbers are shown as boxes/squares..</p>