public void Generate(BasePreviewContainer previewContainer, CancellationToken cancellationToken) { var excelContainer = (ExcelPreviewContainer)previewContainer; var txtDestination = Path.Combine(excelContainer.ContainerPath, PreviewFormats.Text); var updateTxt = !(Directory.Exists(txtDestination) && Directory.GetFiles(txtDestination).Any()); if (!Directory.Exists(txtDestination)) Directory.CreateDirectory(txtDestination); var updated = updateTxt; if (updated) { try { if (ExcelHelper.Instance.Connect()) { MessageFilter.Register(); var workbook = ExcelHelper.Instance.ExcelObject.Workbooks.Open(excelContainer.SourcePath, ReadOnly: true); string txtFileName = Path.Combine(txtDestination, Path.ChangeExtension(Path.GetFileName(excelContainer.SourcePath), "txt")); workbook.SaveAs(txtFileName, XlFileFormat.xlTextWindows); workbook.Close(); Utils.ReleaseComObject(workbook); } } catch { } finally { MessageFilter.Revoke(); ExcelHelper.Instance.Disconnect(); } } if (updated) previewContainer.MarkAsModified(); }
public void Generate(BasePreviewContainer previewContainer, CancellationToken cancellationToken) { var updated = false; FFMpegData videoData = null; if (!cancellationToken.IsCancellationRequested) { var infoDestination = Path.Combine(previewContainer.ContainerPath, PreviewFormats.VideoInfo); var updateInfo = !(Directory.Exists(infoDestination) && Directory.GetFiles(infoDestination).Any()); if (!Directory.Exists(infoDestination)) Directory.CreateDirectory(infoDestination); if (updateInfo) VideoHelper.ExtractVideoInfo(previewContainer.SourcePath, infoDestination, cancellationToken); videoData = ((VideoPreviewContainer)previewContainer).GetVideoData(); updated |= updateInfo; } if (!cancellationToken.IsCancellationRequested && videoData != null && !((VideoPreviewContainer)previewContainer).IsMp4Converted) { var mp4Destination = Path.Combine(previewContainer.ContainerPath, PreviewFormats.VideoMp4); var updateMp4 = !(Directory.Exists(mp4Destination) && Directory.GetFiles(mp4Destination).Any()); if (!Directory.Exists(mp4Destination)) Directory.CreateDirectory(mp4Destination); if (updateMp4) VideoHelper.ExportMp4(previewContainer.SourcePath, mp4Destination, videoData, cancellationToken); updated |= updateMp4; } if (!cancellationToken.IsCancellationRequested && videoData != null) { var sourceFile = !((VideoPreviewContainer)previewContainer).IsMp4Converted ? Path.Combine(previewContainer.ContainerPath, PreviewFormats.VideoMp4, Path.ChangeExtension(Path.GetFileName(previewContainer.SourcePath), ".mp4")) : previewContainer.SourcePath; var thumbDestination = Path.Combine(previewContainer.ContainerPath, PreviewFormats.VideoThumbnail); var updateThumbs = !(Directory.Exists(thumbDestination) && Directory.GetFiles(thumbDestination).Any()); if (!Directory.Exists(thumbDestination)) Directory.CreateDirectory(thumbDestination); if (updateThumbs) { VideoHelper.GenerateThumbnails(sourceFile, thumbDestination, videoData, cancellationToken); PngHelper.ConvertFiles(thumbDestination); } updated |= updateThumbs; } if (updated) previewContainer.MarkAsModified(); }
public void Generate(BasePreviewContainer previewContainer, CancellationToken cancellationToken) { var updated = false; var pdfContainer = (PdfPreviewContainer)previewContainer; var pngDestination = Path.Combine(pdfContainer.ContainerPath, PreviewFormats.Png); var updatePng = !(Directory.Exists(pngDestination) && Directory.GetFiles(pngDestination).Any()) && pdfContainer.GenerateImages; if (updatePng && !Directory.Exists(pngDestination)) Directory.CreateDirectory(pngDestination); var pngPhoneDestination = Path.Combine(pdfContainer.ContainerPath, PreviewFormats.PngForMobile); var updatePngPhone = !(Directory.Exists(pngPhoneDestination) && Directory.GetFiles(pngPhoneDestination).Any()) && pdfContainer.GenerateImages; if (updatePngPhone && !Directory.Exists(pngPhoneDestination)) Directory.CreateDirectory(pngPhoneDestination); var thumbsDestination = Path.Combine(pdfContainer.ContainerPath, PreviewFormats.Thumbnails); var updateThumbs = !(Directory.Exists(thumbsDestination) && Directory.GetFiles(thumbsDestination).Any()) && pdfContainer.GenerateImages; if (updateThumbs && !Directory.Exists(thumbsDestination)) Directory.CreateDirectory(thumbsDestination); var thumbsPhoneDestination = Path.Combine(pdfContainer.ContainerPath, PreviewFormats.ThumbnailsForMobile); var updateThumbsPhone = !(Directory.Exists(thumbsPhoneDestination) && Directory.GetFiles(thumbsPhoneDestination).Any()) && pdfContainer.GenerateImages; if (updateThumbsPhone && !Directory.Exists(thumbsPhoneDestination)) Directory.CreateDirectory(thumbsPhoneDestination); if (updatePng || updateThumbs) PdfHelper.ExportPdf(pdfContainer.SourcePath, pngDestination, thumbsDestination); if (updatePngPhone || updateThumbsPhone) PdfHelper.ExportPdfPhone(pdfContainer.SourcePath, pngPhoneDestination,thumbsPhoneDestination); var txtDestination = Path.Combine(pdfContainer.ContainerPath, PreviewFormats.Text); var updateTxt = !(Directory.Exists(txtDestination) && Directory.GetFiles(txtDestination).Any()) && pdfContainer.GenerateText; if (updateTxt && !Directory.Exists(txtDestination)) Directory.CreateDirectory(txtDestination); if (updateTxt) PdfHelper.ExtractText(pdfContainer.SourcePath, Path.Combine(txtDestination, Path.ChangeExtension(Path.GetFileName(pdfContainer.SourcePath), "txt"))); updated = updatePng || updateThumbs || updatePngPhone || updateThumbsPhone || updateTxt; if (updated) { PngHelper.ConvertFiles(pdfContainer.ContainerPath); previewContainer.MarkAsModified(); } }
public void Generate(BasePreviewContainer previewContainer, CancellationToken cancellationToken) { var wordContainer = (WordPreviewContainer)previewContainer; var pdfDestination = Path.Combine(wordContainer.ContainerPath, PreviewFormats.Pdf); var updatePdf = !(Directory.Exists(pdfDestination) && Directory.GetFiles(pdfDestination).Any()); if (updatePdf && !Directory.Exists(pdfDestination)) Directory.CreateDirectory(pdfDestination); var pngDestination = Path.Combine(wordContainer.ContainerPath, PreviewFormats.Png); var updatePng = !(Directory.Exists(pngDestination) && Directory.GetFiles(pngDestination).Any()) && wordContainer.GenerateImages; if (updatePng && !Directory.Exists(pngDestination)) Directory.CreateDirectory(pngDestination); var pngPhoneDestination = Path.Combine(wordContainer.ContainerPath, PreviewFormats.PngForMobile); var updatePngPhone = !(Directory.Exists(pngPhoneDestination) && Directory.GetFiles(pngPhoneDestination).Any()) && wordContainer.GenerateImages; if (updatePngPhone && !Directory.Exists(pngPhoneDestination)) Directory.CreateDirectory(pngPhoneDestination); var thumbsDestination = Path.Combine(wordContainer.ContainerPath, PreviewFormats.Thumbnails); var updateThumbs = !(Directory.Exists(thumbsDestination) && Directory.GetFiles(thumbsDestination).Any()) && wordContainer.GenerateImages; if (updateThumbs && !Directory.Exists(thumbsDestination)) Directory.CreateDirectory(thumbsDestination); var thumbsPhoneDestination = Path.Combine(wordContainer.ContainerPath, PreviewFormats.ThumbnailsForMobile); var updateThumbsPhone = !(Directory.Exists(thumbsPhoneDestination) && Directory.GetFiles(thumbsPhoneDestination).Any()) && wordContainer.GenerateImages; if (updateThumbsPhone && !Directory.Exists(thumbsPhoneDestination)) Directory.CreateDirectory(thumbsPhoneDestination); var docxDestination = Path.Combine(wordContainer.ContainerPath, PreviewFormats.Word); var updateDocx = !(Directory.Exists(docxDestination) && Directory.GetFiles(docxDestination).Any()); if (updateDocx && !Directory.Exists(docxDestination)) Directory.CreateDirectory(docxDestination); var txtDestination = Path.Combine(wordContainer.ContainerPath, PreviewFormats.Text); var updateTxt = !(Directory.Exists(txtDestination) && Directory.GetFiles(txtDestination).Any()) && wordContainer.GenerateText; if (updateTxt && !Directory.Exists(txtDestination)) Directory.CreateDirectory(txtDestination); var needToUpdate = updatePdf || updatePng || updateThumbs || updateDocx || updateTxt || updatePngPhone || updateThumbsPhone; if (!needToUpdate) return; var updated = false; var tryCount = 0; do { using (var wordProcessor = new WordHidden()) { try { if (!wordProcessor.Connect()) continue; MessageFilter.Register(); var sourceFileName = Path.GetFileName(wordContainer.SourcePath); var sourceFilePath = wordContainer.SourcePath; var document = wordProcessor.WordObject.Documents.Open(sourceFilePath); document.Final = false; var pdfFileName = Path.Combine(pdfDestination, Path.ChangeExtension(sourceFileName, "pdf")); if (updatePdf) { document.ExportAsFixedFormat(pdfFileName, WdExportFormat.wdExportFormatPDF); } if (updatePng || updateThumbs) PdfHelper.ExportPdf(pdfFileName, pngDestination, thumbsDestination); if (updatePngPhone || updateThumbsPhone) PdfHelper.ExportPdfPhone(pdfFileName, pngPhoneDestination, thumbsPhoneDestination); if (updateTxt) { var txtFileName = Path.Combine(txtDestination, Path.ChangeExtension(sourceFileName, "txt")); using (var sw = new StreamWriter(txtFileName, false)) { sw.Write(document.Content.Text); sw.Flush(); } } if (updateDocx) { var documentSlitted = false; wordProcessor.WordObject.Browser.Target = WdBrowseTarget.wdBrowsePage; var pageCount = document.ComputeStatistics(WdStatistic.wdStatisticPages); try { for (var i = 1; i <= pageCount; i++) { document.Bookmarks["\\page"].Range.Copy(); var singlePageDocument = wordProcessor.WordObject.Documents.Add(); singlePageDocument.Activate(); wordProcessor.WordObject.Selection.Paste(); wordProcessor.WordObject.Selection.TypeBackspace(); singlePageDocument.SaveAs(Path.Combine(docxDestination, string.Format("Page{0}.{1}", i, "docx")), WdSaveFormat.wdFormatXMLDocument); singlePageDocument.Close(); Utils.ReleaseComObject(singlePageDocument); document.Activate(); wordProcessor.WordObject.Browser.Next(); } documentSlitted = true; } catch { } if (!documentSlitted) for (var i = 1; i <= pageCount; i++) document.SaveAs(Path.Combine(docxDestination, string.Format("Page{0}.{1}", i, "docx")), WdSaveFormat.wdFormatXMLDocument); } document.Close(false); Utils.ReleaseComObject(document); updated = true; } catch { } finally { tryCount++; MessageFilter.Revoke(); } } } while (!updated && tryCount < 10); if (needToUpdate) { PngHelper.ConvertFiles(wordContainer.ContainerPath); previewContainer.MarkAsModified(); } }
public void Generate(BasePreviewContainer previewContainer, CancellationToken cancellationToken) { var powerPointContainer = (PowerPointPreviewContainer)previewContainer; var updated = false; var tryCount = 0; do { var pdfDestination = Path.Combine(powerPointContainer.ContainerPath, PreviewFormats.Pdf); var updatePdf = !(Directory.Exists(pdfDestination) && Directory.GetFiles(pdfDestination).Any()); if (updatePdf && !Directory.Exists(pdfDestination)) Directory.CreateDirectory(pdfDestination); var pngDestination = Path.Combine(powerPointContainer.ContainerPath, PreviewFormats.Png); var updatePng = !(Directory.Exists(pngDestination) && Directory.GetFiles(pngDestination).Any()) && powerPointContainer.GenerateImages; if (updatePng && !Directory.Exists(pngDestination)) Directory.CreateDirectory(pngDestination); var pngPhoneDestination = Path.Combine(powerPointContainer.ContainerPath, PreviewFormats.PngForMobile); var updatePngPhone = !(Directory.Exists(pngPhoneDestination) && Directory.GetFiles(pngPhoneDestination).Any()) && powerPointContainer.GenerateImages; if (updatePngPhone && !Directory.Exists(pngPhoneDestination)) Directory.CreateDirectory(pngPhoneDestination); var thumbDestination = Path.Combine(powerPointContainer.ContainerPath, PreviewFormats.Thumbnails); var updateThumbs = !(Directory.Exists(thumbDestination) && Directory.GetFiles(thumbDestination).Any()) && powerPointContainer.GenerateImages; if (updateThumbs && !Directory.Exists(thumbDestination)) Directory.CreateDirectory(thumbDestination); var thumbsPhoneDestination = Path.Combine(powerPointContainer.ContainerPath, PreviewFormats.ThumbnailsForMobile); var updateThumbsPhone = !(Directory.Exists(thumbsPhoneDestination) && Directory.GetFiles(thumbsPhoneDestination, "*.png").Length > 0) && powerPointContainer.GenerateImages; if (updateThumbsPhone && !Directory.Exists(thumbsPhoneDestination)) Directory.CreateDirectory(thumbsPhoneDestination); var pptxDestination = Path.Combine(powerPointContainer.ContainerPath, PreviewFormats.PowerPoint); var updatePptx = !(Directory.Exists(pptxDestination) && Directory.GetFiles(pptxDestination).Any()); if (updatePptx && !Directory.Exists(pptxDestination)) Directory.CreateDirectory(pptxDestination); var txtDestination = Path.Combine(powerPointContainer.ContainerPath, PreviewFormats.Text); var updateTxt = !(Directory.Exists(txtDestination) && Directory.GetFiles(txtDestination).Any()) && powerPointContainer.GenerateText; if (updateTxt && !Directory.Exists(txtDestination)) Directory.CreateDirectory(txtDestination); var needToUpdate = updatePdf || updatePng || updateThumbs || updatePptx || updateTxt || updatePngPhone || updateThumbsPhone; if (!needToUpdate) break; using (var powerPointProcessor = new PowerPointHidden()) { try { var processInteropped = false; if (!powerPointProcessor.Connect(true)) continue; MessageFilter.Register(); Presentation presentation = null; processInteropped = powerPointProcessor.DoTimeLimitedAction(() => { presentation = powerPointProcessor.PowerPointObject.Presentations.Open(previewContainer.SourcePath, WithWindow: MsoTriState.msoFalse); presentation.Final = false; }); if (processInteropped || presentation == null) continue; var content = new StringBuilder(); if (!cancellationToken.IsCancellationRequested && (updatePng || updateThumbs || updatePptx || updateTxt || updatePngPhone || updateThumbsPhone)) { var i = 1; var thumbHeight = (int)presentation.PageSetup.SlideHeight / 10; var thumbWidth = (int)presentation.PageSetup.SlideWidth / 10; var phoneHeight = (int)(presentation.PageSetup.SlideHeight / 1.5); var phoneWidth = (int)(presentation.PageSetup.SlideWidth / 1.5); var thumbPhoneHeight = (int)presentation.PageSetup.SlideHeight / 4; var thumbPhoneWidth = (int)presentation.PageSetup.SlideWidth / 4; foreach (Slide slide in presentation.Slides) { if (cancellationToken.IsCancellationRequested) break; if (updatePng) { processInteropped = powerPointProcessor.DoTimeLimitedAction(() => { slide.Export(Path.Combine(pngDestination, String.Format("Slide{0}.{1}", i, "png")), "PNG"); }); if (processInteropped) break; } if (updatePngPhone) { processInteropped = powerPointProcessor.DoTimeLimitedAction(() => { slide.Export(Path.Combine(pngPhoneDestination, String.Format("Slide{0}.{1}", i, "png")), "PNG", phoneWidth, phoneHeight); }); if (processInteropped) break; } if (updateThumbs) { processInteropped = powerPointProcessor.DoTimeLimitedAction(() => { slide.Export(Path.Combine(thumbDestination, String.Format("Slide{0}.{1}", i, "png")), "PNG", thumbWidth, thumbHeight); }); if (processInteropped) break; } if (updateThumbsPhone) { processInteropped = powerPointProcessor.DoTimeLimitedAction(() => { slide.Export(Path.Combine(thumbsPhoneDestination, String.Format("Slide{0}.{1}", i, "png")), "PNG", thumbPhoneWidth, thumbPhoneHeight); }); if (processInteropped) break; } if (updatePptx) { processInteropped = powerPointProcessor.DoTimeLimitedAction(() => { var singleSlidePresentation = powerPointProcessor.PowerPointObject.Presentations.Open(previewContainer.SourcePath, WithWindow: MsoTriState.msoFalse); var totalSlides = singleSlidePresentation.Slides.Count; for (int j = totalSlides; j >= 1; j--) if (j != i) singleSlidePresentation.Slides[j].Delete(); singleSlidePresentation.SaveCopyAs(Path.Combine(pptxDestination, String.Format("Slide{0}.{1}", i, "pptx"))); singleSlidePresentation.Close(); Utils.ReleaseComObject(singleSlidePresentation); }); if (processInteropped) break; } if (updateTxt) { processInteropped = powerPointProcessor.DoTimeLimitedAction(() => { foreach (var shape in slide.Shapes.OfType<Shape>().Where(shape => shape.HasTextFrame == MsoTriState.msoTrue)) content.AppendLine(shape.TextFrame.TextRange.Text.Trim()); }); if (processInteropped) break; } i++; } if (processInteropped) continue; } if (!cancellationToken.IsCancellationRequested && updateTxt) using (var sw = new StreamWriter(Path.Combine(txtDestination, Path.ChangeExtension(Path.GetFileName(powerPointContainer.SourcePath), "txt")), false)) { sw.Write(content.ToString()); sw.Flush(); } if (!cancellationToken.IsCancellationRequested && updatePdf) { processInteropped = powerPointProcessor.DoTimeLimitedAction(() => presentation.ExportAsFixedFormat( Path.Combine(pdfDestination, Path.ChangeExtension(Path.GetFileName(powerPointContainer.SourcePath), "pdf")), PpFixedFormatType.ppFixedFormatTypePDF)); if (processInteropped) continue; } presentation.Close(); Utils.ReleaseComObject(presentation); if (needToUpdate) { PngHelper.ConvertFiles(powerPointContainer.ContainerPath); previewContainer.MarkAsModified(); } updated = true; } catch { } finally { tryCount++; MessageFilter.Revoke(); } } } while (!updated && tryCount < 10); }