Home >Backend Development >C++ >How to Programmatically Convert Word Files to PDFs Efficiently and Inexpensively?

How to Programmatically Convert Word Files to PDFs Efficiently and Inexpensively?

Susan Sarandon
Susan SarandonOriginal
2025-01-26 13:41:14591browse

How to Programmatically Convert Word Files to PDFs Efficiently and Inexpensively?

Programmatically convert Word files to PDF: an efficient and economical method

Challenge

Converting Word (.doc) files to PDF format is a common need in many applications. However, finding a clean, cost-effective programming solution can be challenging. Open source options often lack SDKs, while proprietary solutions are often overpriced.

Solution

To address this challenge, here are two programming approaches:

Use a Foreach loop

This solution resolves an issue in previous code using a for loop. This problem can be solved using a foreach loop implementation code as shown below:

<code class="language-csharp">int j = 0;
foreach (Microsoft.Office.Interop.Word.Page p in pane.Pages)
{
    var bits = p.EnhMetaFileBits;
    var target = path1 + j.ToString() + "_image.doc";
    try
    {
        using (var ms = new MemoryStream((byte[])(bits)))
        {
            var image = System.Drawing.Image.FromStream(ms);
            var pngTarget = Path.ChangeExtension(target, "png");
            image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
        }
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    j++;
}</code>

Use Microsoft Word Interop

This solution utilizes Microsoft Word Interop to achieve the conversion:

<code class="language-csharp">using Microsoft.Office.Interop.Word;
using System;
using System.IO;

// ...其他using语句...

// 创建新的Microsoft Word应用程序对象
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

// C#没有可选参数,所以我们需要一个虚拟值
object oMissing = System.Reflection.Missing.Value;

// 获取指定目录中Word文件的列表
DirectoryInfo dirInfo = new DirectoryInfo(@"\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

word.Visible = false;
word.ScreenUpdating = false;

foreach (FileInfo wordFile in wordFiles)
{
    // 转换为Object类型以用于word Open方法
    Object filename = (Object)wordFile.FullName;

    // 使用虚拟值作为可选参数的占位符
    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;

    // 将文档保存为PDF格式
    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);

    // 关闭Word文档,但保持Word应用程序打开。
    // doc必须转换为_Document类型,以便找到正确的Close方法。
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
    doc = null;
}

// word必须转换为_Application类型,以便找到正确的Quit方法。
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;</code>

The above is the detailed content of How to Programmatically Convert Word Files to PDFs Efficiently and Inexpensively?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn