Question How to Convert .RTF to PDF using PDFSharp?

mashispano

Member
Joined
Feb 5, 2024
Messages
6
Programming Experience
3-5
Hi Guys, I need help converting .RTF File format to .PDF using PDFSharp Version 1.50 my output result is always a blank page, I would really appreciate your help and sample code.

Here is my ASP.NET C# Code
RTF To PDF Help:
using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.Adapters;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using TheArtOfDev.HtmlRenderer.PdfSharp;
    using PdfSharp;
    using PdfSharp.Drawing;
    using PdfSharp.Pdf;
    using PdfSharp.Pdf.IO;
    using MigraDoc.DocumentObjectModel;
    using MigraDoc.Rendering;
    using System.Threading;
    using System.Threading.Tasks;

    protected void ConvertRtf2Pdf()
    {
        //- Source RTF Filename and path
        var MySourceRTFile = HttpContext.Current.Server.MapPath("~/templates/sample.rtf");
        //- Target PDF Filename and path
        var _sFileName = "SAMPLE_" + DateTime.Now.ToString("yyyyMMdd") +
             DateTime.Now.ToString("HH:mm:ss").Trim().Replace(":", "") + ".pdf";
        var MyTragetPDFile = HttpContext.Current.Server.MapPath("~/export/" + FileName);
        //- Read the Source RTF file
        string docfi = System.IO.File.ReadAllText(MySourceRTFile);
        //- Start creating the PDF document
        PdfDocument pdfDoc = new PdfDocument();
        PdfPage pdfPage = pdfDoc.AddPage();
        XGraphics graph = XGraphics.FromPdfPage(pdfPage);
        XFont font = new XFont("Time New Roman", 0, XFontStyle.Regular);
        graph.DrawString(docfi, font, XBrushes.Black, new XRect(0, 0, 0, 0),
                        XStringFormats.Default);
        //- Final result
        pdfDoc.Save(MyTragetPDFile);
        pdfDoc.Dispose();
    }


I have no problem converting flat .TXT file my only problem is converting .RTF(Rich Text File) to .PDF

Thanks
Al
 
Set a breakpoint on line 38. What is the value of docfi? If it's blank or an empty string, then there's the reason why you get a blank page.

Also check the documentation for your call to DrawString(), if that XRect that you are passing in defines the bounds to draw the string into, then it looks like you are passing in a rectangle with zero width and height -- effectively an empty rectangle. Nothing will fit inside an empty rectangle.
 
Thank you for the reply, the value is not blank, System.IO.File.ReadAllText(MySourceRTFile) is reading the whole document and it results value is exact what the document .RTF has.
 
Are you saying that if you use that exact same code with TXT file instead of an RTF file, you see the expected result? If that is the case then the only thing I can think of is that some RTF control character(s) is somehow messing with the way PDFSharp works.

By the way, assuming it worked the way you expected, it looks like that code should output the RTF markup from the file, rather than rendered RTF. Is that what you're trying to do?
 
Thank you, guys, for all your help, after all research and testing coding finally found out that PDFSharp is not the solution to convert .RTF format to PDF. I found that Spire.PDF does the job with very minimum coding. Spire.PDF 10.2.2

Thanks again.
 

Latest posts

Back
Top Bottom