将文件.xlsx或.docx转换为.pdf [英] Convert file .xlsx or .docx to .pdf

查看:268
本文介绍了将文件.xlsx或.docx转换为.pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dears我是一名使用MVC的网站开发人员我想转换我已经上传的文件在我的本地电脑上工作正常但是当我在服务器上发布解决方案时它不起作用而且没有任何错误..



这是我的代码



我尝试了什么:



Dears i'm a web developer using MVC i want to convert files i have already uploaded it works fine on my local pc but when i publish solution on server it not working and not throw any errors..

this is my code

What I have tried:

public jsonResult Convertfiletopdf(string filename , string filePatgh)

    {
        try
        {

            filePatgh = Path.Combine(Server.MapPath("~/Uploads/"));
            if (filename.Contains(".xlsx"))
            {
                var newfilename = filename.Replace(".xlsx", ".pdf");
                var app = new Application();
                var wkb = app.Workbooks.Open(filePatgh + filename);
                if (!System.IO.File.Exists(filePatgh + newfilename))
                {                      
                    wkb.ExportAsFixedFormat(
                        XlFixedFormatType.xlTypePDF,
                        filePatgh + newfilename,
                        XlFixedFormatQuality.xlQualityStandard,
                        true, true, 1, 10, false);

                }
                wkb.Close();

            }
            if (filename.Contains(".xls"))
            {
                var newfilename = filename.Replace(".xls", ".pdf");
                var app = new Application();
                var wkb = app.Workbooks.Open(filePatgh + filename);

                if (!System.IO.File.Exists(filePatgh + newfilename))
                {
                    wkb.ExportAsFixedFormat(
                        XlFixedFormatType.xlTypePDF,
                        filePatgh + newfilename,
                        XlFixedFormatQuality.xlQualityStandard,
                        true,
                        true,
                        1,
                        10,
                        false);

                    // wkb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, filePatgh + newfilename);
                }
                wkb.Close();
            }
            else
            {

                var objWorPdf = new Word2Pdf();
                var fromLocation = filePatgh + "\\" + filename;
                var fileExtension = Path.GetExtension(filename);

                var changeExtension = filename.Replace(fileExtension, ".pdf");
                object toLocation = filePatgh + "\\" + changeExtension;
                objWorPdf.InputLocation = fromLocation;
                objWorPdf.OutputLocation = toLocation;
                if (!System.IO.File.Exists(filePatgh + changeExtension))
                {
                    objWorPdf.Word2PdfCOnversion();
                }



            }
            return Json(filename, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            throw ;
        }
    }

推荐答案

正如Richard所说,MS Office在这里不可用。但是还有一些选项,一些是免费的,一些是价格标签:

- OpenOffice / LibreOffice

Path\soffice.exe - 转换为pdf Path\Input.docx--outdirPath \Output folder

- TotalDocConverter

- PdfConvert

注意很多转换器承诺在服务器上工作,但失败了。根据输入文档,pdf文件可能与您的输入文件略有不同。

我建议您首先尝试OpenOffice / LibreOffice,并且只有当您不能忍受这些小问题时,尝试其他程序。
As Richard stated correctly, MS Office is not usable here. But there are some more options, some free, some with a price tag:
- OpenOffice / LibreOffice
"Path\soffice.exe" --convert-to pdf "Path\Input.docx" --outdir "Path\Output folder"
- TotalDocConverter
- PdfConvert
Note that many converters promise to work on the server, but fail. And depending on the input documents, the pdf files may look a little different from your input files.
I suggest you try OpenOffice/LibreOffice first, and only if you can't live with those minor problems, try other programs.


这篇关于将文件.xlsx或.docx转换为.pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆