Exemplo n.º 1
0
        public static void SaveAsWord(List <Bitmap> images)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Filter     = "Word File | *.doc";
                dialog.DefaultExt = "doc";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Microsoft.Office.Interop.Word.Application app;
                    Microsoft.Office.Interop.Word.Document    doc;
                    object miss = System.Reflection.Missing.Value;

                    app         = new Microsoft.Office.Interop.Word.Application();
                    app.Visible = false;

                    doc = app.Documents.Add(ref miss, ref miss, ref miss, ref miss);

                    foreach (var img in images)
                    {
                        object start = doc.Content.End - 1;
                        object end   = doc.Content.End;
                        Microsoft.Office.Interop.Word.Range rng = doc.Range(ref start, ref end);
                        Clipboard.SetDataObject(img, true);
                        rng.Paste();
                    }

                    doc.SaveAs2(dialog.FileName);

                    doc.Close(ref miss, ref miss, ref miss);
                    app.Quit(ref miss, ref miss, ref miss);
                }
            }
        }
Exemplo n.º 2
0
        public async void NapravleniyaMed(List <PersonShortViewModel> persons)
        {
            try
            {
                // await new ProgressRunner().RunAsync(doAction);
                await new ProgressRunner().RunAsync(
                    new Action <ProgressHandler>((progressChanged) =>
                {
                    progressChanged(1, "Формирование направлений");

                    var path = (string.Format("{0}\\_шаблоны\\" + "Направление мед.dotx", AppDomain.CurrentDomain.BaseDirectory));
                    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application {
                        Visible = false
                    };
                    Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Add(path /*, ReadOnly: false, Visible: true*/);
                    aDoc.Activate();

                    object missing = Missing.Value;
                    object what    = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;
                    object which   = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToLast;


                    Microsoft.Office.Interop.Word.Range baseRange = aDoc.Content;
                    baseRange.Cut();

                    Microsoft.Office.Interop.Word.Range range = null;

                    progressChanged(5);
                    var share = 95.0 / persons.Count;
                    foreach (var p in persons)
                    {
                        if (range != null)    //!firstRow
                        {
                            range = aDoc.GoTo(ref what, ref which, ref missing, ref missing);
                            range.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
                        }

                        range = aDoc.GoTo(ref what, ref which, ref missing, ref missing);

                        range.Paste();

                        range.Find.ClearFormatting();
                        range.Find.Execute(FindText: "{fio}", ReplaceWith: p.Fio, Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll);
                        if (p.BirthDat.HasValue)
                        {
                            range.Find.Execute(FindText: "{birthDat}", ReplaceWith: p.BirthDat.Value.ToString("dd.MM.yyyy"), Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll);
                        }

                        progressChanged(share);
                    }
                    // string otchetDir = @"C:\_provodnikFTP";
                    //  aDoc.SaveAs(FileName: otchetDir + @"\Согласие на обработку персональных данных.DOCX");
                    wordApp.Visible = true;
                })


                    );
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        static void Main(string[] args)
        {
            // Create new barcode
            Barcode barcode = new Barcode();

            // Set symbology
            barcode.Symbology = SymbologyType.Codabar;
            // Set value
            barcode.Value = "123456";

            // Add checksum to barcode
            barcode.AddChecksum = true;

            // Create word instance
            Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();

            // Hide word
            appWord.Visible = false;

            // Create missing object
            object mis = System.Reflection.Missing.Value;
            // Template file - change to the appropraite path where you copied input.doc
            object fileInput = @"C:\input.doc";
            // Template file - change to the appropraite path where you copied input.doc
            object fileOutput = @"C:\output.doc";

            // Open document
            Microsoft.Office.Interop.Word.Document docWord = appWord.Documents.Open(ref fileInput, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis);

            // Set bookmark name
            object bookmarkName = "MyBookmark";

            // Get bookmark location
            Microsoft.Office.Interop.Word.Range bookmarkLocation = docWord.Bookmarks.get_Item(ref bookmarkName).Range;

            // Get barcode image
            Bitmap image = (Bitmap)barcode.GetImage();

            // Copy image to the clipboard
            Clipboard.SetDataObject(image);

            // Paste barcode image to the document
            bookmarkLocation.Paste();

            // Save to the new document
            docWord.SaveAs(ref fileOutput, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis);

            // We needn't save changes
            object saveChanges = false;

            // Close word application
            appWord.Quit(ref saveChanges, ref mis, ref mis);

            // Release COM object
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord);
        }