0

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
provençal le breton
  • 1,428
  • 4
  • 26
  • 43
  • "The document are stored in a dataBase in BLOB format" - okay, so they're stored as binary data... but what's the actual format? Are they *just* plain text? If so, in what encoding? It's very unclear what you're trying to do. – Jon Skeet Jan 21 '14 at 08:35
  • Wait... Am I misunderstanding this or are you using strings and `ReadLine()` to store/read binary data? – BambooleanLogic Jan 21 '14 at 08:56
  • @Smallhacker Well, it's convert into MemoryStream then StreamReader. Is it wrong to code this? – provençal le breton Jan 21 '14 at 08:59
  • @JonSkeet I update the start of my question : is that more clear? In my DB, I stored a document (can be rtf, pdf, doc, docx) as BLOB and in another db field, its extension (varchar2 : .rtf, .pdf etc). – – provençal le breton Jan 21 '14 at 08:59
  • Yes, that's clearer - and basically, you shouldn't use `StreamReader` for content when may not actually be plain text. (A Word document, for example.) – Jon Skeet Jan 21 '14 at 09:30
  • @JonSkeet Ok. Until now I was just testing .rtf documents,so that's why I had'nt errors yet. – provençal le breton Jan 21 '14 at 10:31

2 Answers2

2

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);
astef
  • 8,575
  • 4
  • 56
  • 95
1

You can use Path.GetTempPath to get the TEMP directory. Save your file over there using

string path = Path.Combine(Path.GetTempPath, string.Format("dummy{0}", extension));

File.WriteAllBytes(path, bytes);

Then open it in Word and handle it further.

For printing PDF see this question on SO.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325