Exporting Justified Paragraph on RTF to PDF resulting wrong spacing in the PDF

0 Answers 1 View
PdfProcessing WordsProcessing
Mozart
Top achievements
Rank 1
Iron
Veteran
Mozart asked on 07 Nov 2025, 09:09 AM

Hello,

I want to ask how can make the exported PDF from RTF follows spacing on RTF.
I have RTF file (as attached) , when i try tp export to pdf some paragraph not looking exactly like the RTF , i suspect it because using Justified setting.
Is it a bug ? or can we export the RTF to PDF that have exacts look after exporting to PDF ?

i use Telerik.Documents.Core, Fixed, Flow, Flow.FormattedProvider.Pdf , File Version : 2025.2.520.20 
The project is a .NET 8 class library

Please see attached file (RTF source and exported PDF).

private bool ConvertRtfToPdfWithTelerik(string rtfPath, string pdfPath)
{
    try
    {
        Console.WriteLine("Converting RTF to PDF using Telerik Document Processing...");

        // Create RTF format provider
        RtfFormatProvider rtfProvider = new RtfFormatProvider();

        Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider();
        Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;
        
        // Load RTF document
        RadFlowDocument document;
        //Size oSize = new Size();
        //Padding oPadding = null;

        using (FileStream rtfStream = new FileStream(rtfPath, FileMode.Open))
        {
            document = rtfProvider.Import(rtfStream, null);
            //oSize = document.Sections[0].PageSize;
            //oPadding = document.Sections[0].PageMargins;
        }

        //foreach (var item in document.Sections)
        //{
        //    //item.PageMargins = new Padding(90);
        //    item.PageSize = PaperTypeConverter.ToSize(PaperTypes.Letter);
        //}

        Console.WriteLine("RTF document loaded successfully with Telerik");

        // Create PDF format provider
        PdfFormatProvider pdfProvider = new PdfFormatProvider();

        // Export to PDF
        using (FileStream pdfStream = new FileStream(pdfPath, FileMode.Create))
        {
            pdfProvider.ExportSettings.FontEmbeddingType = Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.FontEmbeddingType.Full;
            pdfProvider.ExportSettings.ImageQuality = Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ImageQuality.High;
            pdfProvider.Export(document, pdfStream, null);
        }

        Console.WriteLine("PDF generated successfully using Telerik Document Processing");
        return true;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error converting RTF to PDF with Telerik: " + ex.Message);
        return false;
    }
}

public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase
{
    public override byte[] GetFontData(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties)
    {
        string fontFileName = fontProperties.FontFamilyName + ".ttf";
        string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);

        //The fonts can differ depending on the file 
        if (fontProperties.FontFamilyName == "Calibri")
        {
            if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold)
            {
                fontFileName = $"calibriz.ttf";
            }
            else if (fontProperties.FontStyle == FontStyles.Italic)
            {
                fontFileName = $"calibrii.ttf";
            }
            else if (fontProperties.FontWeight == FontWeights.Normal)
            {
                fontFileName = "calibri.ttf";
            }
            else if (fontProperties.FontWeight == FontWeights.Bold)
            {
                fontFileName = $"calibrib.ttf";
            }
        }

        else if (fontProperties.FontFamilyName == "Century Gothic")
        {
            if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold)
            {
                fontFileName = $"gothicbi.ttf";
            }
            else if (fontProperties.FontStyle == FontStyles.Italic)
            {
                fontFileName = $"gothici.ttf";
            }
            else if (fontProperties.FontWeight == FontWeights.Normal)
            {
                fontFileName = "gothic.ttf";
            }
            else if (fontProperties.FontWeight == FontWeights.Bold)
            {
                fontFileName = $"gothicb.ttf";
            }
        }

        else if (fontProperties.FontFamilyName == "Wingdings")
        {
            if (fontProperties.FontWeight == FontWeights.Normal)
            {
                fontFileName = $"wingding.ttf";
            }
        }

        //...add more fonts if needed... 

        DirectoryInfo directory = new DirectoryInfo(fontFolder);
        FileInfo[] fontFiles = directory.GetFiles();

        var fontFile = fontFiles.FirstOrDefault(f => f.Name.Equals(fontFileName, StringComparison.InvariantCultureIgnoreCase));
        if (fontFile != null)
        {
            var targetPath = fontFile.FullName;
            using (FileStream fileStream = File.OpenRead(targetPath))
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    fileStream.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
            }
        }

        return null;
    }
}


No answers yet. Maybe you can help?

Tags
PdfProcessing WordsProcessing
Asked by
Mozart
Top achievements
Rank 1
Iron
Veteran
Share this question
or