コード例 #1
0
        /// <summary>
        /// Add PowerPoint presentations to the conversion queue
        /// </summary>
        /// <param name="files">Path to files to be added</param>
        /// <returns></returns>
        private async Task AddFilesAsync(string[] files)
        {
            if (firstAdd)   // remove "drag & drop" placeholder
            {
                listboxFiles.Items.Clear();
                firstAdd = false;
            }

            // add presentations
            foreach (string s in files)
            {
                OfficeFileInfo file = null;
                await Task.Run(() => {
                    try {
                        if (s.Contains(".ppt"))
                        {
                            file = new PptInfo(s);
                        }
                        else if (s.Contains(".doc"))
                        {
                            file = new DocInfo(s);
                        }
                    }
                    catch (PathTooLongException ex) {
                        ex.Data.Add("Path", s);
                        throw;
                    }
                    catch (UnauthorizedAccessException ex) {
                        ex.Data.Add("Path", s);
                        throw;
                    }
                });

                if (file != null)
                {
                    listboxFiles.Items.Add(file);
                }
            }

            // create path to default output folder
            if (listboxFiles.Items.Count > 0)   // were files added?
            // if not use default
            {
                string path = Path.GetDirectoryName((listboxFiles.Items[0] as OfficeFileInfo).Path);
                string dest = Path.Combine(new[] { path, "PDF-Conversions" });
                txtDestDir.Text = dest;

                // show listbox file count
                lblProgress.Content = string.Format("{0} documents(s).", listboxFiles.Items.Count);
            }
        }
コード例 #2
0
        /// <summary>
        /// Converts docs to PDF documents asynchronously
        /// </summary>
        /// <param name="ppApp">The Word application</param>
        /// <param name="info">A DocInfo object of the file to be converted to pdf</param>
        /// <param name="newPath">Full path of the converted file</param>
        private Task wordConvertAsync(Word.Application wordApp, DocInfo info, string destDir)
        {
            return(Task.Run(() => {
                // open doc in Word in the bg
                Word.Document doc = wordApp.Documents.Open(info.Path, AddToRecentFiles: false, Visible: false);

                // build path for converted file
                string newPath = Path.Combine(new[] { destDir, Path.ChangeExtension(Path.GetFileName(info.Path), "pdf") });

                // save as pdf
                doc.SaveAs2(newPath, Word.WdSaveFormat.wdFormatPDF);
                ((Word._Document)doc).Close(Word.WdSaveOptions.wdDoNotSaveChanges);
                Util.ReleaseComObject(doc);
            }));
        }