Beispiel #1
0
        private void CreatePrettyPdf(string watermark = "", bool withAttachments = false)
        {
            try
            {
                List <Tuple <string, string> > selectedDocs = new List <Tuple <string, string> >();

                if (withAttachments)
                {
                    //get available documents
                    List <Tuple <string, string> > attachmentOptions = XLDocument.GetAttachmentFiles();
                    //ask user to check which ones they want
                    XLForms.Attachments attachmentsForm = new XLForms.Attachments(attachmentOptions);
                    attachmentsForm.ShowDialog();
                    selectedDocs = attachmentsForm.selectedDocuments;
                }

                //get the id for later use and before the original is closed.
                string fileID = XLDocument.GetFileID();
                //save the document as a pdf and get location
                string file     = XLDocument.CreatePdf();
                string fileName = XLDocument.currentDoc.Name.Substring(0, XLDocument.currentDoc.Name.LastIndexOf('.'));
                //close the original, it isn't required any more
                XLDocument.EndDocument();
                //add the header and get the location of the new combined file
                file = XLDocument.AddHeadertoPDF(file, watermark, fileName);
                //merge the attachments, if any selected
                if (selectedDocs.Count != 0)
                {
                    List <string> docs = new List <string>();
                    docs.Add(file);
                    foreach (Tuple <string, string> tuple in selectedDocs)
                    {
                        docs.Add(tuple.Item2);
                    }
                    file = XLDocument.AddAttachments(docs);
                }
                if (String.IsNullOrEmpty(fileID))
                {
                    string         finalLocation = "";
                    SaveFileDialog sDialog       = new SaveFileDialog();
                    sDialog.FileName   = "NewPdf.pdf";
                    sDialog.DefaultExt = ".pdf";
                    sDialog.Title      = "Save PDF as...";
                    if (sDialog.ShowDialog() == DialogResult.OK)
                    {
                        finalLocation = sDialog.FileName;
                    }

                    System.IO.File.Copy(file, finalLocation);
                }
                else
                {
                    //index the combined file using the data from the original
                    XLDocument.IndexPDFCopy(file, fileID);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cancelled PDF Creation");
                XLtools.LogException("Word - PDFCreation", ex.Message);
            }
        }