This is my function that converts an RTF document into PDF. The Words.LoadFormat is set to Auto
I am having problems attaching the file, I can email it to you if you tell me at what address. Aspose.Word version 4.4.0.0 works fine.
public static string ConvertToPDF(string fileName, Aspose.Words.LoadFormat fileFormat)
{
string convertedFile = string.Empty;
// License Aspose
Aspose.Words.License wordLicense = new Aspose.Words.License();
wordLicense.SetLicense("Aspose.Custom.lic");
Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
pdfLicense.SetLicense("Aspose.Custom.lic");
// Convert this file into PDF Using Aspose.Word and Aspose.PDF
Aspose.Words.Document wordDoc = new Aspose.Words.Document(fileName, fileFormat, string.Empty);
// If the margins or page size is out of wack, default it
foreach (Aspose.Words.Section currentSection in wordDoc.Sections)
{
if ((currentSection.PageSetup.TopMargin < 0) ||
(currentSection.PageSetup.BottomMargin < 0) ||
(currentSection.PageSetup.LeftMargin < 0) ||
(currentSection.PageSetup.RightMargin < 0) ||
(currentSection.PageSetup.PageWidth < 0) ||
(currentSection.PageSetup.PageHeight < 0)
)
{
// Format as a typical Letter page
currentSection.PageSetup.TopMargin = 18;
currentSection.PageSetup.BottomMargin = 18;
currentSection.PageSetup.LeftMargin = 18;
currentSection.PageSetup.RightMargin = 18;
currentSection.PageSetup.PageHeight = 792;
currentSection.PageSetup.PageWidth = 612;
}
}
MemoryStream msWordDoc = new MemoryStream();
// This next line is what crashes the exe
wordDoc.Save(msWordDoc, Aspose.Words.SaveFormat.AsposePdf);
Aspose.Pdf.Pdf pdfDoc = new Aspose.Pdf.Pdf();
pdfDoc.BindXML(msWordDoc, null);
pdfDoc.IsTruetypeFontMapCached = true;
if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "PDFFonts") == false)
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "PDFFonts");
}
pdfDoc.TruetypeFontMapPath = AppDomain.CurrentDomain.BaseDirectory + "PDFFonts";
convertedFile = fileName.Replace(Path.GetExtension(fileName), ".PDF");
System.IO.FileStream fs = new System.IO.FileStream(convertedFile, System.IO.FileMode.Create);
pdfDoc.Save(fs);
fs.Close();
fs.Dispose();
msWordDoc.Close();
msWordDoc.Dispose();
return convertedFile;
}