首页 > 其他分享 >使用微软自己的方法,将Office文档转化为pdf

使用微软自己的方法,将Office文档转化为pdf

时间:2024-07-23 09:11:09浏览次数:8  
标签:Word Office missing true paramMissing 文档 pdf null ref

转的,整理资料发现的,挺好。

原创作者找不到了。
///


/// Word转换成pdf
///

/// 源文件路径
/// 目标文件路径
/// true=转换成功
public bool DOCConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
object paramMissing = Type.Missing;
Word.ApplicationClass wordApplication = new Word.ApplicationClass();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, 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);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
result = true;
}
catch
{
result = false;
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}

    /// <summary>
    /// 把Excel文件转换成PDF格式文件  
    /// </summary>
    /// <param name="sourcePath">源文件路径</param>
    /// <param name="targetPath">目标文件路径</param>
    /// <returns>true=转换成功</returns>
    public bool XLSConvertToPDF(string sourcePath, string targetPath)
    {
        bool result = false;
        Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
        object missing = Type.Missing;
        Excel.ApplicationClass application = null;
        Excel.Workbook workBook = null;
        try
        {
            application = new Excel.ApplicationClass();
            object target = targetPath;
            object type = targetType;
            workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, missing, missing);
            workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (workBook != null)
            {
                workBook.Close(true, missing, missing);
                workBook = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }
    ///<summary>        
    /// 把PowerPoint文件转换成PDF格式文件       
    ///</summary>        
    ///<param name="sourcePath">源文件路径</param>     
    ///<param name="targetPath">目标文件路径</param> 
    ///<returns>true=转换成功</returns> 
    public bool PPTConvertToPDF(string sourcePath, string targetPath)
    {
        bool result;
        PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
        object missing = Type.Missing;
        PowerPoint.ApplicationClass application = null;
        PowerPoint.Presentation persentation = null;
        try
        {
            application = new PowerPoint.ApplicationClass();
            persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (persentation != null)
            {
                persentation.Close();
                persentation = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }

标签:Word,Office,missing,true,paramMissing,文档,pdf,null,ref
From: https://www.cnblogs.com/madg/p/18317496

相关文章

  • postgresql 导出数据字典文档
    在PostgreSQL中,数据字典(或称系统目录)是由一系列的系统表组成,这些表包含了数据库对象的元数据,例如表、索引、视图、函数、触发器等。要导出数据字典文档,实际上是导出这些系统表中的信息。尽管PostgreSQL并没有直接提供一个工具来一次性生成完整的数据字典文档,但是你可以使用......
  • 在 JavaScript 异步接收到的浏览器中显示 pdf
    我有一个Django应用程序,我从JS打电话索要pdf。Django视图返回HttpResponse的application/pdf如何让JS将接收到的application/pdf数据显示为pdf?Django视图defpdf_generation(request):context={}t=get_template('html_pd......
  • 使用 Langchain 和 OpenAI 将矢量嵌入数据从 PDF 添加到 PineCone
    我不确定PineCone和Langchain的.get()替代方案是什么。我希望运行此代码,但我不断收到错误消息,指出.get不是Pinecone的属性。我不确定有什么替代方案可以替代它。defadd_to_pinecone(chunks:list[Document]):VectorStore=Pinecone(index='portfolio-assistan......
  • java毕业设计-基于springboot+vue的校园二手交易系统,基于java的校园二手交易系统,基于j
    文章目录前言演示视频项目背景项目架构和内容获取(文末获取)具体实现截图前台功能管理后台技术栈具体功能模块设计系统需求分析可行性分析系统测试为什么我?关于我我自己的网站前言博主介绍:✌️码农一枚,专注于大学生项目实战开发、讲解和毕业......
  • [oeasy]python0026_调试程序_pdb3_帮助_help_求助_文档
    调试程序_debug_next_下一步_list_pdb3......
  • powerdesigner导出图片&PDF
    1、导出图片1.1CTR+A选中当前页的设计的表(物理模型或概念模型设计的表都可以)1.2、edit-->exportImage1.3选择自己要保存的图片类型:png/JEPG/SVG等等关于导出来的图片的清晰度,这里要说一下,自己保存的png和jepg都比较模糊,SVG虽然清晰度很高,但是手机上却无法查看,因此又研......
  • 大学英语四级真题试卷及答案PDF电子版下载2024年6月
    2024年6月第一套英语四级真题试卷及答案,PDF电子版和音频打包下载: https://caiyun.139.com/m/i?005CiegTjwjLu 中国移动云盘,下载不限速,不需要开会员。还要更多历年真题将陆续更新。  英语四级考试是中国大陆地区的一种英语水平测试,主要面向在校大学生。这项考试由教育......
  • eyoucms获取当前栏目分类的下级栏目的文档列表
    [基础用法]标签:modelsartlist(channelartlist)备注:使用channelartlist也可以正常输出描述:获取当前栏目分类的下级栏目的文档列表用法:{eyou:modelsartlisttypeid='栏目ID'type='son'loop='20'}<ahref='{eyou:fieldname='typeurl'/}'>{eyou:f......
  • 纳米体育数据API电竞数据接口:指数数据包接口文档API示例①
    纳米体育数据的数据接口通过JSON拉流方式获取200多个国家的体育赛事实时数据或历史数据的编程接口,无请求次数限制,可按需购买,接口稳定高效;覆盖项目包括足球、篮球、网球、电子竞技、奥运等专题、数据内容。纳米数据API2.0版本包含http协议以及websocket协议,主要通过http获取数......
  • 技术文档必备工具:注释目录树神器 Annotree,我的第一个正式开源项目
    hi,大家好,我是爱听书的程序员阿超非常开心能在这里介绍我的第一个正式开源项目Annotree,项目具体情况如下,请继续阅读......