Convert single doc file to pdf

前端 未结 3 1728
别那么骄傲
别那么骄傲 2021-02-06 18:43

I am using following code How do I convert Word files to PDF programmatically? to convert the doc file to pdf. but the code mentions getting all .doc files from specific directo

相关标签:
3条回答
  • 2021-02-06 19:32

    HI,

    i think these links will help you a lot:

    https://stackoverflow.com/questions/891531/convert-xls-doc-files-to-pdf-with-c

    http://www.codeproject.com/KB/cs/convertdocintootherformat.aspx

    0 讨论(0)
  • 2021-02-06 19:40

    The same code, modified for one single file:

    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    ...
    
    // Create a new Microsoft Word application object
    Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
    
    // C# doesn't have optional arguments so we'll need a dummy value
    object oMissing = System.Reflection.Missing.Value;
    
    // Get a Word file
    FileInfo wordFile = new FileInfo("myDoc.doc");
    
    word.Visible = false;
    word.ScreenUpdating = false;
    
    // Cast as Object for word Open method
    Object filename = (Object)wordFile.FullName;
    
    // Use the dummy value as a placeholder for optional arguments
    Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    doc.Activate();
    
    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;
    
    // Save document into PDF Format
    doc.SaveAs(ref outputFileName,
        ref fileFormat, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
    // 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 oMissing, ref oMissing);
    doc = null;
    
    // word has to be cast to type _Application so that it will find
    // the correct Quit method.
    ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
    word = null;
    
    0 讨论(0)
  • 2021-02-06 19:42

    The code is looking for all .doc files in a folder and looping through them. If you jut had the one file in the folder, it would just convert that one.

    You could change this line of code:

    FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
    

    to just look for your file, eg

    FileInfo[] wordFiles = dirInfo.GetFiles("myselectedfile.doc");
    
    0 讨论(0)
提交回复
热议问题