我发现了几个开源/免费软件程序,允许您将 .doc 文件转换为 .pdf 文件,但它们都是应用程序/打印机驱动程序类型,没有附加 SDK。

我发现有几个程序确实有一个 SDK,允许您将 .doc 文件转换为 .pdf 文件,但它们都是专有类型,许可证价格为 2,000 美元左右。

有谁知道有什么干净、便宜(最好是免费)的程序解决方案可以解决我的问题,使用 C# 或 VB.NET?

谢谢!

有帮助吗?

解决方案

使用foreach循环而不是for循环 - 它解决了我的问题。

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

这是对我有用的程序的修改。它使用Word 2007和另存为PDF加载项已安装。它在目录中搜索.doc文件,在Word中打开它们,然后将它们另存为PDF。请注意,您需要将Microsoft.Office.Interop.Word的引用添加到解决方案中。

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 list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

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

foreach (FileInfo wordFile in wordFiles)
{
    // 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;

其他提示

总结一下,对于vb.net用户来说,免费选项(必须安装office):

Microsoft Office 程序集下载:

  • Office 2010 的 pia
  • Office 2007 的 pia

  • 添加对 Microsoft.Office.Interop.Word.Application 的引用

  • 将 using 或 import (vb.net) 语句添加到 Microsoft.Office.Interop.Word.Application

VB.NET 示例:

        Dim word As Application = New Application()
        Dim doc As Document = word.Documents.Open("c:\document.docx")
        doc.Activate()
        doc.SaveAs2("c:\document.pdf", WdSaveFormat.wdFormatPDF)
        doc.Close()

PDFCreator 有一个COM组件,可从.NET或VBScript调用(下载中包含的示例) 。

但是,在我看来,打印机正是您所需要的 - 只需将其与 Word的自动化,你应该很高兴。

只是想补充说我使用的是Microsoft.Interop库,特别是我在这个帖子中没有看到的ExportAsFixedFormat函数。

using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Office.Core;Application app;

public string CreatePDF(string path, string exportDir)
{
    Application app = new Application();
    app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
    app.Visible = true;

    var objPresSet = app.Documents;
    var objPres = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

    var pdfFileName = Path.ChangeExtension(path, ".pdf");
    var pdfPath = Path.Combine(exportDir, pdfFileName);

    try
    {
        objPres.ExportAsFixedFormat(
            pdfPath,
            WdExportFormat.wdExportFormatPDF,
            false,
            WdExportOptimizeFor.wdExportOptimizeForPrint,
            WdExportRange.wdExportAllDocument
        );
    }
    catch
    {
        pdfPath = null;
    }
    finally
    {
        objPres.Close();
    }
    return pdfPath;
}

当有人用10000个单词文件转换为PDF以转换为PDF时,我经历了Word的痛苦。现在我用C#做了它并且使用了Word互操作但是如果我试图使用PC那么它很慢并且崩溃了......非常令人沮丧。

这让我发现我可以转储interops和它们的缓慢.....对于我使用的Excel(EPPLUS)然后我发现你可以获得一个名为Spire的免费工具,允许转换为PDF ...有限制!

http://www.e-iceblue。 COM /介绍/游离DOC-component.html#.VtAg4PmLRhE

这里似乎是一些相关的信息:

在ASP.NET中将MS Word文档转换为PDF

此外,由于Office 2007具有发布到PDF功能,我想您可以使用办公自动化在Word 2007中打开* .DOC文件并另存为PDF。我不是太热衷于办公自动化,因为它很慢而且容易挂起,但只是扔掉那里......

单词的Microsoft PDF加载项似乎是目前最好的解决方案,但您应该考虑到它没有正确地将所有word文档转换为pdf,在某些情况下,您会看到单词和输出之间的巨大差异PDF格式。不幸的是我找不到任何能正确转换所有word文档的api。 我发现确保转换100%正确的唯一解决方案是通过打印机驱动程序转换文档。缺点是文档排队并逐个转换,但您可以确定生成的pdf与word文档布局完全相同。 我个人更喜欢使用UDC(通用文档转换器)并在服务器上安装福昕阅读器(免费版),然后通过启动“处理”按钮来打印文档。并将其Verb属性设置为“print”。您还可以使用FileSystemWatcher在转换完成后设置信号。

使用 Microsoft.Office.Interop.Word 轻松编写代码和解决方案,以PDF格式转换WORD

using Word = Microsoft.Office.Interop.Word;

private void convertDOCtoPDF()
{

  object misValue = System.Reflection.Missing.Value;
  String  PATH_APP_PDF = @"c:\..\MY_WORD_DOCUMENT.pdf"

  var WORD = new Word.Application();

  Word.Document doc   = WORD.Documents.Open(@"c:\..\MY_WORD_DOCUMENT.docx");
  doc.Activate();

  doc.SaveAs2(@PATH_APP_PDF, Word.WdSaveFormat.wdFormatPDF, misValue, misValue, misValue, 
  misValue, misValue, misValue, misValue, misValue, misValue, misValue);

  doc.Close();
  WORD.Quit();


  releaseObject(doc);
  releaseObject(WORD);

}

添加此程序以释放内存:

private void releaseObject(object obj)
{
  try
  {
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
      obj = null;
  }
  catch (Exception ex)
  {
      //TODO
  }
  finally
  {
     GC.Collect();
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top