将Word文件转换为PDF [英] Converting Word file to PDF

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

问题描述

我正在通过以下代码将Word文件转换为pdf:

I am converting word file to pdf by following code:

using System;

namespace DocConvert

{
      class DoctoRtf
      {
            static void Main()
            {

                //Creating the instance of Word Application
                Word.Application newApp = new Word.Application();

                // specifying the Source & Target file names
                object Source="c:\\abc\\Source.doc";
                object Target="c:\\abc\\Target.rtf";

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

                // Source document open here
                // Additional Parameters are not known so that are
                // set as a missing type
                newApp.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);

        // Specifying the format in which you want the output file
                object format = Word.WdSaveFormat.wdFormatRTF;

        //Changing the format of the document
                newApp.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);

                // for closing the application
                newApp.Quit(ref Unknown,ref Unknown,ref Unknown);

            }
      }
}


但要指出的是:


But at the point of:

newApp.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);


我收到一个错误,说价值超出范围.


I am getting an error saying value out of range. can any body help me?

推荐答案

您不能直接使用SaveAs转换为pdf.您可以使用PDFCreator *以编程方式将.doc转换为.pdf.

*为此,您需要在计算机上安装PDFCreator.

You can''t directly use SaveAs to convert to pdf. You can convert .doc to .pdf programatically using PDFCreator*.

*For this to work, you need to have PDFCreator installed on machine.

private PDFCreator.clsPDFCreator m_PDFCreator;
private void CreatePDF(string filename)
{
	//Check that the file is printable
	if (!m_PDFCreator.cIsPrintable(filename)) {
		throw new Exception(filename + " is not printable.");
	}
	//Check that the file exists
	if (System.IO.File.Exists(filename)) {
		string strDefaultPrinter = null;
		strDefaultPrinter = m_PDFCreator.cDefaultPrinter;
		m_PDFCreator.cDefaultPrinter = "PDFCreator";
		if (m_PDFCreator.cPrinterStop)
			m_PDFCreator.cPrinterStop = false;
		m_PDFCreator.cPrintFile(filename);
		m_PDFCreator.cDefaultPrinter = strDefaultPrinter;
	} else {
		throw new Exception(filename + " does not exist.");
	}
}



在VB中:



in VB:

  Private m_PDFCreator As PDFCreator.clsPDFCreator

  Private Sub CreatePDF(ByVal filename As String)
  'Check that the file is printable
  If Not m_PDFCreator.cIsPrintable(filename) Then
    Throw New Exception(filename & " is not printable.")
  End If
  'Check that the file exists
  If System.IO.File.Exists(filename) Then
    Dim strDefaultPrinter As String
    strDefaultPrinter = m_PDFCreator.cDefaultPrinter
    m_PDFCreator.cDefaultPrinter = "PDFCreator"
    If m_PDFCreator.cPrinterStop Then m_PDFCreator.cPrinterStop = False
    m_PDFCreator.cPrintFile(filename)
    m_PDFCreator.cDefaultPrinter = strDefaultPrinter
  Else
    Throw New Exception(filename & " does not exist.")
  End If
End Sub


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

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