PDF Error: here was an error opening this document. This file is damaged and could not be repaired

Hello. I am having problems trying to implement the PDF components for .NET. I am getting the following error when I try to open the document that is created with the code below: PDF Error: here was an error opening this document. This file is damaged and could not be repaired

It is always 167 KB, regardless of what I place in it. I have tried writing out both simple strings and the contents of an ASP.NET Listview control and they both produce the same results. Please let me know if there are other steps that I need to take.

Thank you,
Bob

Pdf pdf = new Pdf();

using (MemoryStream memoryStream = new MemoryStream(100))
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

//reportListView.RenderControl(htmlTextWriter);
htmlTextWriter.Write("hello, this is cool.");

UnicodeEncoding uniEncoding = new UnicodeEncoding();
byte[] reportArray = uniEncoding.GetBytes(stringWriter.ToString());
// Write the first string to the stream.
memoryStream.Write(reportArray, 0, reportArray.Length);
pdf.BindHTML(memoryStream);
pdf.Save("testdoc.pdf", SaveType.OpenInAcrobat, HttpContext.Current.Response);
}

Hi,

Thank you for considering Aspose.

Please try adding Response.End() at the end of your code.

Thank you for the suggestion. Adding the Response.End got me a little further, but it still does not seem to work. It creates a 1kb file that I can open, but it is always empty. It just states that this file was made with an unlicensed version. When I debug my code, it states that my memoryStream is about 23k, but it never gets added to the document.

Pdf pdf = new Pdf();

using (MemoryStream memoryStream = new MemoryStream(100))
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
reportListView.RenderControl(htmlTextWriter);

UnicodeEncoding uniEncoding = new UnicodeEncoding();
byte[] reportArray = uniEncoding.GetBytes(stringWriter.ToString());
memoryStream.Write(reportArray, 0, reportArray.Length);
pdf.BindHTML(memoryStream);
pdf.Save("testdoc.pdf", SaveType.OpenInAcrobat, HttpContext.Current.Response);
Response.End();
}


Hi,

Can you please save the memoryStream into disk file and let us test it?

Due to the content, I am not allowed to send you the output. Can you please send me a working example? I do not have the option so saving a file to disk as documented in your other examples. Even if it used the most simple HTML, I can implement it. I have tried to make it as simple as possible, but it still does not seem to work.

Pdf pdf1 = new Pdf();
string html = "

hello there
";

UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
byte[] firstString = unicodeEncoding.GetBytes(html);

MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(firstString, 0, firstString.Length);

pdf1.BindHTML(memoryStream);
pdf1.Save("testdoc.pdf", SaveType.OpenInBrowser, HttpContext.Current.Response);
Response.End();

If I try write regular text with this example, it works, but the html tags are displayed:

Pdf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();

Text text1 = new Text(sec1, "

hello there
");
sec1.Paragraphs.Add(text1);

pdf1.Save("testdoc.pdf", SaveType.OpenInBrowser, HttpContext.Current.Response);
Response.End();

Hi,

We are looking into the details of Memorystream issue. Where as concerning to your second code snippet, if you need to add the HTML contents instead of adding the HTML Tags to the Pdf file, please add the following code line before adding the text object to paragraphs collection of Pdf.

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

text1.IsHtmlTagSupported = true;

Hi,

Please modify your first code snippet as specified below, in order to generate Pdf properly.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

FYI: BindHTML method only takes input with proper HTML formatting. Where initial tab must be and must contain tag

[C#]

Pdf pdf1 = new Pdf();

string html = "

hello there
";

byte[] firstString = System.Text.Encoding.UTF8.GetBytes(html);

MemoryStream memoryStream = new MemoryStream(firstString);

pdf1.BindHTML(memoryStream);

pdf1.Save("HTML_testdoc.pdf", SaveType.OpenInBrowser, HttpContext.Current.Response);

memoryStream.Close();