I’m building an application where I create a PDF document model from an HTML string and then save the PDF document as a byte array. Then at a later point I load the byte array back into a document model, make changes to it and then save it again. The HTML string contains hidden text (added through <span style="display:none">....</span>
). When I convert this to a document model initially the text is still present in the Content property and stays hidden through the CharacterFormat.Hidden
property in the Run
being set to true. However, when we save the document as a byte array and load it back later, all of the hidden text is gone. Is there any way to preserve hidden text when we’re saving this data?
Here’s a simplified version of the relevant code
Loading from HTML string:
var htmlLoadOptions = new HtmlLoadOptions();
using var htmlStream = new MemoryStream(htmlLoadOptions.Encoding.GetBytes(html));
var document = DocumentModel.Load(htmlStream, htmlLoadOptions);
using var pdfStream = new MemoryStream();
document.Save(pdfStream, new PdfSaveOptions
{
BookmarksCreateOptions = PdfBookmarksCreateOptions.UsingBookmarks,
});
return pdfStream.ToArray();
Loading from saved byte array
using var htmlStream = new MemoryStream(pdfBytes);
var document = DocumentModel.Load(htmlStream);
using var pdfStream = new MemoryStream();
document.Save(pdfStream, new PdfSaveOptions
{
BookmarksCreateOptions = PdfBookmarksCreateOptions.UsingBookmarks,
});
return pdfStream.ToArray();