0

I have searched and I could not find an answer. For example, this doesn't work. My port:

static byte[] RtfToPdf(string rtf)
{
    byte[] pdf = null;

    using (var inputStream = GenerateStreamFromString(rtf))
    using (var outputStream = new MemoryStream())
    {
        var pdfDocument = new iTextSharpDocument();
        var pdfWriter = PdfWriter.GetInstance(pdfDocument, outputStream);

        pdfDocument.Open();

        RtfParser rtfParser = new RtfParser(null);

        rtfParser.ConvertRtfDocument(inputStream, pdfDocument);

        pdfDocument.Close();
        pdfWriter.Close();

        pdf = outputStream.ToArray();
    }

    return pdf;
}

public static MemoryStream GenerateStreamFromString(string value)
{
    return new MemoryStream(Encoding.UTF8.GetBytes(value));
}

That simply copies the text of the RTF to a PDF without any of the formatting that was in the RTF. I am using iTextSharp-LGPL which is version 4.1.6 of iTextSharp.

I personally can't find any useful documentation. iText itself isn't intuitive so I'm having a hard time even guessing about what to try.

user875234
  • 2,399
  • 7
  • 31
  • 49
  • RTF functionality got removed from iText because of legal (copyright) issues. – Amedee Van Gasse Mar 08 '19 at 15:10
  • Do you know which version that happened in? I'm using 4.1.6 and the classes are still there. They just don't work. ....any idea how you would convert a .rtf or .docx file to .pdf or even how you might edit a pdf using c#? – user875234 Mar 08 '19 at 15:36
  • That happened in 5.0.0. I am sure that you are aware that 4.1.6 is deprecated? And as for your question about RTF: I don't know the answer. – Amedee Van Gasse Mar 08 '19 at 16:48
  • Well, with as unhelpful as it has been for me the port of 4.1.6 is apparently helpful to a lot of people. Have a [look](https://github.com/search?q=itextsharp). 875 stars for the 4.1.6 port to .NET Core. The reason everyone is using 4.1.6 is because that's the most recent version since they went to a paid license. – user875234 Mar 08 '19 at 17:31
  • iText 7 works on .NET Core... – Amedee Van Gasse Mar 09 '19 at 13:32
  • Oh and the 876 stars is for the official repo of iText 5 .NET, which is not ported to .NET Core. I am also curious why nobody bothered to submit .NET Core pull requests. – Amedee Van Gasse Mar 09 '19 at 13:34
  • Ah, you are correct. Still, 176 stars is respectable. In any case, [I ended up using LibreOffice to do the document conversion](https://stackoverflow.com/questions/55066373/is-it-at-all-possible-to-convert-a-document-to-pdf-or-edit-a-pdf-in-c-sharp-usin). – user875234 Mar 11 '19 at 11:19
  • For that type of document conversion, that is probably the best solution. Please turn your comment into an answer (with details, not just a link) so it can be upvoted. – Amedee Van Gasse Mar 11 '19 at 11:50

0 Answers0