Free Support Forum - aspose.com

Convert RTF to PDF is not working correctly

Hi Support,

Im trying to convert the rtf content to pdf format. Im using your latest Aspose.words version 18.8.

Below is the code I used for conversion.

Please find the attached rtf content file (rtf_content12.rtf) and output pdf (Notes-RTF-060920181727.pdf).

public bool ConvertNotesRTFtoPDF()Notes-RTF-060920181727.pdf (73.3 KB)

{

    // Get the Application Path & Add the RTF Content File Name
    string appPath = System.IO.Directory.GetCurrentDirectory().Replace("\\bin\\Debug", "");
    string rtfBodypath = appPath + @"\HTML\RTF_content12.rtf";
    // Name the Output File 
    string fileName = appPath + @"\Output\" + "Notes-RTF-" + DateTime.Now.ToString("ddMMyyyyHHmm") + ".pdf";

    // Read the RTF Contents
    string rtfString = ReadFromFile(rtfBodypath);

    // Convert to byteArray
    MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString));
    byte[] bytesOfHtmlString = stream.ToArray();

    using (MemoryStream memStream = new MemoryStream())
    {
        // Write the Body of HTML to PDF
        memStream.Write(bytesOfHtmlString, 0, bytesOfHtmlString.Length);
        memStream.Position = 0;
        //Aspose.Words.RtfLoadOptions loadOptions = new Aspose.Words.RtfLoadOptions();
        //loadOptions.RecognizeUtf8Text = true;

        Aspose.Words.Document doc = new Aspose.Words.Document(memStream);
        doc.Save(fileName);
    }

    return true;
}<a class="attachment" href="/uploads/discourse_instance3/18492">rtf_content12.zip</a> (74.0 KB)

@rajendranb

Thanks for your inquiry. Unfortunately, we have not found the RTF file with your post. Please ZIP and attach it here for testing. We will investigate the issue on our side and provide you more information.

Thanks for your reply. Sorry for the inconvenience. Please find the attached zip file.rtf_content12.zip (74.0 KB)

@rajendranb

Thanks for sharing the document. Your input RTF contains some spaces before and after RTF content. Please use following code example to get the desired output.

byte[] rtfBytes = Encoding.UTF8.GetBytes(File.ReadAllText(MyDir + "rtf_content12.rtf").Trim());
MemoryStream stream = new MemoryStream(rtfBytes);
Aspose.Words.LoadOptions options = new Aspose.Words.LoadOptions();
options.LoadFormat = LoadFormat.Rtf;
Aspose.Words.Document doc = new Aspose.Words.Document(stream, options);
doc.WarningCallback = new HandleDocumentWarnings();
doc.Save(MyDir + "18.9.pdf");