Office Series (1) --- Office files (Word, PPT, Excel) into a PDF file

Requirements: The Office file as a preview of the article and on the web page, mainly (Word, PPT, Excel) 3 types of files.

Research a bit to find two solutions

  • Direct call function to achieve Microsoft's online preview (Preview premise: Preview resources must be accessible directly through the public network) Microsoft interface documentation
https://view.officeapps.live.com/op/view.aspx?src=http%3a%2f%2fvideo.ch9.ms%2fbuild%2f2011%2fslides%2fTOOL-532T_Sutter.pptx

In srcthe back of splicing it is to preview the file address (address above case is the official preview can be viewed directly in the page)

  • Office will convert PDF preview pages

Office-based solutions implemented

Implementation: installed on the local server Microsoft Office, COM interfaces on the server by calling C # code to convert Office files to PDF (analogous to open a Word document using Office software, and then save it as a PDF file).

(These packages can only be installed by Nuget Package Manager package requires the .Net FrameWorkuse of versions of the project)

Microsoft.Office.Interop.Word
Microsoft.Office.Interop.PowerPoint
Microsoft.Office.Interop.Excel

Code:

public class OfficeHelper
{
    static Word.Application wordApplication = new Word.Application();
    static Excel.Application excelApplication = new Excel.Application();
    static PowerPoint.Application pptApplication = new PowerPoint.Application();
    
    /// <summary>
    /// 将Word文档转换成PDF格式
    /// </summary>
    /// <param name="sourcePath">源文件路径</param>
    /// <param name="targetPath">目标文件路径</param>
    /// <returns></returns>
    public static bool WordConvertPDF(string sourcePath, string targetPath)
    {
        bool result;
        Word.Document wordDocument = null;
        try
        {
            wordDocument = wordApplication.Documents.Open(ref sourcePath);
            if (wordDocument != null)
            {
                wordDocument.SaveAs2(targetPath, WdExportFormat.wdExportFormatPDF);
                //wordDocument.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
                result = true;
            }
        }
        catch (Exception ex)
        {
            result = false;
            LogHelper.Log($"文件:{sourcePath} 生成失败,原因:{ex.Message}", ex.StackTrace);
        }
        finally
        {
            if (wordDocument != null)
            {
                wordDocument.Close();
                wordDocument = null;
            }
        }
        return result;
    }
    
    
    /// <summary>
    /// 将Excel文档转换成PDF格式
    /// </summary>
    /// <param name="sourcePath">源文件路径</param>
    /// <param name="targetPath">目标文件路径</param>
    /// <returns></returns>
    public static bool ExcelConvertPDF(string sourcePath, string targetPath)
    {
        bool result;
        Workbook workBook = null;
        try
        {
            workBook = excelApplication.Workbooks.Open(sourcePath);
            if (workBook != null)
            {
                workBook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, targetPath);
                result = true;
            }
        }
        catch (Exception ex)
        {
            result = false;
            LogHelper.Log($"文件:{sourcePath} 生成失败,原因:{ex.Message}", ex.StackTrace);
        }
        finally
        {
            if (workBook != null)
            {
                workBook.Close();
                workBook = null;
            }
        }
        return result;
    }

    /// <summary>
    /// 将PPT文档转换成pdf格式
    /// </summary>
    /// <param name="sourcePath">源文件路径</param>
    /// <param name="targetPath">目标文件路径</param> 
    /// <returns></returns>
    public static bool PPTConvertPDF(string sourcePath, string targetPath)
    {
        bool result;
        object missing = Type.Missing;
        Presentation persentation = null;
        try
        {
            persentation = pptApplication.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
            if (persentation != null)
            {
                persentation.SaveAs(targetPath, PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoTrue);
                //persentation.ExportAsFixedFormat(targetPath, PpFixedFormatType.ppFixedFormatTypePDF);
                result = true;
            }
        }
        catch (Exception ex)
        {
            result = false;
            LogHelper.Log($"文件:{sourcePath} 生成失败,原因:{ex.Message}", ex.StackTrace);
        }
        finally
        {
            if (persentation != null)
            {
                persentation.Close();
                persentation = null;
            }
        }
        return result;
    }
}

Office COM API provided SaveAsand ExportAsFixedFormattwo methods to generate a document, you need to pay attention to different parameters when invoked, most use the default value on it ( interface documentation addresses ).

In the code above wordApplicationas a static variable raised, each time the file is loaded, and through which opening (corresponding to the program has been open Office.Word). When I tested locally, it is locally installed Microsoft Office 2016 version of the code has been operating normally. When I use the program to send someone else, it found only in the first successful conversion Word processing documents, after the file conversion failed.
After examination, because the other side is installed on your computer Office365 version, there may be processed after the first file wordApplicationobject is closed, causing the back of the file can not be converted correctly. Modify the code before each conversion to re-instantiated Word.Application(equivalent to reopen again Office.Word program), although each instantiation lead to inefficient, but all the files are successfully processed. The most amazing is the same environment pptApplicationof continuous call is normal, not yet know the specific cause of the problem, there is little understanding of the partner can tell me.

Solutions based on third-party plug-ins to achieve

Here are introduced at Spire and GemBox deal directly with the documents in the two plug-ins, they can not install Office software on the server.
PS: official version of the fee to use, otherwise the document will be generated with a watermark. Free version will have some limitations, such as creating or reading Officehave page limits when the file, direct throw an exception if the limit is exceeded.

Here briefly under the code, detailed api can go to the official website of the document plug-in to explore.

  • Free Spire.Doc for .NET

public static void WordConvertPDF(string sourcePath, string targetPath)
{
    using (var doc = new Document(sourcePath))
    {
        doc.SaveToFile(targetPath, Spire.Doc.FileFormat.PDF);
    }
}
  • GemBox.Document free version

public static void WordConvertPDF(string sourcePath, string targetPath)
{
    // If using Professional version, put your serial key below.
    ComponentInfo.SetLicense("FREE-LIMITED-KEY");
    DocumentModel document = DocumentModel.Load(sourcePath);
    document.Save(targetPath, SaveOptions.PdfDefault);
}

WPS-based solutions implemented

This and Office-based solutions, call COM interfaces through code that implements the converted file. Of course, in advance WPS software installed on the server.

In the local WPS installation directory, locate the following dll files, and references to the project,

wpsapi.dll
wpsapiex.dll

Code:

public static void WordConvertPDF(string sourcePath, string targetPath)
{
    var app = new Word.Application();
    var doc = app.Documents.Open(sourcePath,Visible: MsoTriState.msoFalse);
    doc.SaveAs2(targetPath, Word.WdExportFormat.wdExportFormatPDF);
    doc.Close();
    app.Close();
}

Which Wordis wpsapi.dlllater added to the program, the assembly namespace name.
WPS convert the document to call me just run natively successful, and no practical application, can only be said to be understanding and try to do a bit of it.

to sum up

The Office documents into PDF files has been successful, preview PDF files in a web page on the simple, direct and even some default browser can open the preview PDF file. This online solution is already a lot, the final choice by pdf.jsthe preview on the page. Preview files already have, and the rest sent a cover, the title is too boring dry, illustrated is king, heading the party needs some "realistic" picture of it to set the mood!
So naturally there is this question of how an appropriate cover art to the document to set it? Immediately thought of a wayPick yourself a picture pleasing to the eye as the cover(Well, this is nonsense). There are several really thinking about the following solutions:

  1. Get Office file inside the picture on the cover
  2. Get PDF file preview the first page as the cover

Predict how funeral, Let's hear next decomposition (after all, I have not finished ah! ~ ~ ~ ~).

Guess you like

Origin www.cnblogs.com/cplemom/p/12186527.html