Converts array of RTF bytes into array of PDF bytes.

Namespace:  SautinSoft
Assembly:  PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 6.7.12.19

Syntax

      
 C#  Visual Basic 
public byte[] RtfToPdfConvertByte(
	byte[] inputBytes
)
Public Function RtfToPdfConvertByte ( _
	inputBytes As Byte() _
) As Byte()

Parameters

inputBytes
array< Byte >[]()[]
RTF document as bytes array

Return Value

PDF document as byte array, or null in case of converting failed

Remarks

Examples

CopyHow to create PDF from RichTextBox in memory using C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace SampleWinForms
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Button button1;
        private Button button2;
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            InitializeComponent();
        }
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Location = new System.Drawing.Point(24, 24);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(440, 176);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";            
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(252, 220);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(96, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Generate PDF";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(141, 220);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(96, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "Open RTF";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(480, 300);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.richTextBox1);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.Text = "From RichTextbox to PDF";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {

        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();

            if (p != null)
            {
                string pdfPath = @"..\..\..\..\..\test.pdf";
                //1. Get RTF content

                string rtfString = this.richTextBox1.Rtf;

                //2. Converting RTF to PDF
                byte[] pdf = p.RtfToPdfConvertByte(rtfString);

                if (pdf != null)
                {
                    //3. Save to PDF file
                    File.WriteAllBytes(pdfPath, pdf);
                    System.Diagnostics.Process.Start(pdfPath);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "RTF documents (*.rtf)|*.rtf|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.Title = "Select any RTF document:";
            openFileDialog1.Multiselect = false;
            openFileDialog1.InitialDirectory = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Rtf = File.ReadAllText(openFileDialog1.FileName, System.Text.Encoding.UTF8);
            }
        }

    }
}
CopyHow to create PDF from RichTextBox in memory using VB.Net
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.IO

Namespace SampleWinForms
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private WithEvents richTextBox1 As System.Windows.Forms.RichTextBox
        Private WithEvents button1 As System.Windows.Forms.Button
        Private WithEvents button2 As System.Windows.Forms.Button
        Private components As System.ComponentModel.Container = Nothing

        Public Sub New()
            InitializeComponent()
        End Sub
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not components Is Nothing Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

#Region "Windows Form Designer generated code"
        ''' <summary>
        ''' Required method for Designer support - do not modify
        ''' the contents of this method with the code editor.
        ''' </summary>
        Private Sub InitializeComponent()
            Me.button2 = New System.Windows.Forms.Button
            Me.button1 = New System.Windows.Forms.Button
            Me.richTextBox1 = New System.Windows.Forms.RichTextBox
            Me.SuspendLayout()
            '
            'button2
            '
            Me.button2.Location = New System.Drawing.Point(174, 295)
            Me.button2.Name = "button2"
            Me.button2.Size = New System.Drawing.Size(96, 23)
            Me.button2.TabIndex = 5
            Me.button2.Text = "Open RTF"
            Me.button2.UseVisualStyleBackColor = True
            '
            'button1
            '
            Me.button1.Location = New System.Drawing.Point(290, 295)
            Me.button1.Name = "button1"
            Me.button1.Size = New System.Drawing.Size(96, 23)
            Me.button1.TabIndex = 4
            Me.button1.Text = "Generate PDF"
            '
            'richTextBox1
            '
            Me.richTextBox1.Location = New System.Drawing.Point(12, 12)
            Me.richTextBox1.Name = "richTextBox1"
            Me.richTextBox1.Size = New System.Drawing.Size(542, 277)
            Me.richTextBox1.TabIndex = 3
            Me.richTextBox1.Text = ""
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(566, 330)
            Me.Controls.Add(Me.button2)
            Me.Controls.Add(Me.button1)
            Me.Controls.Add(Me.richTextBox1)
            Me.Name = "Form1"
            Me.Text = "How to generate PDF from RichTextBox"
            Me.ResumeLayout(False)

        End Sub
#End Region

        Shared Sub Main()
            Application.Run(New Form1())
        End Sub

        Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
            Dim openFileDialog1 As System.Windows.Forms.OpenFileDialog = New OpenFileDialog()
            openFileDialog1.Filter = "RTF documents (*.rtf)|*.rtf|All files (*.*)|*.*"
            openFileDialog1.FilterIndex = 0
            openFileDialog1.Title = "Select any RTF document:"
            openFileDialog1.Multiselect = False
            openFileDialog1.InitialDirectory = ""

            If openFileDialog1.ShowDialog() = DialogResult.OK Then
                richTextBox1.Rtf = File.ReadAllText(openFileDialog1.FileName, System.Text.Encoding.UTF8)
            End If
        End Sub

        Private Sub button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
            Dim p As New SautinSoft.PdfMetamorphosis()

            If p IsNot Nothing Then
                Dim pdfPath As String = "..\..\..\..\..\test.pdf"
                '1. Get RTF content


                Dim rtfString As String = Me.richTextBox1.Rtf

                '2. Converting RTF to PDF
                Dim pdf() As Byte = p.RtfToPdfConvertByte(rtfString)

                If pdf IsNot Nothing Then
                    '3. Save to PDF file
                    File.WriteAllBytes(pdfPath, pdf)
                    System.Diagnostics.Process.Start(pdfPath)
                End If
            End If
        End Sub
    End Class
End Namespace

See Also