Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert base64 encoding to pdf

Tags:

c#

I tried multiple solution but every time I am getting error that pdf can not be open. This is the edited code with actual data stream that need to be converted in .pdf.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Threading;

namespace ConsoleApplication3
{
class Program
{
    static void Main(string[] args)
    {
        try
        {
            string base64BinaryStr = "Q29tcGFueSBTZXJ2aWNpbmcNCjEuCVRvb2xiYXIgQ29tcGFueSBTZXJ2aWNpbmcgU2VsZWN0IEFuIEFjY291bnQNCjIuCVRvb2xiYXJfQ29tcGFueSBTZXJ2aWNpbmcgRG91YmxlIENsaWNrIG9uIGFuIEFjY291bnQNCjMuCVRvb2xiYXIgQ29tcGFueSBTZXJ2aWNpbmdfRW50ZXIgYWNjb3VudCBudW1iZXIgaW4gdGhlIE51bWJlciBmaWVsZCBvZiB0aGUgQWNvdW50IFNlYXJjaCBTZWN0aW9uDQo0LglUb29sYmFyIENvbXBhbnkgU2VydmljaW5nX1JldHVybg0KNS4JVG9vbGJhciBDb21wYW55IFNlcnZpY2luZ19DbG9zZQ0KNi4JVG9vbGJhciBDb21wYW55IFNlcnZpY2luZ19NZW1vc19DbGVhciBCdXR0b24NCjcuCVRvb2xiYXJfQ29tcGFueSBTZXJ2aWNpbmdfTWVtb3NfQWRkIEJ1dHRvbg0KOC4JVG9vbGJhcl9Db21wYW55IFNlcnZpY2luZ19NZW1vc19BbHQtQSBUbyBBZGQgQSBNZW1vDQo5LglUb29sYmFyX0NvbXBhbnkgU2VydmljaW5nX01lbW9zX0NoYW5nZQ0KMTAuCVRvb2xiYXJfQ29tcGFueSBTZXJ2aWNpbmdfTWVtb3NfRGVsZXRlIEJ1dHRvbg0KMTEuCVRvb2xiYXJfQ29tcGFueSBTZXJ2aWNpbmdfTWVtb3NfUmVmcmVzaA0KMTIuCVRvb2xiYXJfQ29tcGFueSBTZXJ2aWNpbmdfTWVtb3NfQ2xvc2UgQnV0dG9uDQoxMy4JVG9vbGJhcl9Db21wYW55IFNlcnZpY2luZ19TcGVuZGluZyBDb250cm9sX0VsaXRlDQoxNC4JVG9vbGJhcl9Db21wYW55IFNlcnZpY2luZ19TcGVuZGluZyBDb250cm9sX0Rlc2NyaXB0aW9uIGJ1dHRvbg0KMTUuCVRvb2xiYXJfQ29tcGFueSBTZXJ2aWNpbmdfU3BlbmRpbmcgQ29udHJvbF9EZXNjcmlwdGlvbiBidXR0b25fU2VsZWN0IFNwZW5kaW5nIENvbnRyb2wNCjE2LglUb29sYmFyX0NvbXBhbnkgU2VydmljaW5nX1NwZW5kaW5nIENvbnRyb2xfRGVzY3JpcHRpb24gYnV0dG9uX1NwZW5kaW5nIENvbnRyb2wgTGluayB0byBWTA0KMTcuCVRvb2xiYXJfQ29tcGFueSBTZXJ2aWNpbmdfU3BlbmRpbmcgQ29udHJvbF9EZXNjcmlwdGlvbl9DbG9zZSBidXR0b24NCjE4LglUb29sYmFyX0NvbXBhbnkgU2VydmljaW5nX1NwZW5kaW5nIENvbnRyb2xfU2VsZWN0IEFsbF9WaWV3IFRyYW5zYWN0aW9ucw0KMTkuCVRvb2xiYXJfQ29tcGFueSBTZXJ2aWNpbmdfU3BlbmRpbmcgQ29udHJvbF9TcGVuZGluZyBDb250cm9sIExpbmsgdG8gVkwNCjIwLglUb29sYmFyX0NvbXBhbnkgU2VydmljaW5nX1NwZW5kaW5nIENvbnRyb2xfQ2FuY2VsIGJ1dHRvbg0K";

            byte[] sPDFDecoded = Convert.FromBase64String(base64BinaryStr);




            BinaryWriter writer = new BinaryWriter(File.Open(@"c:\Users\u316383\Documents\pdf9.pdf", FileMode.CreateNew));
            writer.Write(sPDFDecoded);

            string s = Encoding.UTF8.GetString(sPDFDecoded);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            Console.ReadLine();
        }
    }
}

}

like image 225
Vicky Avatar asked Jul 17 '14 15:07

Vicky


People also ask

Can you Base64 encode a PDF?

Base64 encoding is used to encode binary data, such as a PDF file, into an ASCII string format that is compatible with systems that can only handle text. For example, email attachments and binary uploads in HTML forms are converted and transmitted as Base64 encoded data.

How do I read a Base64 file?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

Can I decrypt Base64?

You can DECODE the base64 values into bytes (so just a sequence of bits). And from there, you need to know what these bytes represent and what original encoding they were represented in, if you wish to convert them again to a legible format.


1 Answers

Was given the wrong string. It was simple, for any other reference my code was:

byte[] sPDFDecoded = Convert.FromBase64String(base64BinaryStr);
    
File.WriteAllBytes(@"c:\Users\u316383\Documents\pdf8.pdf", sPDFDecoded);

Thanks all.

like image 157
Vicky Avatar answered Sep 17 '22 16:09

Vicky