gpt4 book ai didi

c# - 在C#中将doc转换为pdf

转载 作者:行者123 更新时间:2023-11-30 14:18:12 24 4
gpt4 key购买 nike

如何使用 asp.net c# 将 .doc 转换为 .pdf。我不能使用任何第三方组件。

代码应该在

  1. C# 或 vb.net
  2. 与 VS 2005 兼容。(如果不兼容,请发表您的回复,然后我会手动转换为 VS 2005)

如果有任何疑问,请告诉我。

谢谢!

最佳答案

private Microsoft.Office.Interop.Word.ApplicationClass MSdoc;       

//Use for the parameter whose type are not known or say Missing
object Unknown = Type.Missing;

private void word2PDF(object Source, object Target)
{ //Creating the instance of Word Application
if (MSdoc == null)MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();

try
{
MSdoc.Visible = false;
MSdoc.Documents.Open(ref Source, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
MSdoc.Application.Visible = false;
MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;

object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if (MSdoc != null)
{
MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
//WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
}
// for closing the application
WordDoc.Quit(ref Unknown, ref Unknown, ref Unknown);
}
}

先决条件:

  • MS word2007(默认安装主要互操作性程序集)。
  • 插件 SaveAsPDFandXPS (从 MS 站点免费)

确保您引用了 Word.12。它会自动将 Microsoft.Office.interop.word 添加到您的引用中。按照这些用于其他办公应用程序。(注意:您应该已经安装了 VS 2005 Tools for Office 2nd Ed. Runtime (VSTO 2005 SE) (x86)

关于c# - 在C#中将doc转换为pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4908819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com