Exemplo n.º 1
0
        public override bool Convert(string sourceFilePath, string targetPdfPath)
        {
            Aspose.Cells.Workbook       workbook    = new Aspose.Cells.Workbook(sourceFilePath);
            Aspose.Cells.PdfSaveOptions saveOptions = new Aspose.Cells.PdfSaveOptions();
            saveOptions.OnePagePerSheet = true;
            workbook.Save(targetPdfPath, saveOptions);

            return(File.Exists(targetPdfPath) && new FileInfo(targetPdfPath).Length > 0);
        }
Exemplo n.º 2
0
        public void ExeclToPDF(String from, String to)
        {
            try
            {
                Aspose.Cells.Workbook       xls           = new Aspose.Cells.Workbook(from);
                Aspose.Cells.PdfSaveOptions xlsSaveOption = new Aspose.Cells.PdfSaveOptions();
                xlsSaveOption.SecurityOptions = new Aspose.Cells.Rendering.PdfSecurity.PdfSecurityOptions();
                #region pdf 加密
                //Set the user password
                //PDF加密功能
                //xlsSaveOption.SecurityOptions.UserPassword = "******";
                //Set the owner password
                //xlsSaveOption.SecurityOptions.OwnerPassword = "******";
                #endregion
                //Disable extracting content permission
                xlsSaveOption.SecurityOptions.ExtractContentPermission = false;
                //Disable print permission
                xlsSaveOption.SecurityOptions.PrintPermission = false;
                xlsSaveOption.AllColumnsInOnePagePerSheet     = true;

                //权限这块的设置成不可复制
                PdfSaveOptions saveOptions = new PdfSaveOptions();
                // Create encryption details and set owner password.
                PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails(string.Empty, "password", PdfEncryptionAlgorithm.RC4_128);
                // Start by disallowing all permissions.
                encryptionDetails.Permissions = PdfPermissions.DisallowAll;
                // Extend permissions to allow editing or modifying annotations.
                encryptionDetails.Permissions = PdfPermissions.ModifyAnnotations | PdfPermissions.DocumentAssembly;
                saveOptions.EncryptionDetails = encryptionDetails;
                // Render the document to PDF format with the specified permissions.
                //doc.Save(to, saveOptions);

                xls.Save(to, xlsSaveOption);

                Console.WriteLine("转换成功!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.WriteLine("强行报错!");
            }
        }
Exemplo n.º 3
0
        private static void ConvertExcelToPdf()
        {
            var extension = Path.GetExtension(excelPath).ToLower();
            var pdfPath   = Path.Combine(pdfDirectory, $"{Path.GetFileName(excelPath)}.pdf");

            if (File.Exists(pdfPath))
            {
                File.Delete(pdfPath);
            }

            Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(excelPath);
            //excel.Worksheets[0].PageSetup.PaperSize = Aspose.Cells.PaperSizeType.PaperA2;
            excel.Worksheets[0].PageSetup.PaperSize = Aspose.Cells.PaperSizeType.PaperA2;

            Aspose.Cells.PdfSaveOptions saveOptions = new Aspose.Cells.PdfSaveOptions(Aspose.Cells.SaveFormat.Pdf);
            saveOptions.AllColumnsInOnePagePerSheet = true;
            //saveOptions.OnePagePerSheet = true;
            //saveOptions.PdfCompression = Aspose.Cells.Rendering.PdfCompressionCore.Flate;
            //saveOptions.PrintingPageType = Aspose.Cells.PrintingPageType.Default;
            excel.Save(pdfPath, saveOptions);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 上传附件转换成PDF
        /// </summary>
        /// <param name="fileFullPath">文件完整路径</param>
        /// <returns>转换后PDF的URL</returns>
        public static string AttachmentFile2PDF(string fileFullPath, string target)
        {
            string ret = string.Empty;

            try
            {
                string fileExt  = Path.GetExtension(fileFullPath).ToLower();
                string filePath = Path.GetFullPath(fileFullPath);
                string pdfFile  = fileFullPath + ".pdf";
                string outFile  = "";
                // string pdfPath = fileFullPath.Replace(ConfigUtil.GetAppConfig("AttachmentPath"), "").Replace(@"\", @"/") + ".pdf";
                string pdfPath = target.Replace(@"\", @"/") + ".pdf";
                if (!File.Exists(pdfFile))
                {
                    switch (fileExt)
                    {
                    case ".doc":
                    case ".docx":
                        Aspose.Words.Document doc = new Aspose.Words.Document(fileFullPath);
                        doc.Save(pdfFile, Aspose.Words.SaveFormat.Pdf);
                        outFile = pdfPath;
                        break;

                    case ".xls":
                    case ".xlsx":
                        Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(fileFullPath);
                        excel.Settings.MemorySetting        = Aspose.Cells.MemorySetting.MemoryPreference;
                        excel.Settings.AutoCompressPictures = true;
                        excel.Settings.EnableMacros         = false;
                        Aspose.Cells.PdfSaveOptions saveOptions = new Aspose.Cells.PdfSaveOptions(Aspose.Cells.SaveFormat.Pdf);
                        saveOptions.OnePagePerSheet  = true;
                        saveOptions.PdfCompression   = Aspose.Cells.Rendering.PdfCompressionCore.Flate;
                        saveOptions.PrintingPageType = Aspose.Cells.PrintingPageType.IgnoreBlank;
                        excel.Save(pdfFile, saveOptions);
                        outFile = pdfPath;
                        break;

                    case ".ppt":
                    case ".pptx":
                        Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(fileFullPath);
                        ppt.Save(pdfFile, Aspose.Slides.Export.SaveFormat.Pdf);
                        outFile = pdfPath;
                        break;

                    case ".png":
                    case ".jpg":
                    case ".jpeg":
                    case ".bmp":
                        try
                        {
                            Aspose.Pdf.Generator.Pdf     pdf   = new Aspose.Pdf.Generator.Pdf();
                            Aspose.Pdf.Generator.Section sec   = pdf.Sections.Add();
                            Aspose.Pdf.Generator.Image   image = new Aspose.Pdf.Generator.Image(sec);
                            sec.Paragraphs.Add(image);
                            image.ImageInfo.File = fileFullPath;
                            // image.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg;
                            pdf.Save(pdfFile);
                            outFile = pdfPath;
                        }
                        catch (Exception ex)
                        {
                            if (File.Exists(pdfFile))
                            {
                                File.Delete(pdfFile);
                            }
                            throw ex;
                        }


                        break;

                    case ".pdf":
                        pdfPath = pdfPath.Substring(0, pdfPath.Length - 4);
                        outFile = pdfPath;
                        break;
                    }
                }

                return(outFile);
            }
            catch (System.Exception ex)
            {
                ret = string.Empty;
                // LogWriter.GetInstance().ErrOut(ex.ToString());
            }
            return(ret);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 上传附件转换成PDF
        /// </summary>
        /// <param name="fileFullPath">文件完整路径</param>
        /// <returns>转换后PDF的URL</returns>
        protected static string AttachmentFile2PDF(ref string savePath, ref string fileName, string fileFullPath)
        {
            string ret = string.Empty;

            try
            {
                string fileExt  = Path.GetExtension(fileFullPath).ToLower();
                string filePath = Path.GetFullPath(fileFullPath);
                string pdfFile  = fileFullPath + ".pdf";
                string pdfPath  = fileFullPath.Replace(@"\", @"/") + ".pdf";
                if (!File.Exists(pdfFile))
                {
                    switch (fileExt)
                    {
                    case ".doc":
                    case ".docx":
                    case ".rtf":
                        Aspose.Words.Document doc = new Aspose.Words.Document(fileFullPath);
                        doc.Save(pdfFile, Aspose.Words.SaveFormat.Pdf);
                        ret      = savePath + ".pdf";
                        fileName = fileName + ".pdf";
                        break;

                    case ".xls":
                    case ".xlsx":
                        Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(fileFullPath);
                        excel.Settings.MemorySetting        = Aspose.Cells.MemorySetting.MemoryPreference;
                        excel.Settings.AutoCompressPictures = true;
                        excel.Settings.EnableMacros         = false;
                        Aspose.Cells.PdfSaveOptions saveOptions = new Aspose.Cells.PdfSaveOptions(Aspose.Cells.SaveFormat.Pdf);
                        saveOptions.OnePagePerSheet  = true;
                        saveOptions.PdfCompression   = Aspose.Cells.Rendering.PdfCompressionCore.Flate;
                        saveOptions.PrintingPageType = Aspose.Cells.PrintingPageType.IgnoreBlank;
                        excel.Save(pdfFile, saveOptions);
                        ret      = savePath + ".pdf";
                        fileName = fileName + ".pdf";
                        break;

                    case ".ppt":
                    case ".pptx":
                        Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(fileFullPath);
                        ppt.Save(pdfFile, Aspose.Slides.Export.SaveFormat.Pdf);
                        ret      = savePath + ".pdf";
                        fileName = fileName + ".pdf";
                        break;

                    case ".png":
                    case ".jpg":
                        try
                        {
                            Aspose.Pdf.Generator.Pdf     pdf   = new Aspose.Pdf.Generator.Pdf();
                            Aspose.Pdf.Generator.Section sec   = pdf.Sections.Add();
                            Aspose.Pdf.Generator.Image   image = new Aspose.Pdf.Generator.Image(sec);
                            sec.Paragraphs.Add(image);
                            image.ImageInfo.File = fileFullPath;
                            //image.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg;
                            pdf.Save(pdfFile);
                        }
                        catch (Exception ex)
                        {
                            if (File.Exists(pdfFile))
                            {
                                File.Delete(pdfFile);
                            }
                            throw ex;
                        }

                        ret      = savePath + ".pdf";
                        fileName = fileName + ".pdf";
                        break;

                    case ".pdf":

                    case ".rar":
                    case ".zip":
                    case ".7z":
                        pdfPath = pdfPath.Substring(0, pdfPath.Length - 4);
                        ret     = savePath;
                        break;
                    }
                }
                return(ret);
            }
            catch (Exception ex)
            {
                ret = string.Empty;
                return(ret);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 服务器生成水印文件
        /// </summary>
        /// <param name="fileKey">文件KEY</param>
        /// <param name="cType">1:受控水印;2:临时受控水印;3:作废水印</param>
        /// <param name="code">文档编号,非空时添加文档编号水印</param>
        /// <param name="aTime">评审日期 cType=1传入生效日期,cType=2传入有效期,cType=3传入作废日期</param>
        /// <returns></returns>
        public bool CreateWatermarkFile(string filename, string filekey, string cType, string code, DateTime aTime)
        {
            string tempPath   = textBox2.Text.Trim() + @"\Temp";
            string UploadPath = textBox2.Text.Trim() + @"\Upload";

            if (!File.Exists(Path.Combine(UploadPath, filekey.Substring(0, 2), filekey)))
            {
                Log.Debug("CreateWatermarkFile时发现 {0} 文件不存在!", Path.Combine(UploadPath, filekey.Substring(0, 2), filekey));
                return(false);
            }
            string path = Path.Combine(UploadPath, filekey.Substring(0, 2), filekey);

            if (!Directory.Exists(Path.Combine(tempPath, filekey.Substring(0, 2))))
            {
                Directory.CreateDirectory(Path.Combine(tempPath, filekey.Substring(0, 2)));
            }
            string targetFileName = string.Format("{0}-watermark", filekey);
            string targetPath     = Path.Combine(tempPath, filekey.Substring(0, 2), string.Format("{0}temp", targetFileName));
            string extname        = Path.GetExtension(filename);

            //添加水印效果
            if (extname == "")
            {
                Log.Debug("CreateWatermarkFile时发现BSFILEUPLOAD中找不到 {0} 的文件扩展名!", filekey);
                return(false);
            }
            if (extname.ToLower() == ".doc" || extname.ToLower() == ".docx")
            {
                try
                {
                    using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        Aspose.Words.Document doc = new Aspose.Words.Document(path);
                        doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
                    }
                }
                catch (Exception ex)
                {
                    Log.Exception(ex.Message, ex);
                    return(false);
                }
            }
            else if (extname.ToLower() == ".xls" || extname.ToLower() == ".xlsx")
            {
                try
                {
                    Aspose.Cells.Workbook       xls           = new Aspose.Cells.Workbook(path);
                    Aspose.Cells.PdfSaveOptions xlsSaveOption = new Aspose.Cells.PdfSaveOptions();
                    xlsSaveOption.SecurityOptions = new Aspose.Cells.Rendering.PdfSecurity.PdfSecurityOptions();
                    xlsSaveOption.SecurityOptions.ExtractContentPermission = false;
                    xlsSaveOption.SecurityOptions.PrintPermission          = false;
                    xlsSaveOption.AllColumnsInOnePagePerSheet = true;
                    xls.Save(targetPath, xlsSaveOption);
                }
                catch (Exception ex)
                {
                    Log.Exception(ex.Message, ex);
                    return(false);
                }
            }
            else if (extname.ToLower() == ".pdf")
            {
                File.Copy(path, targetPath, true);
            }

            if (FileAddWaterMark(targetPath, Path.Combine(tempPath, filekey.Substring(0, 2), targetFileName), cType, code, aTime))
            {
                File.Delete(targetPath);
                return(true);
            }
            else
            {
                return(false);
            }
        }