将字节文档放入字符串中,然后在桌面中注册 [英] Put a byte document into a string, then register it in the desktop

查看:99
本文介绍了将字节文档放入字符串中,然后在桌面中注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个winform,必须搜索一些文档才能打印.

可以是.rtf,.doc(和docx)或pdf.

文档以BLOB格式存储在数据库中,在另一个字段中,我以varchar2格式获得扩展名,而在Winform上以字节格式获得它们.

这些文档可以是pdf,rtf,doc或docx.然后,当我从数据库中获取它们时,它们是byte[],并且我知道它们的格式以及DB中包含.pdf,.rtf等...

的字段.

要打印它们,我想以它们的实际格式(在DB中存储的Extension属性已知)在我的办公桌上注册它们.然后,当它们在我的办公桌上时,我要打印它们. /p>

因此,当我在数据库中使用扩展名为.pdf的Blob时,我想将此文档以pdf格式注册在我的办公桌上,并打印出来.

我成功使用rtf进行了此操作,但是与其他人一起,我始终在此代码上遇到格式错误"错误:

fichierSortie = new FileStream(fullPath + echange.Extension, FileMode.Create); ;

enregistreurFichier = new StreamWriter(fichierSortie);
                    string pmessage = "";

                byte[] text = echange.DocEchange;
                using (var file = new MemoryStream(text))
                using (var reader = new StreamReader(file))
                {
                    reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    while (!reader.EndOfStream)
                    {
                        pmessage += reader.ReadLine();
                    }
                }

enregistreurFichier.Write(pmessage);

                    enregistreurFichier.Close();
                    fichierSortie.Close();

印刷部分:

 Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                wordApp.Visible = true;

                PrinterSettings settings = new PrinterSettings();
                foreach (string printer in PrinterSettings.InstalledPrinters)
                {
                    settings.PrinterName = printer;
                    if (settings.IsDefaultPrinter)
                    {
                        settings.Duplex = Duplex.Simplex;
                    }
                }

                wordApp.Documents.Open(fullPath + echange.Extension); //for VS 2008 and earlier - just give missing for all the args

                wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                wordApp.ActiveDocument.PrintOut(false); //as before - missing for remaining args, if using VS 2008 and earlier

                wordApp.Quit(WdSaveOptions.wdDoNotSaveChanges); //ditto

所以,看来我必须使用if/else来处理所有格式.

但是我找不到任何方法来注册我的字节,打开它并为doc和pdf打印它.

有人可以帮我解决那些问题吗?

谢谢.

解决方案

将文档保存到磁盘所需要做的就是:

System.IO.File.WriteAllBytes("my.pdf", bytes);

其中"my.pdf"-具有正确扩展名的文件名,bytes是从数据库中检索到的字节数组.

要打印文件,您可以查看以下

So, I have a winform that must search some document to print.

It can be in .rtf, .doc(and docx) or pdf.

The document are stored in a dataBase in BLOB format, and in another field, I got the extension in varchar2 format, and I get them in byte format on my winform.

These documents can be pdf, rtf, doc or docx. Then, when I take them from the DataBase, they are byte[], and I know their format with a field in the DB that contains .pdf, .rtf etc...

And to print them, I want to register them in my desk, in their actual format (known with Extension properties stored in the DB. And then, when they are in my desk, I want to print them.

So, when I got a Blob in DB with Extension .pdf , I want to register this documents as a pdf in my desk, and print it.

I sucessfully do it with rtf, but with the others, I always got ìncorrect format`errors on this code :

fichierSortie = new FileStream(fullPath + echange.Extension, FileMode.Create); ;

enregistreurFichier = new StreamWriter(fichierSortie);
                    string pmessage = "";

                byte[] text = echange.DocEchange;
                using (var file = new MemoryStream(text))
                using (var reader = new StreamReader(file))
                {
                    reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    while (!reader.EndOfStream)
                    {
                        pmessage += reader.ReadLine();
                    }
                }

enregistreurFichier.Write(pmessage);

                    enregistreurFichier.Close();
                    fichierSortie.Close();

ANd the printing part :

 Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                wordApp.Visible = true;

                PrinterSettings settings = new PrinterSettings();
                foreach (string printer in PrinterSettings.InstalledPrinters)
                {
                    settings.PrinterName = printer;
                    if (settings.IsDefaultPrinter)
                    {
                        settings.Duplex = Duplex.Simplex;
                    }
                }

                wordApp.Documents.Open(fullPath + echange.Extension); //for VS 2008 and earlier - just give missing for all the args

                wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                wordApp.ActiveDocument.PrintOut(false); //as before - missing for remaining args, if using VS 2008 and earlier

                wordApp.Quit(WdSaveOptions.wdDoNotSaveChanges); //ditto

So, it seems I must use some if/else to deal with all format.

But I do not found any way to register my byte, open it and print it for doc and pdf.

Can somebody help me sove those problems?

Thank you.

解决方案

All you need to save your document to a disk is:

System.IO.File.WriteAllBytes("my.pdf", bytes);

Where "my.pdf" - file name with correct extension and bytes is your byte array retrieved from database.

To print your files you can look at this article which proposes this code:

//Using below code we can print any document
ProcessStartInfo info = new ProcessStartInfo(txtFileName.Text.Trim());
info.Verb = "Print";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);

这篇关于将字节文档放入字符串中,然后在桌面中注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆