Save to pdf after upgrade to aspose.words 10

After upgrading my application the save to pdf is no longer working. The process converts to pdf in order to add to an email as an attachment.

MemoryStream stream = new MemoryStream();
wordDoc.Save(stream, Word.SaveFormat.Pdf);
stream.Seek(0, SeekOrigin.Begin);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stream);
Pdf.Pdf pdfDoc = new Pdf.Pdf();
pdfDoc.BindXML(xmlDoc, null);
pdfDoc.IsImagesInXmlDeleteNeeded = true;
return pdfDoc;

I modified the saveFormat as per the upgrade notes but it still fails.

Please advise.

Hi
Thanks for your request. Currently Aspose.Words supports direct conversion to PDF (without using Aspose.Pdf). So, you should use direct method to convert your document to PDF. Here is the code:

Document doc = new Document("in.docx");
doc.Save("out.pdf");

Best regards,

Replacing the stream?

MemoryStream stream = new MemoryStream();
wordDoc.Save(stream, Word.SaveFormat.Pdf);

Hello
Thanks for your request. If you need to save your document to a stream you should use the following code:

Document doc = new Document("C:\\Temp\\Test.docx");
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Pdf);

Please see the following link to learn how to save the document to PDF:
https://docs.aspose.com/words/net/convert-a-document-to-pdf/
Best regards,

I do have aspose.pdf is it not compatible with the newer version of aspose.words?

Hi
Thanks for your request. Currently the latest version of Aspose.Words supports direct way of PDF conversion (without using Aspose.Pdf). Old legacy method which used Aspose.PDF was excluded from the latest version.
Why do you need to use Aspose.PDF?
Best regards,

I appreciate your patience.

I have a series of documents to attach and send out as attachments on an email. The problem is that it was written to create the pdf on the fly and send it out without saving it to a location on the server/workstation. This no longer works and I am not sure how to accomplish the same thing with aspose.words.

This is the calling code and the pdf code that I had been using until this update.

This calls it:

{
    // convert to .pdf
    Word.Document wd = new Aspose.Words.Document(dr["PhysicalLocation"].ToString());
    Pdf.Pdf pd = OMSDocumentOperations.convertWordtoPDF(wd);
    MemoryStream ms = new MemoryStream(pd.GetBuffer());
    data = new Attachment(ms, dr["DocLocation"].ToString().Replace(".doc", ".pdf"));
}

and this is what conversion code

public static Word.Pdf convertWordtoPDF(Word.Document wordDoc)
{
    MemoryStream stream = new MemoryStream();
    wordDoc.Save(stream, Word.SaveFormat.Pdf);
    stream.Seek(0, SeekOrigin.Begin);
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(stream);
    Pdf.Pdf pdfDoc = new Pdf.Pdf();
    pdfDoc.BindXML(xmlDoc, null);
    pdfDoc.IsImagesInXmlDeleteNeeded = true;
    return pdfDoc;
}

Hello
Thanks for your request. I think you should use the following code:

// convert to .pdf
Document wd = new Document(dr["PhysicalLocation"].ToString());
MemoryStream ms = new MemoryStream();
wd.Save(ms, SaveFormat.Pdf);
data = new Attachment(ms, dr["DocLocation"].ToString().Replace(".doc", ".pdf"));

Best regards,

Thank you very much - I will try it out now.

R

Thank you for your help. I was able to generate the pdf and attach to an email but in some cases it is failing when I then try to open the attached pdf. My process is to create a the document from an html document and save it to the server. Then when the user wants to include it as an email attachment it converts it to pdf to send out. For this reason I used stream to create the pdf. My code is listed below.

The error message when trying to open in acrobat 7 is that it could not open because it is either not a supported file type or because it is damaged. I am able to open the generated word document and print to pdf without an error.

// convert to .pdf
Aspose.Words.Document wd = new Aspose.Words.Document(dr["PhysicalLocation"].ToString());
MemoryStream ms = new MemoryStream();
wd.Save(ms, Word.SaveFormat.Pdf);
data = new Attachment(ms, dr["DocLocation"].ToString().Replace(".doc", ".pdf"));

Hi Rose,
Thanks for your inquiry.
Could you please attach one of the output PDF’s that is not working to this thread? We will look into it and provide you with some further feedback.
Thanks,

I am attaching a pdf.

I must be tired - I do not see how to attach it to the issue.

Hi
Thank you for additional information. The file you have attached is zero size. I suppose, the problem occurs because after saving position of the stream is at the end of it. Please try using the following code:

Aspose.Words.Document wd = new Aspose.Words.Document(dr["PhysicalLocation"].ToString());
MemoryStream ms = new MemoryStream();
wd.Save(ms, Word.SaveFormat.Pdf);
ms.Position = 0;
data = new Attachment(ms, dr["DocLocation"].ToString().Replace(".doc", ".pdf"));

Hope this helps.
Best regards,

Thank you - that took care of it.