Free Support Forum - aspose.com

Passing a Word Document to create a PDF and return a MemoryStream

I am using trial version of ASPOSE.Word .NET (11.6.0.0) to generate a PDF from a word doc.

But I am getting the following error when trying to load stream into xmlDocument.

Inner exception: Root element is missing.

at System.Xml.XmlTextReaderImpl.Throw(Exception e)

at System.Xml.XmlTextReaderImpl.ParseDocumentContent()

at System.Xml.XmlTextReaderImpl.Read()

at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)

at System.Xml.XmlDocument.Load(XmlReader reader)

at System.Xml.XmlDocument.Load(Stream inStream)

at Common.Components.Document.ConvertDocToPDF(MemoryStream wordDocument)

Below is my code

public static MemoryStream ConvertDocToPDF(MemoryStream wordDocument)
{
Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = Aspose.Words.LoadFormat.Doc;
Aspose.Words.Document doc = new Aspose.Words.Document(wordDocument, loadOptions);
wordDocument.Close();

//Save the document in Aspose.Pdf.Xml format into a memory stream.
MemoryStream xmlStream = new MemoryStream();
doc.Save(xmlStream, Aspose.Words.SaveFormat.Pdf);
//Seek to the beginning so it can be read by XmlDocument.

xmlStream.Seek(0, SeekOrigin.Begin);

//Load the document into an XmlDocument
XmlReader.Create(xmlStream);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlStream);
xmlStream.Close();

// Load the XML document into Aspose.Pdf
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
pdf.BindXML(xmlDoc, null);

// Instruct to delete temporary image files.
pdf.IsImagesInXmlDeleteNeeded = true;

//THIS MAKES PERFORMANCE **SIGNIFICANTLY** BETTER
pdf.IsTruetypeFontMapCached = true;

//THIS WOULD SAVE SERVER MEMORY - WOULD IT WORK FOR US, OR CAUSE FUNCTIONALITY ISSUES?
//http://www.aspose.com/wiki/default.aspx/Aspose.Pdf/WritingPdfFileDirectly.html

// Produce the PDF file.
MemoryStream stream = new MemoryStream();
pdf.Save(stream);
return stream;
}public static MemoryStream ConvertDocToPDF(MemoryStream wordDocument)
{
Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = Aspose.Words.LoadFormat.Doc;
Aspose.Words.Document doc = new Aspose.Words.Document(wordDocument, loadOptions);
wordDocument.Close();

//Save the document in Aspose.Pdf.Xml format into a memory stream.
MemoryStream xmlStream = new MemoryStream();
doc.Save(xmlStream, Aspose.Words.SaveFormat.Pdf);
//Seek to the beginning so it can be read by XmlDocument.

xmlStream.Seek(0, SeekOrigin.Begin);

//Load the document into an XmlDocument
XmlReader.Create(xmlStream);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlStream);
xmlStream.Close();

// Load the XML document into Aspose.Pdf
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
pdf.BindXML(xmlDoc, null);

// Instruct to delete temporary image files.
pdf.IsImagesInXmlDeleteNeeded = true;

//THIS MAKES PERFORMANCE **SIGNIFICANTLY** BETTER
pdf.IsTruetypeFontMapCached = true;

//THIS WOULD SAVE SERVER MEMORY - WOULD IT WORK FOR US, OR CAUSE FUNCTIONALITY ISSUES?
//http://www.aspose.com/wiki/default.aspx/Aspose.Pdf/WritingPdfFileDirectly.html

// Produce the PDF file.
MemoryStream stream = new MemoryStream();
pdf.Save(stream);
return stream;
}

Hi,

Thanks for your inquiry. I would suggest you please upgrade to the latest version (v13.1.0) from here and let us know how it goes on your side. If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.