1

I’m searching for quite a while how to print “PDF” files in c# , I’m trying to print shipping labels which I get it in a “GZip Stream” string and the format is a pdf, So my question is what’s is the best way to print out the “PDF” label (Not images or any image format), and also be able to set to which printer to print? The best way would be without having to save the label in my computer and then recall the file! it's doesn't make sense that the only way is to install third party classes!

This is what i have done!

    private void PrintFDFLabel(string imageLabel)
    {
        var byteStream = Convert.FromBase64String(imageLabel);
        MemoryStream memoryStream = Decompress(byteStream);// i need to Decompress the Gzip

        PrintDocument print = new PrintDocument();

        print.PrinterSettings.PrinterName = Properties.Settings.Default.DefaultPrimePrinter;
        print.Print();

    }

    private MemoryStream Decompress(byte[] b)
    {
        MemoryStream memoryStream;
        using (var ms = new MemoryStream())
        {
            using (var bs = new MemoryStream(b))
            using (GZipStream gZipStream = new GZipStream(bs, CompressionMode.Decompress))
            {
                memoryStream = new MemoryStream();
                gZipStream.CopyTo(memoryStream);
            }
            return memoryStream;
        }
    }
cmark
  • 21
  • 3
  • there are several examples online in regards to printing PDF from a `MemoryStream` did you execute a simple google search at least..? – MethodMan Nov 22 '16 at 15:39
  • yes! i did a lot research but i could not find even one example ! the only thing i found is third party classes! which this is not what i'm looking for! if you have any example please link me one thanks – cmark Nov 22 '16 at 15:41
  • http://stackoverflow.com/questions/1392852/how-do-i-send-a-pdf-in-a-memorystream-to-the-printer-in-net – Dan Wilson Nov 22 '16 at 15:41
  • You need to convert PDF markup to something your printer understands, like PostScript, PCL, etc. Something has to do the conversion, and the logic is non-trivial, hence the use of 3rd party libraries. – Dan Wilson Nov 22 '16 at 15:42
  • the link you shared i'm not able to set printers – cmark Nov 22 '16 at 15:45
  • @cmark then you'll first have to set the default printer of windows http://stackoverflow.com/questions/971604/how-do-i-set-the-windows-default-printer-in-c and after execute the adobe reader command with the print argument – Innat3 Nov 22 '16 at 15:52
  • @cmark did u solved it? if yes then how u solved it? I am in similar situation, it will be really helpful for me :) – user3085082 Sep 09 '20 at 09:06

0 Answers0