天天看點

C# 将Word,Execl,PPT,Project, 檔案轉成PDF, 不依賴Office!!

git 位址

https://gitee.com/bandung/Execl_WordTOPDF.git 

包括了各種破解的dll

Word轉PDF

C# 将Word,Execl,PPT,Project, 檔案轉成PDF, 不依賴Office!!

挨個引用

Word轉PDF

public void WordToPDF(String from,String to)
        {
            try
            {
                Document doc = new Document(from);
                //儲存為PDF檔案,此處的SaveFormat支援很多種格式,如圖檔,epub,rtf 等等

                //權限這塊的設定成不可複制
                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);
            
                //doc.Save(to, SaveFormat.Pdf);
                Console.WriteLine("成功!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.WriteLine("強行報錯!");
            }
         
        }      

Execl轉 PDF

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 = "pdfKey";
                //Set the owner password
                //xlsSaveOption.SecurityOptions.OwnerPassword = "sxbztxmgzxt";
                #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("強行報錯!");
            }

        }      

PPT轉PDF

public  void pptToPdf(string from, string to)
        {
            Presentation ppt = new Presentation(from);
            ppt.Save(to, Aspose.Slides.Export.SaveFormat.Pdf);
            Console.WriteLine("成功!");
        }      

Project 檔案轉PDF

public void project()
        {
            var project = new Aspose.Tasks.Project("1.mpp");
            // save in different formats
            project.Save("file\\output.pdf", Aspose.Tasks.Saving.SaveFileFormat.PDF);
            project.Save("file\\output.html", Aspose.Tasks.Saving.SaveFileFormat.HTML);
            project.Save("file\\output.xml", Aspose.Tasks.Saving.SaveFileFormat.XML);
            project.Save("file\\output.png", Aspose.Tasks.Saving.SaveFileFormat.PNG);

            Console.WriteLine("成功!");

        }      

把檔案壓縮成壓縮包

using (var archive = new Archive())
            {
               archive.CreateEntry("1.pptx", @"1.pptx");
               archive.Save(@"result.zip");
            }      

 如果想在其他語言調用使用Thrift吧,其實java 來轉的話也很快