Print a stream

DaWaDaWa Member ✭✭

Hi,

I would like to print a stream with my printer.

For the first step it would be sufficent to get it running on UWP.

I already saw a lot of solutions (https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/print-from-your-app) by using PrintDocument but these examples want to print a page and I only want to print a stream.

Is there a way to do this?

Thx,
DW

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    You could try what this blog said.
    Also refer to this sample for detailed code. It used dependency to consume a stream in Forms and print a pdf file on UWP platform.

  • DaWaDaWa Member ✭✭

    @LandLu thanks for your reply but I want to print my stream with a printer on a paper and I can not see that it will be done in your solution. Do you have any other advise?

  • DaWaDaWa Member ✭✭
    edited December 2018 Accepted Answer

    For all interested, this link helped me to print my stream:
    https://github.com/bushbert/XamarinPCLPrinting

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @LandLu said:
    Also refer to this sample for detailed code. It used dependency to consume a stream in Forms and print a pdf file on UWP platform.

    Hey @LandLu,

    Thanks for the awesome sample app! Worked first go for me! :-) (though now that I'm replying I can see it actually came from Syncfusion. Damn, you miss out on the credit there then! ;-) )

    Now of course though, I need to move beyond printing an embedded resource to one that has been generated dynamically. The relevant PDF has been saved as a file in UWP, and I figured I could substitute a FileStream for the pdfViewer stream, however when VS tried to install System.IO.FileSystem it told me that it wasn't compatible with the targeted framework, so how would I modify this app to read a file instead?

    note: I don't really need to see the PDF in the app, and so can get by without pdfViewer, unless it's necessary for other parts of the code? i.e. I'm looking for what code can I use to get a file instead where there's printStream=pdfViewer.SaveDocument() in the UWP project. I can't afford Syncfusion,but am open to a free PDFviewer if that's needed.

    thanks,
    Donald.

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @DaWa said:
    @LandLu thanks for your reply but I want to print my stream with a printer on a paper and I can not see that it will be done in your solution. Do you have any other advise?

    Not sure where you were looking, but it definitely prints to paper. I tried it and it worked first go on UWP!

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    Oh, P.S. @LandLu, is there a way to bypass the print dialogue? The app I'm doing will be doing a lot of printing, and I don't want the users having to respond to the pop-up every time - would prefer to set the printer via a settings page which the user only has to set-up once.

  • JohnHardmanJohnHardman GBUniversity mod
    edited August 28

    @LandLu said:
    You could try what this blog said.
    Also refer to this sample for detailed code. It used dependency to consume a stream in Forms and print a pdf file on UWP platform.

    Do you find that the Print Preview works on UWP using this code?

    cc @SmartmanApps

  • JohnHardmanJohnHardman GBUniversity mod
    edited August 30

    @DaWa said:
    For all interested, this link helped me to print my stream:
    https://github.com/bushbert/XamarinPCLPrinting

    Did you also implement this for Android? That sample doesn't currently have an Android implementation. If so, could you share your code pls?

  • BushbertBushbert Member ✭✭✭

    I added the android implementation to the sample project.

  • JohnHardmanJohnHardman GBUniversity mod

    @Bushbert said:
    I added the android implementation to the sample project.

    Thanks @Bushbert. Unfortunately, the code doesn't look correct.

            void IPrint.Print(byte[] content)
            {
                //Android print code goes here
                Stream inputStream = new MemoryStream(formData);
    

    I assume that formData needs to be replaced by content?

  • BushbertBushbert Member ✭✭✭

    @JohnHardman said:

    @Bushbert said:
    I added the android implementation to the sample project.

    Thanks @Bushbert. Unfortunately, the code doesn't look correct.

            void IPrint.Print(byte[] content)
            {
                //Android print code goes here
                Stream inputStream = new MemoryStream(formData);
    

    I assume that formData needs to be replaced by content?

    Yeah, i've updated it again. Copied and pasted from another project :-)

  • JohnHardmanJohnHardman GBUniversity mod
    edited September 11

    Thanks @Bushbert

  • SmartmanAppsSmartmanApps AUMember ✭✭✭
    edited September 17

    @JohnHardman said:
    Do you find that the Print Preview works on UWP using this code?

    cc @SmartmanApps

    Hi @JohnHardman,

    Sorry for not replying sooner, but I never got a notification that you tagged me! I only just saw your post by chance when I came back here to download the package again.

    Yes! It does work... but that comes with some qualifiers.

    It's REALLY old. i.e. Xamarin Forms 2.4! And it's PCL. And it uses Syncfusion (which I can't afford). It uses the embedded Syncfusion PDFviewer to print an embedded PDF. Obviously I need to be able to print other PDF's. It takes a stream from the PDFviewer, so I figured I could substitute another kind of stream and it should still work. e.g. Filestream. However, being PCL, it told me it didn't have System.IO.Filesystem, and it wouldn't update to it either due to being old PCL. Another option would be to use a Dropbox stream, which I'm currently trying to get working (I just successfully implemented a Dropbox stream with something else last week, so seeing if I can apply it here).

    The problem is when I tried updating the packages, it failed to update the XF UWP package for some reason (it updated the other projects ok), and it breaks the whole thing. I think it's going to come down to having the write the whole thing from scratch with modern packages and .NET standard in the first place, but using the sample app as a guide. I'm just trying to get it working with a Dropbox stream as proof of concept that it's going to work before undertaking all that coding (but I still need to do some coding even to do that, as the sample app hasn't been written follwing MVVM, whereas that's the way I wrote my Dropbox package, and not working when I try to just stick it in the sample app page, so looks like I need to set-up some MVVM just to try that).

    If this is something you're interested in at the moment, and know of any relevant shortcuts I can take to get there, I'm all ears. But in short, yes it works, but it's too old to be integrated with anything and needs an updated version of it.

    thanks,
    Donald.

  • JohnHardmanJohnHardman GBUniversity mod

    Thanks @SmartmanApps

    I'm in the midst of putting together some code for my .Net Standard codebase to get printing working across Android, iOS and UWP. Initially, I'm putting together bits for printing Images, WebViews and PDFs. Unfortunately, I'm not getting much time to do this, just odd minutes here and there, but I'll let you know how I get on. Thankfully, I can make use of Syncfusion if I find it useful.

    @SmartmanApps said:
    The problem is when I tried updating the packages, it failed to update the XF UWP package for some reason (it updated the other projects ok), and it breaks the whole thing.

    Yes, I tried that too and hit the same problem.

    @SmartmanApps said:
    I think it's going to come down to having the write the whole thing from scratch with modern packages and .NET standard in the first place, but using the sample app as a guide.

    Yes, that's what I'm doing, although tied in with the rest of my codebase currently rather than being independent of it (so using helpers for error handling, task handling etc).

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @JohnHardman said:
    Yes, that's what I'm doing, although tied in with the rest of my codebase currently rather than being independent of it (so using helpers for error handling, task handling etc).

    Hey @JohnHardman,

    Any chance of at least writing the core part in a re-usable way and sharing it? I can potentially help if you want (though on my end I'm working on making my Dropbox solution ready to be shared publicly, which you've expressed interest in before). It seems a waste of time for us to be both writing the same thing at the same time. As you've probably already found, Xamarin is desperately in need of an up-to-date printing class (couldn't tell you how much time I've spent already trying to find one).

    thanks,
    Donald.

  • JohnHardmanJohnHardman GBUniversity mod

    @SmartmanApps said:
    Any chance of at least writing the core part in a re-usable way and sharing it?

    Happy to share once working. I'll have to take a look to see how much work it will be to de-couple it from other parts of my codebase. Will let you know.

Sign In or Register to comment.