zoukankan      html  css  js  c++  java
  • 实现使用Com组件把Word文档导出为PDF或XPS,并向Word中插入图片的功能

    1,引用“Microsoft Word 15.0 Object Library"

      

    2,引用以上DLL后项目中会出现三个引用,如图:

      

    3,需要修改“Microsoft.Office.Interop.Word"在可嵌入类型,如图:

      

    4,源码如下:

      

    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace OfficeLibrary
    {
        public class WordHelper : IDisposable
        {
            ApplicationClass wordApplication;
            Document wordDocument;
            object paramMissing = Type.Missing;
    
            bool paramOpenAfterExport = false;
            WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
            WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;
    
            public WordHelper(string sourceDocPath)
            {
                if (string.IsNullOrWhiteSpace(sourceDocPath))
                    throw new ArgumentNullException("sourceDocPath");
    
                object docPath = (object)sourceDocPath;
    
                wordApplication = new ApplicationClass();
                wordDocument = wordApplication.Documents.Open(
                        ref docPath, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing);
            }
    
            public void ExportToPDF(string exportFilePath)
            {
                ExportAsFixedFormat(exportFilePath, WdExportFormat.wdExportFormatPDF);
            }
    
            public void ExportToXPS(string exportFilePath)
            {
                ExportAsFixedFormat(exportFilePath, WdExportFormat.wdExportFormatXPS);
            }
    
            private void ExportAsFixedFormat(string exportFilePath, WdExportFormat exportFormat)
            {
                if (string.IsNullOrWhiteSpace(exportFilePath))
                    throw new ArgumentNullException("exportFilePath");
    
                // Export it in the specified format.
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(exportFilePath,
                        exportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            }
    
            public void InsertPicture(string fileName)
            {
                if (string.IsNullOrWhiteSpace(fileName))
                    throw new ArgumentNullException("fileName");
    
                if (wordDocument != null)
                {
                    Range range = wordDocument.Paragraphs.Last.Range;
                    wordDocument.InlineShapes.AddPicture(fileName, paramMissing, paramMissing, range);
                }
            }
    
            public void Dispose()
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
    
                // Quit Word and release the ApplicationClass object.
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
    
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }

     5,测试:  

    string sourceDocPath = @"C:TempTest.docx";
    string exportFilePath = @"C:TempTest.pdf";
    string imagePath = @"C:Temp11.jpg";
    
    //导出PDF
    using (WordHelper wordHelper = new WordHelper(sourceDocPath))
    {
        wordHelper.ExportToPDF(exportFilePath);
    }  
    
    //插入图片
    using (WordHelper wordHelper = new WordHelper(sourceDocPath))
    {
        wordHelper.InsertPicture(imagePath);
    }
    
    Bitmap map = new Bitmap("C:\Temp\11.jpg");
    string targetJpg = "C:\Temp\22.jpg";
    map.Save(targetJpg, ImageFormat.Jpeg);
    using (WordHelper wordHelper = new WordHelper(sourceDocPath))
    {
        wordHelper.InsertPicture(targetJpg);
    }
    File.Delete(targetJpg);  
  • 相关阅读:
    我开发中的用到的几个框架
    关于ASP.NETCore的分享之学习路线
    首个.NET5+Vue.js业务模块化快速开发框架【NetModular】发布
    [C#] (原创)一步一步教你自定义控件 —— 系列文章
    EFS加密
    博客园样式美化:给博客添加一个音乐播放器
    XSS语义分析
    TCP回放攻击 & DDoS脉冲攻击Hit and Run IoT僵尸网络 在DDoS攻击黑产领域最活跃
    小样本学习,阿里做得比较早,但是效果未知——小样本有3类解决方法(算法维度):迁移学习、元学习(模型基础上学习模型)、度量学习(相似度衡量,也就是搜索思路),数据维度还有GAN
    真实世界中的开集识别问题(Open-Set Recognition Problem)——Walter J. Scheirer研究是最深的,安全里已经有研究了,但是感觉只是触及了皮毛而已
  • 原文地址:https://www.cnblogs.com/jiao1855/p/6517611.html
Copyright © 2011-2022 走看看