Word2PDF in COM ASP (Example code)

Spent quite some time trying to figure out how to do this as there were no cut-and-paste examples .... so here you go, in case it is of some help to others like ourselves.

JScript ASP page implementing Word to PDF using the COM interface ...

========================

<%@ LANGUAGE = "jscript" %>



Begin ...
<%
var baseFile = "C:\\Inetpub\\wwwroot\\WordSamples\\test3";

/* =====================
Open Word & Save
to Aspose.Pdf XML
===================== */
var lic = Server.CreateObject("Aspose.Words.License");
lic.SetLicense("C:\\Inetpub\\wwwroot\\WordSamples\\Aspose.Custom.lic");

var helper = Server.CreateObject("Aspose.Words.ComHelper");
var doc = helper.Open(baseFile + ".doc");
doc.Save_2(baseFile + ".xml", 3);
doc = null;
helper = null;
lic = null;

/* =====================
Convert to PDF
===================== */
var lic = Server.CreateObject("Aspose.Pdf.License");
lic.SetLicense("C:\\Inetpub\\wwwroot\\WordSamples\\Aspose.Custom.lic");

var pdf = Server.CreateObject("Aspose.Pdf.Pdf");
pdf.BindXML_2(baseFile + ".xml",null);
pdf.isImagesInXmlDeleteNeeded = true;

// OPTIONAL: Set PDF custom properties
// ===================================
pdf.author = "Demo User";
pdf.creator = "Demo System";
pdf.keywords = "Hot Document";
pdf.subject = "Report on Things";
pdf.title = "Customer #123";

// OPTIONAL: Set PDF document security
// ===================================
var sec = Server.CreateObject("Aspose.Pdf.Security");
sec.isContentsModifyingAllowed = false;
pdf.security = sec;
sec = null;

pdf.Save(baseFile + ".pdf");
pdf = null;
lic = null;

/* =====================
Remove Interim
XML File
===================== */
var fs = Server.CreateObject("Scripting.FileSystemObject");
fs.DeleteFile(baseFile + ".xml");
fs = null;

%>
End!

Thanks for your valuable feedback. If you don't object I am going to put these examples to a Wiki article so that the other users could benefit from it as well.

I will be happy to put a reference in this article if you will kindly provide your name here.

Thanks again & Best regards,

I am getting an error here…

Aspose.Words error ‘80070057’

Invalid save format requested.

@edgararroyo Thanks for your request. The conversion to PDF method described in the original post is obsolete. Now Aspose.Words does not require Aspose.Pdf to convert document to PDF. Simply specify SaveFormat.Pdf, 40 in COM. See SaveFormat enumeration to learn more about save formats supported by Aspose.Words.

1 Like

@alexey.noskov Thank you. I ended up using doc.Save (basefile + “.pdf”) and it worked. Do I need to change it to doc.Save_2 (basefile + “.pdf” , 40) instead of doc.Save (basefile + “.pdf”) ?

@edgararroyo No, it is not required. doc.Save (basefile + “.pdf”) determines save format by extension.

1 Like

Ok, thank you.