c# - Word 到 PDF 转换后的 PDF 出现错误 - 拒绝访问错误

标签 c# pdf ms-word itext

enter image description here将 MS word 文档转换为 PDF 后。如果我将相同的转换字路径发送到 pdf。即 PDF 。在提取文件时,在服务器中发布网站时我会收到访问被拒绝的错误。它在本地机器上工作。是否有任何我留在代码中或是否有任何我需要安装的东西。

 public string WordtoPdf_Input(string wordFileName_input)
    {

        try
        {

            Microsoft.Office.Interop.Word.Application appWord_input = new Microsoft.Office.Interop.Word.Application();
            object _MissingValue_ip = System.Reflection.Missing.Value;
            //filename_doc = System.IO.Path.GetFileName(LblFleip.Text);
            //wordFileName = LblFleip.Text;
            string pdfFileName = string.Empty;
            appWord_input.Visible = false;
            appWord_input.ScreenUpdating = false;

            // Cast as Object for word Open method
            object filename = (object)wordFileName_input;

            // Use the dummy value as a placeholder for optional arguments
            Microsoft.Office.Interop.Word.Document doc = appWord_input.Documents.Open(ref filename, ref _MissingValue_ip,
             ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip,
             ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip,
             ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip);
            doc.Activate();

            InputFilename = pdfFileName = Path.ChangeExtension(wordFileName_input, ".pdf");
            object fileFormat = WdSaveFormat.wdFormatPDF;

            //All is well until here, Save thinks the excelfile is readonly
            object tmpName = Path.GetTempFileName();

            File.Delete(tmpName.ToString());

            // Save document into PDF Format
            doc.SaveAs(ref tmpName,
             ref fileFormat, ref _MissingValue_ip, ref _MissingValue_ip,
             ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip,
             ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip,
             ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip);

            // Close the Word document, but leave the Word application open.
            // doc has to be cast to type _Document so that it will find the
            // correct Close method.    

            object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
            ((_Document)doc).Close(ref saveChanges, ref _MissingValue_ip, ref _MissingValue_ip);
            doc = null;

            // word has to be cast to type _Application so that it will find
            // the correct Quit method.
            ((_Application)appWord_input).Quit(ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip);
            appWord_input = null;


            File.Delete(InputFilename.ToString());

            File.Move(tmpName.ToString(), InputFilename.ToString());
            //File.Move(tmpName,InputFilename.ToString())
            filePath_input = InputFilename.ToString();


            GC.Collect();
            GC.WaitForPendingFinalizers();

        }


        catch (Exception ex)
        {

        }
        finally
        {
            GC.Collect();  // force final cleanup!
            GC.WaitForPendingFinalizers();
        }
        return filePath_input;
    }

Public void PDFnumofPagecount()
{
 LblFleip.Text = filePath_input;





                PdfReader readerPages_ip = new PdfReader(LblFleip.Text);
  NumberofPages_ip = readerPages_ip.NumberOfPages;
                txtbNumberofPages_ip.Text = NumberofPages_ip.ToString();
                readerPages_ip.Close();

}

i have used itextsharp for reading pages and extraction of PDF

最佳答案

您是否在出现错误的机器上检查了用户的权限(以确保他们具有所需的读/写权限)?

关于c# - Word 到 PDF 转换后的 PDF 出现错误 - 拒绝访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17743891/

相关文章:

java - 如何使用 PDFBox 2.0.0 从 PDF 文件签署 InputStream

html - 如何将HTML文件转换成word?

vba - 从 Word 使用 VBA 发送 HTTP 请求

excel - 如何从 "horizontal"单元格范围填充组合框?

c# - 流阅读器无法将文本从文件文本获取到文本框

c# - 这是对返回 JSON 数据的动态关键字的正确使用吗?

javascript - 提供的数据不是 ReactJS 中有效的 base64 字符串 jsPDF

java - PDFBox convertToImage 无法正确呈现某些 PDF

c# - 在 MVC4 中使用 HtmlHelpers

c# - UINT8 中的自定义符号 - 如何转换为 C#?