public void ThenSymbolsAreEncodedProperly()
        {
            this.File.Position = 0;
            var doc = new Words.Document(this.File);

            Assert.IsTrue(doc.GetText().Contains("строка"), "Wrong conversion");
        }
Esempio n. 2
0
        public static void InsertTextInputFormField(string dataDir)
        {
            // ExStart:DocumentBuilderInsertTextInputFormField
            Words.Document  doc     = new Words.Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "Hello", 0);
            dataDir = dataDir + "DocumentBuilderInsertTextInputFormField_out.doc";
            doc.Save(dataDir);
            // ExEnd:DocumentBuilderInsertTextInputFormField
            Console.WriteLine("\nText input form field using DocumentBuilder inserted successfully into a document.\nFile saved at " + dataDir);
        }
        // This method is called every time an email is sent.
        private void Application_ItemSend(object Item, ref bool Cancel)
        {
            if (EnableAsposeWordsMetadata)
            {
                try
                {
                    Outlook.MailItem thisEmail   = Item as Outlook.MailItem;
                    bool             flgPassword = false;
                    for (int i = thisEmail.Attachments.Count; i > 0; i--)
                    {
                        Outlook.Attachment attachment = thisEmail.Attachments[i];

                        // Save the attachment in the temp location.
                        int    attachmentIndex = attachment.Index;
                        string tempPath        = Path.GetTempPath();
                        string tempFileName    = tempPath + attachment.FileName;
                        attachment.SaveAsFile(tempFileName);

                        // Check the file format for Microsoft Word documents.
                        FileFormatInfo info           = FileFormatUtil.DetectFileFormat(tempFileName);
                        bool           wordAttachment = false;

                        switch (info.LoadFormat)
                        {
                        case LoadFormat.Doc:
                        case LoadFormat.Dot:
                        case LoadFormat.Docx:
                        case LoadFormat.Docm:
                        case LoadFormat.Dotx:
                        case LoadFormat.Dotm:
                        case LoadFormat.FlatOpc:
                        case LoadFormat.Rtf:
                        case LoadFormat.WordML:
                        case LoadFormat.Html:
                        case LoadFormat.Mhtml:
                        case LoadFormat.Odt:
                        case LoadFormat.Ott:
                        case LoadFormat.DocPreWord60:
                            wordAttachment = true;
                            break;
                        }

                        // If a Word Attachment is found:
                        if (wordAttachment)
                        {
                            try
                            {
                                Aspose.Words.Document doc = new Words.Document(tempFileName);

                                // Remove if there is any protection on the document.
                                ProtectionType protection = doc.ProtectionType;
                                if (protection != ProtectionType.NoProtection)
                                {
                                    doc.Unprotect();
                                }

                                // Remove all built-in and Custom Properties.
                                doc.CustomDocumentProperties.Clear();
                                doc.BuiltInDocumentProperties.Clear();

                                // Password will be removed if the document is password protected.
                                if (protection != ProtectionType.NoProtection)
                                {
                                    doc.Protect(protection);
                                }

                                // Save the file back to temp location.
                                doc.Save(tempFileName);

                                // Replace the original attachment.
                                thisEmail.Attachments.Remove(attachmentIndex);
                                thisEmail.Attachments.Add(tempFileName, missing, attachmentIndex, missing);
                            }
                            catch (Words.IncorrectPasswordException ex)
                            {
                                flgPassword = true;
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        if (File.Exists(tempFileName))
                        {
                            File.Delete(tempFileName);
                        }
                    }
                    if (flgPassword)
                    {
                        MessageBox.Show("Password protected documents cannot be processed for Metadata cleaning");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message, "Error");
                    Cancel = true;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Converts source file into target format, saves resulted file to out file.
        /// Returns null in case of multiple files (all saved into outFolder).
        /// </summary>
        /// <param name="sourceFile">Source slides file to proceed.</param>
        /// <param name="outFolder">Output folder.</param>
        /// <param name="format">Output format.</param>
        /// <returns>Result file path.</returns>
        public string Conversion(
            string sourceFile,
            string outFolder,
            SlidesConversionFormat format
            )
        {
            using (var presentation = new Presentation(sourceFile))
            {
                var fileName   = Path.GetFileNameWithoutExtension(sourceFile);
                var outOneFile = Path.Combine(outFolder, $"{fileName}.{format}");

                switch (format)
                {
                case SlidesConversionFormat.odp:
                case SlidesConversionFormat.otp:
                case SlidesConversionFormat.pptx:
                case SlidesConversionFormat.pptm:
                case SlidesConversionFormat.potx:
                case SlidesConversionFormat.ppt:
                case SlidesConversionFormat.pps:
                case SlidesConversionFormat.ppsm:
                case SlidesConversionFormat.pot:
                case SlidesConversionFormat.potm:
                case SlidesConversionFormat.pdf:
                case SlidesConversionFormat.xps:
                case SlidesConversionFormat.ppsx:
                case SlidesConversionFormat.tiff:
                case SlidesConversionFormat.html:
                case SlidesConversionFormat.swf:
                    var slidesFormat = format.ToString().ParseEnum <SaveFormat>();
                    presentation.Save(outOneFile, slidesFormat);
                    return(outOneFile);

                case SlidesConversionFormat.txt:
                    var lines = new List <string>();
                    foreach (var slide in presentation.Slides)
                    {
                        foreach (var shp in slide.Shapes)
                        {
                            if (shp is AutoShape ashp)
                            {
                                lines.Add(ashp.TextFrame.Text);
                            }
                        }

                        var notes = slide.NotesSlideManager.NotesSlide?.NotesTextFrame?.Text;

                        if (!string.IsNullOrEmpty(notes))
                        {
                            lines.Add(notes);
                        }
                    }
                    System.IO.File.WriteAllLines(outOneFile, lines);
                    return(outOneFile);

                case SlidesConversionFormat.doc:
                case SlidesConversionFormat.docx:
                    using (var stream = new MemoryStream())
                    {
                        presentation.Save(stream, SaveFormat.Html);
                        stream.Flush();
                        stream.Seek(0, SeekOrigin.Begin);

                        var doc = new Words.Document(stream);
                        switch (format)
                        {
                        case SlidesConversionFormat.doc:
                            doc.Save(outOneFile, Words.SaveFormat.Doc);
                            break;

                        case SlidesConversionFormat.docx:
                            doc.Save(outOneFile, Words.SaveFormat.Docx);
                            break;

                        default:
                            throw new ArgumentException($"Unknown format {format}");
                        }
                    }
                    return(outOneFile);

                case SlidesConversionFormat.bmp:
                case SlidesConversionFormat.jpeg:
                case SlidesConversionFormat.png:
                case SlidesConversionFormat.emf:
                case SlidesConversionFormat.wmf:
                case SlidesConversionFormat.gif:
                case SlidesConversionFormat.exif:
                case SlidesConversionFormat.ico:
                    ImageFormat GetImageFormat(SlidesConversionFormat f)
                    {
                        switch (format)
                        {
                        case SlidesConversionFormat.bmp:
                            return(ImageFormat.Bmp);

                        case SlidesConversionFormat.jpeg:
                            return(ImageFormat.Jpeg);

                        case SlidesConversionFormat.png:
                            return(ImageFormat.Png);

                        case SlidesConversionFormat.emf:
                            return(ImageFormat.Wmf);

                        case SlidesConversionFormat.wmf:
                            return(ImageFormat.Wmf);

                        case SlidesConversionFormat.gif:
                            return(ImageFormat.Gif);

                        case SlidesConversionFormat.exif:
                            return(ImageFormat.Emf);

                        case SlidesConversionFormat.ico:
                            return(ImageFormat.Icon);

                        default:
                            throw new ArgumentException($"Unknown format {format}");
                        }
                    }

                    ///var size = presentation.SlideSize.Size;

                    for (var i = 0; i < presentation.Slides.Count; i++)
                    {
                        var slide   = presentation.Slides[i];
                        var outFile = Path.Combine(outFolder, $"{i}.{format}");
                        using (var bitmap = slide.GetThumbnail(1, 1))                                // (new Size((int)size.Width, (int)size.Height)))
                            bitmap.Save(outFile, GetImageFormat(format));
                    }

                    return(null);

                case SlidesConversionFormat.svg:
                    var svgOptions = new SVGOptions
                    {
                        PicturesCompression = PicturesCompression.DocumentResolution
                    };

                    for (var i = 0; i < presentation.Slides.Count; i++)
                    {
                        var slide   = presentation.Slides[i];
                        var outFile = Path.Combine(outFolder, $"{i}.{format}");
                        using (var stream = new FileStream(outFile, FileMode.CreateNew))
                            slide.WriteAsSvg(stream, svgOptions);
                    }

                    return(null);

                default:
                    throw new ArgumentException($"Unknown format {format}");
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 将Office格式, PDF格式转换成HTML格式
        /// </summary>
        /// <param name="pathSrc"></param>
        /// <param name="pathDst"></param>
        /// <returns></returns>
        public static string ConvertToHtml(this string pathSrc, string pathDst = null)
        {
            if (!System.IO.File.Exists(pathSrc))
            {
                throw new Exception($"指定文件 {pathSrc} 不存在!");
            }

            var extfilename = System.IO.Path.GetExtension(pathSrc);

            if (string.IsNullOrWhiteSpace(pathDst))
            {
                pathDst = System.IO.Path.GetDirectoryName(pathSrc) + @"\Formated\" + System.IO.Path.GetFileName(pathSrc) + ".html";
            }

            switch (extfilename)
            {
            case ".doc":
            case ".docx":
                var docSrc = new Words.Document(pathSrc);
                docSrc.Save(pathDst, Words.SaveFormat.Html);
                break;

            case ".xls":
            case ".xlsx":
                Cells.Workbook wb = new Cells.Workbook(pathSrc);
                wb.Save(pathDst, new Cells.HtmlSaveOptions(Cells.SaveFormat.Html));
                break;

            case ".ppt":
            case ".pptx":
                using (var pres = new Slides.Presentation(pathSrc))
                {
                    var htmlOpt = new Slides.Export.HtmlOptions
                    {
                        HtmlFormatter = Slides.Export.HtmlFormatter.CreateDocumentFormatter("", false)
                    };
                    pres.Save(pathDst, Slides.Export.SaveFormat.Html, htmlOpt);
                }
                break;

            case ".pdf":
                var pdfSrc = new Pdf.Document(pathSrc);
                pdfSrc.Save(pathDst, Pdf.SaveFormat.Html);
                //pdfSrc.Save(pathDst, new Pdf.HtmlSaveOptions
                //{
                //	FixedLayout = true,
                //	RasterImagesSavingMode = Pdf.HtmlSaveOptions.RasterImagesSavingModes.AsExternalPngFilesReferencedViaSvg,
                //	FontSavingMode = Pdf.HtmlSaveOptions.FontSavingModes.SaveInAllFormats,
                //	// Split HTML output into pages
                //	SplitCssIntoPages = true,
                //	// Split css into pages
                //	SplitIntoPages = true
                //});
                break;

            default:
                throw new Exception("不支持的格式");
            }
            var html = System.IO.File.ReadAllText(pathDst);

            html = Regex.Replace(html, "(Evaluation Only\\. Created with Aspose\\.(.+?)\\. Copyright \\d+-\\d+ Aspose Pty Ltd\\.)|(This document was truncated here because it was created in the Evaluation Mode\\.)", "");
            System.IO.File.WriteAllText(pathDst, html);
            return(pathDst);
        }
Esempio n. 6
0
        // This method will be executed everytime an email is sent
        private void Application_ItemSend(object Item, ref bool Cancel)
        {
            if (EnableAsposeWordsMetadata)
            {
                try
                {
                    Outlook.MailItem thisEmail = Item as Outlook.MailItem;
                    bool flgPassword = false;
                    for (int i = thisEmail.Attachments.Count; i > 0; i--)
                    {
                        Outlook.Attachment attachment = thisEmail.Attachments[i];

                        // save attachment in temp location
                        int attachmentIndex = attachment.Index;
                        string tempPath = Path.GetTempPath();
                        string tempFileName = tempPath + attachment.FileName;
                        attachment.SaveAsFile(tempFileName);

                        // Check the file format for word documents
                        FileFormatInfo info = FileFormatUtil.DetectFileFormat(tempFileName);
                        bool WordAttachment = false;

                        switch (info.LoadFormat)
                        {
                            case LoadFormat.Doc:
                            case LoadFormat.Dot:
                            case LoadFormat.Docx:
                            case LoadFormat.Docm:
                            case LoadFormat.Dotx:
                            case LoadFormat.Dotm:
                            case LoadFormat.FlatOpc:
                            case LoadFormat.Rtf:
                            case LoadFormat.WordML:
                            case LoadFormat.Html:
                            case LoadFormat.Mhtml:
                            case LoadFormat.Odt:
                            case LoadFormat.Ott:
                            case LoadFormat.DocPreWord97:
                                WordAttachment = true;
                                break;
                            default:
                                WordAttachment = false;
                                break;
                        }

                        // If word Attachment is found
                        if (WordAttachment)
                        {
                            try
                            {
                                Aspose.Words.Document doc = new Words.Document(tempFileName);

                                // Remove if there is any protection on the document
                                ProtectionType protection = doc.ProtectionType;
                                if (protection != ProtectionType.NoProtection)
                                    doc.Unprotect();

                                // Remove all built-in and Custom Properties
                                doc.CustomDocumentProperties.Clear();
                                doc.BuiltInDocumentProperties.Clear();

                                // Password will be removed if the document is password protected.
                                if (protection != ProtectionType.NoProtection)
                                    doc.Protect(protection);

                                // Save the file back to temp location
                                doc.Save(tempFileName);

                                // Replace the original attachment
                                thisEmail.Attachments.Remove(attachmentIndex);
                                thisEmail.Attachments.Add(tempFileName, missing, attachmentIndex, missing);
                            }
                            catch (Words.IncorrectPasswordException ex)
                            {
                                flgPassword = true;
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        // Delete file from temp folder
                        if (File.Exists(tempFileName))
                            File.Delete(tempFileName);
                    }
                    if (flgPassword)
                    {
                        MessageBox.Show("Password protected documents cannot be processed for Metadata cleaning");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message, "Error");
                    Cancel = true;
                }
            }
        }
        public string Conversion(
            string sourceFile,
            string outFolder,
            string format
            )
        {
            using (var presentation = new Presentation(sourceFile))
            {
                var fileName   = Path.GetFileNameWithoutExtension(sourceFile);
                var outOneFile = Path.Combine(outFolder, $"{fileName}.{format}");

                switch (format)
                {
                case "odp":
                case "otp":
                case "pptx":
                case "pptm":
                case "potx":
                case "ppt":
                case "pps":
                case "ppsm":
                case "pot":
                case "potm":
                case "pdf":
                case "xps":
                case "ppsx":
                case "tiff":
                case "html":
                case "swf":
                    var slidesFormat = format.ToString().ParseEnum <SaveFormat>();
                    presentation.Save(outOneFile, slidesFormat);
                    return(outOneFile);

                case "txt":
                    var lines = new List <string>();
                    foreach (var slide in presentation.Slides)
                    {
                        foreach (var shp in slide.Shapes)
                        {
                            if (shp.Placeholder != null)
                            {
                                lines.Add(((AutoShape)shp).TextFrame.Text);
                            }
                        }

                        var notes = slide.NotesSlideManager.NotesSlide?.NotesTextFrame?.Text;

                        if (!string.IsNullOrEmpty(notes))
                        {
                            lines.Add(notes);
                        }
                    }
                    System.IO.File.WriteAllLines(outOneFile, lines);
                    return(outOneFile);

                case "doc":
                case "docx":
                    using (var stream = new MemoryStream())
                    {
                        presentation.Save(stream, SaveFormat.Html);
                        stream.Flush();
                        stream.Seek(0, SeekOrigin.Begin);

                        var doc = new Words.Document(stream);
                        switch (format)
                        {
                        case "doc":
                            doc.Save(outOneFile, Words.SaveFormat.Doc);
                            break;

                        case "docx":
                            doc.Save(outOneFile, Words.SaveFormat.Docx);
                            break;

                        default:
                            throw new ArgumentException($"Unknown format {format}");
                        }
                    }
                    return(outOneFile);

                case "bmp":
                case "jpeg":
                case "png":
                case "emf":
                case "wmf":
                case "gif":
                case "exif":
                case "ico":
                    ImageFormat GetImageFormat(string f)
                    {
                        switch (format)
                        {
                        case "bmp":
                            return(ImageFormat.Bmp);

                        case "jpeg":
                            return(ImageFormat.Jpeg);

                        case "png":
                            return(ImageFormat.Png);

                        case "emf":
                            return(ImageFormat.Wmf);

                        case "wmf":
                            return(ImageFormat.Wmf);

                        case "gif":
                            return(ImageFormat.Gif);

                        case "exif":
                            return(ImageFormat.Emf);

                        case "ico":
                            return(ImageFormat.Icon);

                        default:
                            throw new ArgumentException($"Unknown format {format}");
                        }
                    }

                    ///var size = presentation.SlideSize.Size;

                    for (var i = 0; i < presentation.Slides.Count; i++)
                    {
                        var slide   = presentation.Slides[i];
                        var outFile = Path.Combine(outFolder, $"{i}.{format}");
                        using (var bitmap = slide.GetThumbnail(1, 1))                                // (new Size((int)size.Width, (int)size.Height)))
                            bitmap.Save(outFile, GetImageFormat(format));
                    }

                    return(null);

                case "svg":
                    var svgOptions = new SVGOptions
                    {
                        PicturesCompression = PicturesCompression.DocumentResolution
                    };

                    for (var i = 0; i < presentation.Slides.Count; i++)
                    {
                        var slide   = presentation.Slides[i];
                        var outFile = Path.Combine(outFolder, $"{i}.{format}");
                        using (var stream = new FileStream(outFile, FileMode.CreateNew))
                            slide.WriteAsSvg(stream, svgOptions);
                    }

                    return(null);

                default:
                    throw new ArgumentException($"Unknown format {format}");
                }
            }
        }
        private void SaveChunk(string outputDir, string chunkName, string fileName, ISlide[] chunkSlides, SlidesConversionFormat format, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (chunkSlides.Length == 0)
            {
                return;
            }
            cancellationToken.ThrowIfCancellationRequested();

            var presentation = chunkSlides[0].Presentation;

            switch (format)
            {
            case SlidesConversionFormat.pptx:
            case SlidesConversionFormat.pptm:
            case SlidesConversionFormat.ppsx:
            case SlidesConversionFormat.ppsm:
            case SlidesConversionFormat.potx:
            case SlidesConversionFormat.potm:
            case SlidesConversionFormat.pps:
            case SlidesConversionFormat.ppt:
            case SlidesConversionFormat.pot:
            case SlidesConversionFormat.odp:
            case SlidesConversionFormat.otp:

                using (var splitPresentation = new Presentation())
                {
                    while (splitPresentation.Slides.Count > 0)
                    {
                        splitPresentation.Slides.RemoveAt(0);
                    }

                    foreach (var slide in chunkSlides)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        splitPresentation.Slides.AddClone(slide);
                    }

                    splitPresentation.Save(Path.Combine(outputDir, $"{fileName}_{chunkName}.{format}"), format.ToString().ParseEnum <SaveFormat>());
                }
                break;

            case SlidesConversionFormat.pdf:
            case SlidesConversionFormat.xps:
            case SlidesConversionFormat.tiff:
            case SlidesConversionFormat.html:
            case SlidesConversionFormat.swf:

                presentation.Save(Path.Combine(outputDir, $"{fileName}_{chunkName}.{format}"),
                                  chunkSlides.Select(s => s.SlideNumber).ToArray(),
                                  format.ToString().ParseEnum <SaveFormat>());
                break;

            case SlidesConversionFormat.doc:
            case SlidesConversionFormat.docx:

                using (var stream = new MemoryStream())
                {
                    presentation.Save(stream, chunkSlides.Select(s => s.SlideNumber).ToArray(), SaveFormat.Html);
                    stream.Flush();
                    stream.Seek(0, SeekOrigin.Begin);

                    var doc        = new Words.Document(stream);
                    var wordFormat = format == SlidesConversionFormat.doc
                                                        ? Words.SaveFormat.Doc
                                                        : Words.SaveFormat.Docx;
                    doc.Save(Path.Combine(outputDir, $"{fileName}_{chunkName}.{format}"), wordFormat);
                }
                break;

            case SlidesConversionFormat.txt:

                var lines = new List <string>();
                foreach (var slide in chunkSlides)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    foreach (var shp in slide.Shapes)
                    {
                        if (shp is AutoShape ashp)
                        {
                            lines.Add(ashp.TextFrame.Text);
                        }
                    }

                    var notes = slide.NotesSlideManager.NotesSlide?.NotesTextFrame?.Text;

                    if (!string.IsNullOrEmpty(notes))
                    {
                        lines.Add(notes);
                    }
                }
                System.IO.File.WriteAllLines(Path.Combine(outputDir, $"{fileName}_{chunkName}.{format}"), lines);
                break;

            case SlidesConversionFormat.bmp:
            case SlidesConversionFormat.jpeg:
            case SlidesConversionFormat.png:
            case SlidesConversionFormat.emf:
            case SlidesConversionFormat.wmf:
            case SlidesConversionFormat.gif:
            case SlidesConversionFormat.exif:
            case SlidesConversionFormat.ico:

                foreach (var slide in chunkSlides)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var outFile = Path.Combine(outputDir, $"{fileName}_{slide.SlideNumber:D2}.{format}");
                    using (var bitmap = slide.GetThumbnail(1, 1))
                    {
                        bitmap.Save(outFile, GetImageFormat(format));
                    }
                }
                break;

            case SlidesConversionFormat.svg:

                var svgOptions = new SVGOptions
                {
                    PicturesCompression = PicturesCompression.DocumentResolution
                };
                foreach (var slide in chunkSlides)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var outFile = Path.Combine(outputDir, $"{fileName}_{slide.SlideNumber:D2}.{format}");
                    using (var stream = new FileStream(outFile, FileMode.CreateNew))
                    {
                        slide.WriteAsSvg(stream, svgOptions);
                    }
                }
                break;

            default:
                throw new ArgumentException($"Unknown format {format}");
            }
        }