2

I am trying to generate pdf file using Byte Array. The code is used is:

Public Sub SavePdf(ByVal bytes() As Byte)
    Dim filePath As String = HttpContext.Current.Server.MapPath("~/ClientBin/file1.pdf")
    Dim byteData() As Byte = bytes
    File.WriteAllBytes(filePath,byteData)
End Sub

The byte array is generated using richEditControl. The code is as follows:

 Public Sub generateByte_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim ms As MemoryStream = New MemoryStream()
    ms.Flush()
    richEditControl1.SaveDocument(ms, DocumentFormat.Rtf)
    client.SavePdf(ms.ToArray())
    MessageBox.Show("FileSaved")
End Sub

The file so generated can't be open.

Anish
  • 1,920
  • 11
  • 28
  • 48

1 Answers1

2

I don't think .Rtf (Rich Text Format) data is equivalent to .Pdf (Portable document format).

It appears you are using the DevExpress RichEditControl which doesn't appear to support saving PDF files directly, see the documentation for available document formats.

I'd recommend either saving the RTF data with the .Rtf extension and use an RTF to PDF document converter after writing, or using a Pdf document writer library like PDFsharp.

To convert a .Rtf file to .Pdf you could either automate Microsoft Word (if you can assume it will be on the target machine) or use a standalone .Net library, e.g.

Phillip Trelford
  • 6,513
  • 25
  • 40
  • I need to export it to pdf – Anish Apr 09 '14 at 06:13
  • @Ngeunpo see SO answer Convert RTF to PDF format http://stackoverflow.com/a/21747316/2012417 & Free RTF to PDF .NET component or tool? http://stackoverflow.com/a/5596782/2012417 – Phillip Trelford Apr 09 '14 at 06:22
  • @Ngeunpo you may also want to look at commercial offerings like http://www.sautinsoft.com/products/pdf-metamorphosis/ or http://www.html-to-pdf.net/rtf-to-pdf.aspx – Phillip Trelford Apr 09 '14 at 06:27
  • as you said, it works fine with .rtf format. Can you help me on the conversion step [Rtf to Pdf] – Anish Apr 09 '14 at 06:27
  • @Ngeunpo you can either use Microsoft Word automation or an RTF to PDF library, I've updated the answer with some details – Phillip Trelford Apr 09 '14 at 06:45