您现在的位置是:首页 > C# > C#实现PDF文档转Word

C#实现PDF文档转Word

王递杰 2021年6月25日 C#

打开nuget安装Aspose.PDF

微信截图_20210625094756.jpg

控制台程序实现:

    class Program
    {
        static void Main(string[] args)
        {
            string pdfFilePath = @"D:\信息安全维护服务协议.pdf";
            string outputFilePath = @"D:\信息安全维护服务协议.docx";
            var stream = FileToStream(pdfFilePath);
            PdfToWordHelper.ConvertToWord(stream, outputFilePath);
            Console.WriteLine("转换成功!");
            Console.ReadKey();
        }

        /// <summary>
        /// 文件转换成文件流
        /// </summary>
        /// <param name="filePath">文件名</param>
        /// <returns>文件流</returns>
        public static Stream FileToStream(string filePath)
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); //打开文件

            // 读取文件Byte[]
            //
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();

            Stream stream = new MemoryStream(bytes); //byte[]转换为Stream
            return stream;
        }

    }


PdfToWordHelper 代码:

 public class PdfToWordHelper
    {
        //若已失效,请更换lic
        private static string lic = "DQo8TGljZW5zZT4NCjxEYXRhPg0KPExpY2Vuc2VkVG8+QXNwb3NlIFB0eSBMdGQ8L0xpY2Vuc2VkVG8+DQo8RW1haWxUbz5zYWxtYW4uc2FyZnJhekBhc3Bvc2UuY29tPC9FbWFpbFRvPg0KPExpY2Vuc2VUeXBlPkRldmVsb3BlciBTbWFsbCBCdXNpbmVzczwvTGljZW5zZVR5cGU+DQo8TGljZW5zZU5vdGU+T25lIERldmVsb3BlciBBbmQgT25lIERlcGxveW1lbnQgTG9jYXRpb248L0xpY2Vuc2VOb3RlPg0KPE9yZGVySUQ+MjAwNTA2MDc1OTMzPC9PcmRlcklEPg0KPFVzZXJJRD43NDc0MTU8L1VzZXJJRD4NCjxPRU0+VGhpcyBpcyBub3QgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KPFByb2R1Y3RzPg0KPFByb2R1Y3Q+Q29uaG9sZGF0ZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVjdD4NCjwvUHJvZHVjdHM+DQo8RWRpdGlvblR5cGU+UHJvZmVzc2lvbmFsPC9FZGl0aW9uVHlwZT4NCjxTZXJpYWxOdW1iZXI+ODUxZTUwM2MtYmU0NS00M2I4LWFjZWItMzM4OGNmMWQxY2Y1PC9TZXJpYWxOdW1iZXI+DQo8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjEwNTA2PC9TdWJzY3JpcHRpb25FeHBpcnk+DQo8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4NCjxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuY29uaG9sZGF0ZS5jb20vcG9saWNpZXMvdXNlLWxpY2Vuc2U8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQo8L0RhdGE+DQo8U2lnbmF0dXJlPk1rbG0wMWludldkZ0Q3SzQyVHVFQml6MXpVRkhlR1JqVlo5ZDhESDdycVBrV2ozWDAxei84YzlCQjYwb0UvaWF6ZEdSMTFjRWxSR1JUbnVIRHlJQkhOWEFxQVI2dmpjKzExb05ld2lJbzRlNWg4OEYybS91UTRjN3dVQVl6ZUtuV1BzQUhJTVdTQXM0YTVZbmJlWlUrd1p3VzFpR2RnNEZaUEVSdTZsV01jOD08L1NpZ25hdHVyZT4NCjwvTGljZW5zZT4NCg==";


        public static void ConvertToWord(string pdfFilePath,string outputFilePath)
        {
            SetPdfLicense();
            Document pdfDocument = new Document(pdfFilePath);
            pdfDocument.Save(outputFilePath, SaveFormat.DocX);
            pdfDocument.Dispose();
        }

        public static void ConvertToWord(Stream stream, string outputFilePath)
        {
            SetPdfLicense();
            Document pdfDocument = new Document(stream);
            pdfDocument.Save(outputFilePath, SaveFormat.DocX);
            pdfDocument.Dispose();
        }

        private static void SetPdfLicense()
        {
            MemoryStream licStream = new MemoryStream(Convert.FromBase64String(lic));
            License license = new License();
            licStream.Seek(0, SeekOrigin.Begin);
            license.SetLicense(licStream);
            licStream.Close();
        }

        //文件转成文件流
        private static Stream FileToStream(string filePath)
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); //打开文件

            // 读取文件Byte[]
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();

            Stream stream = new MemoryStream(bytes); //byte[]转换为Stream
            return stream;
        }

    }


评论

评论插件