Eri*_*ess 200
使用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++;
}
Run Code Online (Sandbox Code Playgroud)
这是对我有用的程序的修改.它使用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;
Run Code Online (Sandbox Code Playgroud)
Elg*_*des 34
为vb.net用户总结一下,免费选项(必须安装办公室):
Microsoft office assembies下载:
添加对Microsoft.Office.Interop.Word.Application的引用
使用或导入(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()
Run Code Online (Sandbox Code Playgroud)
Mar*_*ett 14
PDFCreator有一个COM组件,可以从.NET或VBScript调用(下载中包含的示例).
但是,在我看来,打印机正是您所需要的 - 只需将其与Word的自动化相结合,您应该很高兴.
zet*_*eta 10
只是想补充说我使用了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;
}
Run Code Online (Sandbox Code Playgroud)
在Joel的论坛上有关于将Word转换为PDF的库的完整讨论.线程的一些建议:
小智 5
当有人用10000个单词文件转换为PDF以转换为PDF时,我经历了Word到PDF的痛苦.现在我用C#做了它并且使用了Word互操作但是如果我试图使用PC那么它很慢并且崩溃了......非常令人沮丧.
这让我发现我可以转储interops和它们的缓慢.....对于我使用的Excel(EPPLUS)然后我发现你可以获得一个名为Spire的免费工具,允许转换为PDF ...有限制!
http://www.e-iceblue.com/Introduce/free-doc-component.html#.VtAg4PmLRhE
Microsoft.Office.Interop.Word
用于将 WORD 转换为 PDF 的简单代码和解决方案
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);
}
Run Code Online (Sandbox Code Playgroud)
添加此过程以释放内存:
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
//TODO
}
finally
{
GC.Collect();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
300235 次 |
最近记录: |