C# - Convert a PDF File to Base64String

C# Convert a PDF File to Base64String


  1. Create a Windows Forms Project
  1. Name it “ConvertFileToBase64”


  1. Open Form1.cs and add a Label, two textboxes and a button




  1. Double click on the button and add the following code:


string userPath;
userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Desktop";
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.InitialDirectory = userPath;
        ofd.Filter = "html files (*.html)|*.txt|All files (*.*)|*.*";
        ofd.FilterIndex = 2;
        ofd.RestoreDirectory = true;
        ofd.ShowDialog();
        string fName = ofd.FileName;
        textBox1.Text = fName;
  
        var doc = File.ReadAllBytes(textBox1.Text);
        textBox2.Text = Convert.ToBase64String(doc);
        Clipboard.SetText(textBox2.Text);

  1. Run the project
  1. Click on the “Select File” button and pick a file of your choice


  1. The result
  1. The following part of code will copy the text to the clipboard in order to save you some time

Comments

Popular posts from this blog

Adding IQ Driver to LINQPad and connecting to ORACLE Database

Windows Azure BizTalk SB adapter to Windows Azure Queue and back - Simple Example