We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Words object into Aspose.Pdf object

We have upgraded from a very old version of Aspose to the latest. Our app was designed around a very specific set of (now obsolete) assumptions to turn a Words object into a Pdf object (example below). This no longer works. It gives me a “XmlException: Data at the root level is invalid. Line 1, position 1.” error.

Research seems to indicate this is an obsolete method to save a word file to PDF format. However, that’s not how it’s being used in this context. It’s transforming the Words object and then, later on (not part of this example), using the resulting PDF object to add things like watermarks.

I’ve been trying to find a way to transfer the contents of the Aspose.Words.Document object into an Aspose.Pdf.Generator.Pdf object, WITHOUT it going into direct mode, but have run out of ideas.

Is this still possible?

NOTE: “doc” is an Aspose.Words.Document object passed into the method.

var p = new Aspose.Pdf.Generator.Pdf();
using (MemoryStream ms = new MemoryStream())
{
doc.Save(ms, Aspose.Words.SaveFormat.Pdf);
p.BindXML(ms, null);
}
p.IsImagesInXmlDeleteNeeded = true;
p.SetUnicode();
p.Save(outputFileName);
return p;

Hi Scott,


Thanks for your feedback. You are correct now Aspose.Pdf does not accept the Aspose.Words object. However, now Aspose.Words supports DOC/DOCX to PDF conversion. You may save PDF document in memory stream and later can pass it to Aspose.Pdf.Document object for further manipulation using Aspose.Pdf. Hopefully it will help you to accomplish the task.

We are sorry for the inconvenience caused.

Best Regards,

Hi Scott,


Adding more to Tilal’s comments, the scenario which you have shared in your first post is a legacy approach of convert Word files to PDF format. In this approach, the contents of word/doc files where first transformed to XML format (according to Document Object Model of Aspose.Pdf) using Aspose.Words and then the XML file was converted to PDF format using Aspose.Pdf. However, this approach is obsolete now and you can directly convert Word files to PDF format using Aspose.Words.

Furthermore, please note that Aspose.Pdf.Generator namespace provides the feature to create PDF documents from scratch and I am afraid it does not support the capability to manipulate existing PDF files. So if you need to make some modifications/editing in PDF files created with Aspose.Words, you may consider using Aspose.Pdf.Document Object. For further details, please visit Working with Aspose.Pdf

Thank you for your note. The incompatibility I’m running into revolves around watermarks. As currently engineered, a watermark is added to the PDF object after it has been created. What I must have is the ability to specify a watermark (currently implemented as a Aspose.Pdf.Generator.FloatingBox) in the object. I’ve tried many different ways to do this but none seem to work so far. Essentially, the watermark is a bordered box with a background color set and a single line of text in it.

The STAMPS namespace seems to be what pdf.documents expects, but I don’t see how to specify things like borders.

What would you suggest I explore moving forward?

Update: The document starts life as an aspose.words document, and I do have the ability to alter it just before it gets turned into a PDF. Is it easier to add this using the .words object?

Hi Scott,

dscottj:
Thank you for your note. The incompatibility I'm running into revolves around watermarks. As currently engineered, a watermark is added to the PDF object *after* it has been created. What I must have is the ability to specify a watermark (currently implemented as a Aspose.Pdf.Generator.FloatingBox) in the object. I've tried many different ways to do this but none seem to work so far. Essentially, the watermark is a bordered box with a background color set and a single line of text in it.

The STAMPS namespace seems to be what pdf.documents expects, but I don't see how to specify things like borders.

What would you suggest I explore moving forward?

Thanks for sharing the details. You may try to use Aspose.Pdf.FloatingBox, hopefully it will help you to accomplish your requirements. If issue persist then please share your sample code so we will look into it and suggest you accordingly.

Best Regards,
dscottj:
Update: The document starts life as an aspose.words document, and I do have the ability to alter it just before it gets turned into a PDF. Is it easier to add this using the .words object?

Hi Scott,

Yes, Once document is loaded in DOM you may add watermark in Aspose.Words and if you don't have any other requirements then Aspose.Words solely will fulfill your requirements.

Best Regardsin