0

I have a program that uses WebClient to download a file from a remote server. I am using DownloadData rather than DownloadFile because I don't really need the physical file. I am just taking the byte array and converting it to a base64 string for submission through an API as part of the payload. For some files, I will download them as RTF, but they need to be converted to a searchable PDF before I send them through the API. I'd rather handle this conversion with just the byte arrays in memory, rather than saving files to disk, then converting. Is there a way for me to take the RTF byte array and convert it to a (searchable) PDF byte array?

I tried just saving the rtf byte array to a pdf, but that does not work. Here's my code for that: System.IO.File.WriteAllBytes(@"C:\temp\pdfTest.pdf", fileBytes);

Ben Walker
  • 2,037
  • 5
  • 34
  • 56
  • 3
    rtf != pdf. They have different names because they're completely different formats. What does the API do? rtf is just as searchable as pdf, if not more. – Millie Smith Oct 22 '15 at 05:17
  • yes, rtf is certainly searchable, but this particular document is required to be a searchable pdf when submitted to this third-party api. If I have to, I'll perform the file download, then convert to pdf, but I'd like to just keep it in memory if possible. – Ben Walker Oct 22 '15 at 05:19
  • Why can't you just convert it to a pdf in memory? Also, I was asking about the API because maybe there's one that operates on rtf? – Millie Smith Oct 22 '15 at 05:19
  • You need some software that can parse RTF and emit pdf. Do you have such a library? – David Heffernan Oct 22 '15 at 05:20
  • 1
    I was thinking of using a pdf printer like cutepdf if I do this with file i/o. I'm not sure if those libraries have functionality for byte arrays, but I suppose they might. I'll research it a bit. – Ben Walker Oct 22 '15 at 05:29

1 Answers1

0

The following solution requires a Telerik subscription, but I am using it to convert an rtf byte array to a pdf byte array:

byte[] bOutput;
Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider RTFprovider = new Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider();
string strRTF = System.Text.Encoding.ASCII.GetString(bInput);
Telerik.Windows.Documents.Flow.Model.RadFlowDocument document = RTFprovider.Import(strRTF);
        Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider PDFprovider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
        bOutput = PDFprovider.Export(document);
codeky
  • 1