convert office documents to pdf in c#

五迷三道 提交于 2019-12-06 16:35:01

问题


i want to pragmatically convert office documents to pdf.i do not want to use the microsoft office service ddl. this is the code which i used,

using System;
using System.IO;
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.IO.Compression;
using Document = Microsoft.Office.Interop.Word.Document;
   namespace pdfconversion
{
    public class PdfMerge
    {
        static void Main()
        {
            var wordApplication = new Microsoft.Office.Interop.Word.Application();
            Document wordDocument = null;
            object paramSourceDocPath = @"C:\testfile.doc";
            object paramMissing = Type.Missing;
            string paramExportFilePath = @"C:\Temp\Test1234.xps";
            WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS;
            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;
                try
            {
                // Open the source document.
                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);
                   // Export it in the specified format.
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            }
            catch (Exception ex)
            {
                // Respond to the error
            }
            finally
            {
                // Close and release the Document object.
                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();

i want to use a assembly not a com.does anyone know anything that might help me?i tried itextsharp.it does not do the converting. and cutepdf is not able to convert to pdf directly. please help me out with this.i have tried ghostscript. but it converts postscripts to pdf i couldn't find a way to convert office documents to .ps. is there any other free library which i can use to meet my requirement?

please help me out here thanx


回答1:


I've recently looked at lot of free softwares to convert documents to PDF and I must admit that none of them were meeting my requirements. Some converters were not bad but the licenses were to restrictive. I think you will have to find a good non free converter.

You can take a look a this list of PDF converters: Converters




回答2:


Actually, the method I know is similar with you. But I found a simple method with 3rd party component from a blog post.

http://stephenchy520.blog.com/2011/11/24/how-to-convert-word-doc-to-pdf-for-cvb-net/

Maybe method in this post is helpful for you.




回答3:


I'd recommend automating Open Office to do the conversion. It's free, and in my experience far more stable than Microsoft Office when being used for automated conversion tasks.

See my answer on similar question here, which includes a code sample.



来源:https://stackoverflow.com/questions/6566974/convert-office-documents-to-pdf-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!