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

VBA Randomize a 3x3 Filter for Convoluted Neural Network

Adding IQ Driver to LINQPad and connecting to ORACLE Database

Upload a file to a Web server in ASP.NET by using Visual C# .NET