질문하기질문하기
 

답변됨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일 수요일 오후 5: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일 월요일 오후 7: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일 토요일 오후 5: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일 월요일 오후 2: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.

     

    • 답변으로 제안됨Robert Taylor 2009년 3월 6일 금요일 오후 5:34
    •  
  • 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일 목요일 오후 9: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일 금요일 오후 9: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일 화요일 오후 7: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일 수요일 오후 2: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일 금요일 오후 5: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일 토요일 오후 6:19Murali DotNet Developer 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Thanks Robert, your solution helped me.
  • 2009년 7월 14일 화요일 오후 2:37Kareem Koki 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     제안된 답변

    i appeared to me message say value out of range?

    what i do in this  message?

    • 답변으로 제안됨Kareem Koki 2009년 7월 14일 화요일 오후 2:41
    •  
  • 2009년 7월 14일 화요일 오후 2: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 ?

  • 2009년 10월 7일 수요일 오후 6:21Rodrigo Romano - CanalSharePoint.com.br 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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