在c#中将Doc自动化为PDF

时间:2011-02-17 14:40:51

标签: c# automation pdf-generation doc

我有大约200个单词文档,我需要pdf。

显然,我不能逐一pdf,因为首先需要花费很长时间,其次我肯定这样做是不好的做法。

我需要找到一种自动转换的方法,因为我们需要一次又一次。

我使用C#,但解决方案不一定必须在c#中,但它是首选。

我看过几个库,比如PDfCreator,Office 2007加载项,ITextSharp等等,论坛上没有任何明确的答案。

PDFCreator有c#sample,但它只适用于txt文件。 Office 2007添加没有文档锁定功能,这是自动化必须的。

以前有人实施过这样的场景吗?我希望你能听到你的建议。

提前致谢

问候

7 个答案:

答案 0 :(得分:3)

您可以在此博客文章中尝试此方法:

http://angrez.blogspot.com/2007/06/create-pdf-in-net-using-pdfcreator.html

答案 1 :(得分:3)

我这样做是为了自动将doc和docx文档转换为pdf:

private bool ConvertDocument(string file)
{
    object missing = System.Reflection.Missing.Value;

    OW.Application word = null;
    OW.Document doc = null;

    try
    {
        word = new OW.Application();
        word.Visible = false;
        word.ScreenUpdating = false;

        Object filename = (Object)file;

        doc = word.Documents.Open(ref filename, 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, ref missing, ref missing);
        doc.Activate();

        if (Path.GetExtension(file) == ".docx")
            file = file.Replace(".docx", ".pdf");
        else
            file = file.Replace(".doc", ".pdf");

        object fileFormat = OW.WdSaveFormat.wdFormatPDF;

        doc.ExportAsFixedFormat(file, OW.WdExportFormat.wdExportFormatPDF, false, OW.WdExportOptimizeFor.wdExportOptimizeForPrint,
            OW.WdExportRange.wdExportAllDocument, 1, 1, OW.WdExportItem.wdExportDocumentContent, true, true, OW.WdExportCreateBookmarks.wdExportCreateNoBookmarks,
            true, true, false, ref missing);
    }
    catch(Exception ex)
    {
        return false;
    }
    finally
    {
        if (doc != null)
        {              
            object saveChanges = OW.WdSaveOptions.wdDoNotSaveChanges;
            ((OW._Document)doc).Close(ref saveChanges, ref missing, ref missing);
            doc = null;
        }

        if (word != null)
        {
            ((OW._Application)word).Quit(ref missing, ref missing, ref missing);
            word = null;
        }
    }

    return true;
}

其中OW是Microsoft.Office.Interop.Word的别名。

答案 2 :(得分:2)

您是否已查看此MSDN article


编辑:

请注意这个“操作方法”示例不会按原样运行,因为:

  1. 由于某些原因,它会在第77行,第81行和第81行中的程序参数(ConvertDocCS.exe [sourceDoc] [targetDoc] [targetFormat])上运行。 #82。
  2. 我将项目转换为VS 2010,并且不得不重新引用Microsoft.Office.Core。这是一个名为Microsoft Office 12.0 Object Library的COM引用。
  3. 样本不包括相对路径。
  4. 我相信你会设法克服这些障碍:)


    最后一件事。如果你正在使用.NET 4,那么由于可选参数的奇迹,你不需要发送所有烦人的Missing.Value

答案 3 :(得分:1)

您可以尝试Aspose.Words for .NETconvert DOC files to PDF。它可以像任何其他.NET程序集一样在任何带有C#或VB.NET的.NET应用程序中使用。它也适用于任何Windows操作系统和32/64位系统。

披露:我在Aspose担任开发人员传播者。

答案 4 :(得分:0)

正如HuBeZa所说,如果您的工作站上安装了Word,您可以使用Word Automation逐个打开文件并将其另存为PDF。 您所需要的只是引用COM组件“Microsoft Word对象库”并使用此程序集的类。

执行时间可能会有点长,但您的转化会自动完成。

答案 5 :(得分:0)

我们可以为单词自动化设置字体,我将单个字体应用于我的解决方案中针对相同应用程序生成的所有文档 - 并节省了我的时间来手动进入每个模板并分别为每个标签和标题等设置字体。 。

 using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(input, true))
                {
                    // Get all content control elements
                    List<DocumentFormat.OpenXml.OpenXmlElement> elements =
                        wordProcessingDocument.MainDocumentPart.Document.Body.ToList();
                    // Get and set the style properties of each content control
                    foreach (var itm in elements)
                    {
                        try
                        {
                            List<RunProperties> list_runProperties = 
                                  itm.Descendants<RunProperties>().ToList();
                            foreach (var item in list_runProperties)
                            {
                                if (item.RunFonts == null)
                                    item.RunFonts = new RunFonts();

                                item.RunFonts.Ascii = "Courier New";
                                item.RunFonts.ComplexScript = "Courier New";
                                item.RunFonts.HighAnsi = "Courier New";
                                item.RunFonts.Hint = FontTypeHintValues.ComplexScript;
                            }
                        }
                        catch (Exception)
                        {
                            //continue for other tags in document 
                            //throw;
                        }
                    }
                    wordProcessingDocument.MainDocumentPart.Document.Save();
                }

答案 6 :(得分:-2)

我认为对此的直接回答是否定的! 但有可能通过解决方法,我建议使用imagemagik或某些库,看看它是否可以提供你的单词doc的图像,然后在itextsharp中使用这些图像来创建pdf