C# - Convert a PDF File to Base64String
C# Convert a PDF File to Base64String
- Create a Windows Forms Project
- Name it “ConvertFileToBase64”
- Open Form1.cs and add a Label, two textboxes and a button
- 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);
|
- Run the project
- Click on the “Select File” button and pick a file of your choice
- The result
- The following part of code will copy the text to the clipboard in order to save you some time
Comments
Post a Comment