提出问题提出问题
 

已答复Convert word doc to pdf

  • 2007年1月10日 11:51mrleokarthik 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi,

     I need to convert word document to pdf file using C#.2

     Iam having

    1. MS visual studio 2005 professional (and also MS VS.2003 enterprise ed)
    2. MS Office 2007
    3. I have downloaded SaveAsPDFandXPS (used to convert doc to pdf)
    4. Microsoft Visual Studio 2005 Tools for Office Second Edition Runtime (VSTO 2005 SE)

    My question is

    Do i need "MS visual studio 2005 for office tools" version to work with ms word, so that i can convert word to pdf. 

     

答案

  • 2007年1月12日 11:26mrleokarthik 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    hello everybody

    I myself find out answer for this

    Add reference to MS.Word

      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);
                }
            }

     

    Prerequisite:

    MS word2007 with (Primary Interoperability assembly  will be installed by default).

    plugin SaveAsPDFandXPS (free from MS Site)

     

    Karthik

  • 2007年1月21日 6:43Cindy MeisterMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    Hi "My Ruf Account"

    There is no VSTO interface for VS 2003 that you can use with Office 2007. The dll discussed above is VS 2005, only. The most you could do with VS 2003 is to automate Word 2007 from, for example, a Windows Forms application.

全部回复

  • 2007年1月10日 17:20Cindy MeisterMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi Chennai

    No, VSTO is not required to work automate the Word application. I've never tried it, but I'm told the ExportAsFixedFormat method should convert a Word 2007 document to pdf.

    If by "list out the namespaces required to work with the document" you mean the Help for the object model: The best way to access this is to start Word, press Alt+F11 to go into its IDE, then use the Help menu. This will take you to an online source.

    You can also press F2 in the IDE to bring up its object browser.

  • 2007年1月12日 11:26mrleokarthik 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    hello everybody

    I myself find out answer for this

    Add reference to MS.Word

      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);
                }
            }

     

    Prerequisite:

    MS word2007 with (Primary Interoperability assembly  will be installed by default).

    plugin SaveAsPDFandXPS (free from MS Site)

     

    Karthik

  • 2007年1月14日 11:51AmrJx 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    hello

    i like what you written it much much close to my style.

    but where can i find Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

    it's not there how can i add it.

    kindly can you describe it very clearly

     

    Yours

    A/\/\RJX

  • 2007年1月17日 7:47mrleokarthik 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

     

    Make sure you have reference to Word.12.

    It will automatically add Microsoft.Office.interop.word to your reference. Follow these  for other office application.

    (Note: you should have installed VS 2005 Tools for Office 2nd Ed. Runtime (VSTO 2005 SE) (x86)

    For PDF conversion Plugin- SaveAsPDFandXPS)

    PDF conversion only works with Lastest MS Office 2007 only

     

  • 2007年1月20日 7:36My Ruf Account 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hello Friends,

    Have you some stable solution to convert Word to Pdf.
    You r talking about VS2005 has one dll for that or class for that.

    But what for VS2003.
    Have you some solution for that, Have you got dll file for that.

    Also please can you find out that the dll used in VS2005 (which you use) is also usable for VS2003.
    and working Ok!!.

    If you got some other dll or any solution please reply.


  • 2007年1月21日 6:43Cindy MeisterMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    Hi "My Ruf Account"

    There is no VSTO interface for VS 2003 that you can use with Office 2007. The dll discussed above is VS 2005, only. The most you could do with VS 2003 is to automate Word 2007 from, for example, a Windows Forms application.

  • 2007年1月22日 8:31My Ruf Account 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hello Friends,

    First of all Thanks for Reply.
    Now the issue is,
    I want to this for my web project...
    here We uses VS2003.
    So Is there any alternative Solution for that?....

    Please If You have some idea then Reply....
  • 2007年1月22日 19:13Cindy MeisterMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    If you're targeting Office 2003, then make sure the documents are saved in Word 2003 XML file format. You can then transform the result to the PDF XML format. There are already third-party solutions available; searching the Internet should turn them up.

    This approach is really the only viable one for a server- or web-interface as it doesn't involve running the Word application.

  • 2007年10月13日 12:44neeleshk 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi Friends,

     

    I am also coverting word document into pdf and I am having Office 2007 and MS visual studio 2005

    I did the same as Karthik have mensioned above. But it gives me a exception below.

     

    System.Runtime.InteropServices.COMException (0x800A1066): Command failed

       at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Objec

    t& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles,

     Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts,

     Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELette

    r, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Objec

    t& LineEnding, Object& AddBiDiMarks)

       at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settin

    gs\neeleshk\Desktop\test11\ConsoleApplication2\ConsoleApplication2\Program.cs:li

    ne 3

     

  • 2007年10月13日 17:59Cindy MeisterMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    The PDF file converter is not installed as part of Office, due to licensing issues raised by Adobe. It must be downloaded and installed separately. Are you sure the file converter is correctly installed on the machine where this error is being raised?

     

  • 2007年10月15日 8:45neeleshk 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi Cindy,

     

    I am not using any kind of PDF file convertor rather i am trying to do this with C# code. and that code convert .doc to .rtf & .html without any error. and when i am trying it for PDF then it gives me exception like

    "The remote procedure call failed and did not execute. (Exception from HRESULT: 0x800706BF)"

     

    My steps are

    -          First I have converted  .doc to .rtf (converted successfully).

    -          Then I have tried .rtf to .pdf (gives exception as messioned above) and I have also tried to convert .doc to .pdf then it give me exceptions.

     

    Thanks,

    Neelesh

     

  • 2007年10月15日 14:48Cindy MeisterMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     建议的答复

    Hi Neelesh

     

    When Word saves a document to any file format other than its native *.doc format, it uses a file converter. This is true for RTF, for HTML, or for saving as plain text (just as examples). The difference between these file formats and PDF is that their file converters are installed by default when Office is installed. The PDF file converter is not part of the Office package. Originally, it was supposed to be, but Adobe threatened a lawsuit.

     

    Therefore, the PDF file converter must be downloaded and installed separately before you can use it. It doesn't matter whether you're automating Office or working as an end-user.

     

  • 2007年12月27日 3:50manzana 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

     

    I installed office 2003 on server and client, and use Microsoft.Office.Interop.Word to handle word by C#.But I can't start word on client.I'm not sure what's wrong with my codes? They just like:

    m_appWord = new Microsoft.Office.Interop.Word.Application();

    m_appWord.Visible = true;

    m_appWord.DisplayAlerts = WdAlertLevel.wdAlertsNone;

    m_bStartupApp = true;

    bResult = true;

     

    and so on...

    Please give me your suggestions.

    Thanks a lot

  • 2008年2月14日 21:59imatworknow 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Could you give some insight on how to call this?

    i.e.  Should this work?  word2PDF("filename.doc", "filename.pdf");

    Thank you.
  • 2008年3月7日 21:52Tim Ferro 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks for the help so far but I am getting an error when I call the line of code to save the file as pdf.
    The error says that the application is busy. Any thoughts?

    {"The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))"}
  • 2008年3月19日 12:12LarryOliver 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Guys!

    I advice you to try the PDF Metamorphosis .Net, it able to convert RTF to PDF:
    http://www.sautinsoft.com/components/pdf_metamorphosis_net.zip

    So, at first you may convert DOC to RTF by MS Office and next RTF to PDF by Metamorphosis.

    This is a sample code on VB.Net:

    Dim p As New SautinSoft.PdfMetamorphosis
    p.RtfToPdfConvertFile("D:\Example.rtf", "D:\File.pdf")


    C# sample:

    SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();
    p.RtfToPdfConvertFile("D:\Example.rtf", "D:\File.pdf");


    Best wishes,
    Max

  • 2008年8月12日 19:27AlbertLiang 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Not sure if this helps anyone but I recently had to convert Word 2003 docs to pdf. You basically print the document out as a postscript file, then take that file and run it through Adobe Acrobat Distiller.

     

    added a reference to COM lib "ACRODISTXLib" which is the Adobe Acrobat Distiller.

     

    private Word.Document _myWordDoc;

    private Word.Application _myWordApp;

    private string filename_template = "C:\\doc1.doc";

    private object filename_obj = (object)filename_template;

     

    private object missing = System.Reflection.Missing.Value; // our 'void' value

    private object printrange_obj = Word.WdPrintOutPages.wdPrintAllPages;

    private object true_obj = true;

     

    ...

     

    _myWordDoc= _myWordApp.Documents.Open( // load the template into a document workspace

    ref filename_obj, // and reference it through our myWordDoc

    ref missing,

    ref true_obj, // ReadOnly

    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

     

    string destination_ps = "C:\\doc1.ps";

    object destination_ps_obj = destination_ps;

    string destination_doc = "C:\\doc1.pdf";

     

    //save as pdf

    _myWordDoc.PrintOut(ref false_obj, ref printrange_obj, ref missing, ref destination_ps_obj,

    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref true_obj, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

     

    PdfDistillerClass distiller = new PdfDistillerClass();

    distiller.FileToPDF(destination_ps, destination_pdf, "");

  • 2008年9月17日 14:21fgozar 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi all,
    great post.

    I write my console application and it work fine on Xp and Vista.


    I also try to include this code in  a windows services application but it work fine in Xp and NOT in Vista.

     

    I try to go in debug mode and to analize object after "open" method, first to define assign document format:


     

    Code Snippet

     

    object Unknown = Type.Missing;

     

    Word.Document ObjDoc = newApp.Documents.Open(ref _Sources, 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);

     

    // Specifying the format in which you want the output file

    object format = Word.WdSaveFormat.wdFormatPDF;

     

     

    It seam me:

    Code Snippet
    "Il comando non è disponibile perché non c'è alcun documentoaperto" System.Runtime.InteropServices.ExternalException {System.Runtime.InteropServices.COMException}


    "Command not avaible because there isn't open document" System.Runtime.InteropServices.ExternalException {System.Runtime.InteropServices.COMException}

     

    ErrorCode -2146824040 


    There is someone that say me because on Vista it work as console application and not as windows services?

    thank's

  • 2009年3月6日 17:36Robert Taylor 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    You need to install the word converter. If you load Word, click 'SaveAs' and see an option to save  as XPS then you're ok. For Word 2007 go to this link...

     http://www.microsoft.com/downloads/thankyou.aspx?familyId=4d951911-3e7e-4ae6-b059-a2e79ed87041&displayLang=en
    Robert
  • 2009年4月4日 18:19Murali DotNet Developer 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks Robert, your solution helped me.
  • 2009年7月14日 14:37Kareem Koki 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     建议的答复

    i appeared to me message say value out of range?

    what i do in this  message?

  • 2009年7月14日 14:46Kareem Koki 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi "My Ruf Account" ?

    I get message for this code and say value Out of range ?

    What do i do in this message ?

    replay to me  fast ?

  • Segue link para gerar PDF utilizando C# e VSTO,

    http://rodrigo-romano.spaces.live.com/blog/cns!9E20A90BA8FC57D3!280.entry









    Rodrigo Alves Romano
    New Cocnept - Soluções Inovadoras
    http://www.nconcept.net
    Blog Técnico: http://rodrigo-romano.spaces.live.com