Printing Memory Stream in Windows Forms

Hi All,

I’ve written a user control that’s fed a memory stream created from a multi-page tif. The front end is written in VS2010 C# and the user control in WPF4. I haven’t been able to find a way to print a memory stream in WPF4, and so I was wondering if there’s a way with the Aspose pdf kit.

Thanks!

Hi Larry,

As I understand, you’re trying to print a TIFF file. I’m sorry to share with you that Aspose.Pdf.Kit for .NET only allows you to print a PDF file; it can be used with .NET 4.0 as well. The code snippets are given in the following samples:
Printing PDF files using Printer Settings and Page Settings
Printing to an XPS File and Hiding Print Dialog

However, I’m afraid, printing TIFF file is not supported in Aspose.Pdf.Kit.

We’re sorry for the inconvenience. If you have any further questions, please do let us know.
Regards,

Actually, I don’t even have a TIFF file, only a memory stream that came from a TIFF file. However, from what you said it doesn’t sound like Aspose can help. I’d probably have to convert the memory stream to a PDF on hard disk, then print. I was trying to avoid saving to a file.

Thanks!

I just found out that the memory stream doesn’t actually come from a TIFF at all. It actually begins as an output from a fax, stored as binary in a SQL db, read into a byte array, and then converted into a memory stream.

Wow

So, how do you print this animal?? Save it one page at a time to a canvas and PrintVisual? Convert it back to a byte array and do something? Use another tool like an Image control (probably lose paging) or ReportViewer or something?

ANY ideas???

Hi Larry,

As I mentioned earlier, Aspose.Pdf.Kit can only print a PDF file; I’m afraid, in order to print this you need to convert it to PDF file. The final data converted to memory stream seems like an image. Am I right? If so, you might try Aspose.Pdf which allows you to create a PDF file from scratch and then you’ll be able to use Aspose.Pdf.Kit to print that file. Kindly share your further thoughts, so I could guide you accordingly.

We’re sorry for the inconvenience.
Regards,

Thanks Shahzad!

Actually, I found a way to do it from within WPF4. You can create a flow document and add "blocks" of images to the document, and then print the flow document. This seems to work, but may not be an ideal solution in the long run, especially if watermarks or plain text ever have to be processed. I also had to resize the images so it didn't create extra blank pages.

I'd still be interested in a pure Aspose approach, however, I'd rather avoid creating physical files. The above approach is nice because the flow document and the images only exist in memory. I'm sure there must be other ways to accomplish this, but WPF4 is still so new!

Thanks again for your rapid response and help!

Larry

Hi Larry,

I’m glad to know that you have found a workaround at the moment. However, the approach I shared earlier using Aspose.Pdf and Aspose.Pdf.Kit can also allow you to save the intermediate documents as memory streams instead of saving to physical files; you might give it a try.

If you have any further questions or concerns, please do let us know.
Regards,

Hi Shahzad,

I'm getting back to this issue - been working on other things, and thought I'd try the Aspose technique. Two problems.

1. I added the Aspose.pdf.dll to my project (it wasn't there before) so I could convert the memory stream to a stream for printing. However, when I try to compile I get the following error:

The referenced assembly "Aspose.Pdf" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.

I'm not sure what that means. I'm not getting it for Aspose.pdf.kit.dll.

2. This is procedural. Once I get the pdf.dll to work, is this the right approach?

//create PdfViewer object

PdfViewer viewer = new PdfViewer();

//reset the memory stream

ImageStream.Seek(0, SeekOrigin.Begin);

//Instantiate Pdf instance by calling its empty constructor

Pdf p = new Pdf();

//Add a section into the pdf document

Aspose.Pdf.Section sec = p.Sections.Add();

//Create an image object in the section

Aspose.Pdf.Image imageht = new Aspose.Pdf.Image(sec);

//Set the type of image using ImageFileType enumeration

imageht.ImageInfo.ImageFileType = ImageFileType.Tiff;

// Specify the image source as MemoryStream

imageht.ImageInfo.ImageStream = ImageStream;

//Add image object into the Paragraphs collection of the section

sec.Paragraphs.Add(imageht);

//Save the Pdf

MemoryStream ms = new MemoryStream();

p.Save(ms);

// Close the MemoryStream Object

ms.Close();

viewer.OpenPdfFile(ms);

THANKS!!!!

I found the latest Aspose.pdf 4.7 and downloaded/installed it. Now when I compile I get:

The referenced assembly "Aspose.Pdf, Version=4.7.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.

Hi Larry,

I have asked my colleague from Aspose.Pdf team to look into this issue; you’ll be updated with the results the earliest possible.

We’re sorry for the inconvenience.
Regards,

Hello Larry,

Thanks for sharing the information.

I am representative from Aspose.Pdf team. Can you please share some details regarding your working environment i.e. Visual Studio version, .NET Framework version etc.

As far as I can understand, the issue is occurring because you are including the .dll file of different target platform in your project as compare to the one installed over your system. For details regarding appropriate .dll version for specific platform, please take a look over readme.txt inside [$Installdir]\Aspose\Aspose.Pdf for .NET\Bin (where Installdir is the directory where Aspose.Pdf for .NET is installed)

We apologize for your inconvenience.

Sorry for not getting back to you sooner - for some reason I'm not getting notified of updates.

I'm using VS2010, C#.net 4.0.

My real interest is printing as noted above. If I don't have to use Aspose.Pdf then I won't, but I have a feeling I'll need to. That's where the other problem comes in with Aspose.Pdf.dll is not correct for my environment for some reason. Both of my Aspose versions - Aspose.Pdf.Kit.dll and Aspose.Pdf.dll are version 4.7.

Thanks!

Hello Larry,

Thanks for sharing the information.

As far as I can understand, you are using VS2010 and have .NET framework v4.0, I would recommend you to use aspose.pdf.dll present inside net4.0 or net4.0_ClientProfile folder present at [$Installdir]\Aspose\Aspose.Pdf for .NET\Bin (where Installdir is the directory where Aspose.Pdf for .NET is installed). In case you still face any problem or you have any further query, please feel free to contact. We apologize for your inconvenience.

You may also try using the latest release version of Aspose.Pdf for .NET 4.8.0 available over this link.