将字节文档放入字符串中,然后在桌面中注册



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

它可以在.rtf,.doc(and docx)或pdf中。

该文档以BLOB格式存储在数据库中,在另一个领域,我以Varchar2格式获得了扩展名,并且我以字节格式在Winform上以字节格式获得。

这些文档可以是PDF,RTF,DOC或DOCX。然后,当我从数据库中取出它们时,它们是byte[],我知道它们的格式,其中包含.pdf,.rtf等的DB字段...

并打印它们,我想以实际格式在桌子中注册它们(以 Extension属性存储在db中。然后,当它们在我的桌子中时,我想打印它们。

所以,当我带有扩展名.pdf的DB中有一个斑点时,我想在桌子上注册此文档,并打印它。

我用RTF成功地做到了,但是与其他人一起,我总是在此代码上获得``ncorrect格式'':

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

所以,看来我必须使用一些如果/其他方式处理所有格式。

但我找不到注册我的字节,将其打开并为doc和pdf打印的任何方法。

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

谢谢。

将文档保存到磁盘所需的一切是:

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

其中" my.pdf" - 带有正确扩展名的文件名和 bytes是您从数据库中检索的字节数组。

要打印您的文件,您可以查看本文提出此代码:

//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);

您可以使用path.getTemppath获取临时目录。使用

将您的文件保存在那儿
string path = Path.Combine(Path.GetTempPath, string.Format("dummy{0}", extension));
File.WriteAllBytes(path, bytes);

然后用文字打开它并进一步处理。

用于打印PDF,请参阅此问题。

相关内容

最新更新