Exemplo n.º 1
0
		internal Dictionary<string, PageSelection> CreateInterimPdfs(List<WorkbookItem> workbookItems, ProgressCallback progress)
		{
			Dictionary<string, PageSelection> interimPdfFiles = new Dictionary<string, PageSelection>();

			// Need to ensure the DocumentPublisher is disposed after the Pdfs are created otherwise
			// application instances are left in memory until the Combine exe closes. This can cause
			// issues with using Word automation while Combine exe is still open (see DE7973).
			using (DocumentPublisher documentPublisher = DocumentPublisher.Instance())
			{
				foreach (WorkbookItem wfm in workbookItems)
				{
					if (progress != null)
					{
						progress.OnProgressUpdate(wfm, CombineStage.BeforeConvert, true);
					}

					try
					{
						if (string.IsNullOrEmpty(wfm.Pdf))
						{
                            var encryptionManager = new Workshare.Policy.ContentEncryption.ContentEncryptionManager();
                            var decryptedFilePath=string.Empty;
                            var encryptResult = encryptionManager.DecryptFile(wfm.FileName, wfm.DocumentId,encryptionUI, out decryptedFilePath);
                            
                            switch (encryptResult)
                            {
                                case Policy.ContentEncryption.Interfaces.DecryptResult.Ok:
                                    {
                                        wfm.TempCopy = decryptedFilePath;
                                        CleanDocumentIfRequired(wfm);

                                        if (wfm.PageSelection == null)
                                        {
                                            wfm.Pdf = documentPublisher.Print(wfm);
                                        }
                                        else
                                        {
                                            wfm.Pdf = documentPublisher.PrintRange(wfm, wfm.PageSelection);
                                        }
                                        break;
                                    }
                                case Policy.ContentEncryption.Interfaces.DecryptResult.Cancel: return new Dictionary<string, PageSelection>();
                                case Policy.ContentEncryption.Interfaces.DecryptResult.Skip: continue;
                            }
						}
                        else
                        {
                            if (Workshare.Pdf.Security.HasOpenPassword(wfm.Pdf) &&
                                !Utilities.RemovePassword(wfm))
                            {
                                continue; 
                            }

                            if (wfm.PageSelection != null) 
                            {
                                Workshare.Pdf.Edit.DeletePages(wfm.Pdf, Utilities.GetPageNumbersToDelete(wfm));
                            }
                        }

						// Powerpoint or Excel documents with no content generate zero byte Pdfs
						// which then cause exceptions in the TronReader.  Pick up zero byte pdfs
						// here and do not include them.
						FileInfo infoPDF = new FileInfo(wfm.Pdf);
						if (!infoPDF.Exists || infoPDF.Length <= 0)
						{
							if (progress != null)
							{
								progress.OnProgressUpdate(wfm, CombineStage.AfterConvert, false);
							}

							string message = wfm.DisplayName + " is an empty document.\n\nDo you wish to continue?";
							if (WsMessage.ShowMessage(IntPtr.Zero, message, MessageButtons.WsYesNo, MessageBranding.WsDefault) == MessageResult.WsResultNo)
							{
                                return GetEmptyResult();
							}
							continue;
						}

						interimPdfFiles.Add(wfm.Pdf, wfm.PageSelection);
					}
					catch (Exception ex)
					{
						if (progress != null)
						{
							progress.OnProgressUpdate(wfm, CombineStage.AfterConvert, false);
						}

						// Use the filename part of DocumentId, but if empty then use DisplayName.
						// Reason: Depending on the file format and method of loading, the DisplayName may *not*
						// include the file extension. (...or even match the file name at all!)
						string sFilename;
                        if (string.IsNullOrEmpty(wfm.DocumentId))
                        {
                            sFilename = wfm.DisplayName;
                        }
                        else
                        {
                            sFilename = System.IO.Path.GetFileName(wfm.DocumentId);
                        }

						string message = "Failed to convert " + sFilename + "\nThis document will be skipped.\n\nDo you wish to continue?";

						Workshare.Interop.Logging.Logger.LogError(message);
						Workshare.Interop.Logging.Logger.LogError(ex);

						if (WsMessage.ShowMessage(IntPtr.Zero, message, MessageButtons.WsYesNo, MessageBranding.WsDefault) == MessageResult.WsResultNo)
						{
                            return GetEmptyResult();
						}
						continue;
					}

					if (progress != null)
					{
						progress.OnProgressUpdate(wfm, CombineStage.AfterConvert, true);
					}
				}
			}

			return interimPdfFiles;
		}