今天遇到了,需要将pdf文件插到word里,好像word不支持直接插入pdf文件,所以现在通过将pdf转为图片的方式,逐个将图片插入到word。这里使用apose.pdf第三方库,将多个pdf文件读取,然后转为pdf。具体的实现代码如下所示。

 private void BTN_PDF_TO_IMAGES_Click(object sender, EventArgs e)
        {
            List<string> files = GetFiles(@"C:\Users\qrb\Desktop\测绘资质\三、专业技术人员\part7", new string[] { "*.pdf"});
            int indexxx = 0;
            foreach (var item in files)
            {
                indexxx = indexxx + 1;
                Console.WriteLine(item);

                try
                {
                    Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(item);
                    int pageCount = pdfDocument.Pages.Count;
                    Aspose.Pdf.Devices.Resolution resolution = new Aspose.Pdf.Devices.Resolution(130);
                    Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(resolution);

                    string parentPath=Path.GetDirectoryName(item);

                    
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(item);
                    for (int pageIndex = 1; pageIndex <= pageCount; pageIndex++) {

                        string fileName = parentPath + "\\"+ fileNameWithoutExtension + "_" + Convert.ToString(pageIndex)+".jpeg";
                        MemoryStream stream = new MemoryStream();
                        jpegDevice.Process(pdfDocument.Pages[pageIndex], stream);

                        using (var fs = new FileStream(fileName, FileMode.OpenOrCreate))
                        {
                            jpegDevice.Process(pdfDocument.Pages[pageIndex], fs);
                            fs.Close();
                        }

                    }
                }
                catch (Exception ex) {
                    System.Windows.Forms.MessageBox.Show("异常错误:" + ex.ToString());
                }

            }
10-18 22:56