【发布时间】:2018-03-21 12:36:55
【问题描述】:
好的,我有一个问题再次让我感到困惑。我有一些将 DOC 文件转换为 PNG 文件的代码。当我在本地主机上执行此操作时,图像很好。当我采用相同的代码并将其放在实时服务器上时,图像非常小(与我从中获取 DOC 文件的 DOT 文件大小相同,基本上 DOT 被填写并变成了 DOC)。现在......这是疯狂的部分。如果我以管理员身份登录托管服务器,然后访问实时网站,即使我从 iPhone 访问该网站,图像也会大而清晰。一旦我退出托管服务器并刷新实时页面,图像就会再次变小。这是我用来将 DOC 转换为 PNG 的代码。顺便说一句,如果我使用方法2,我可以使图像更大更高分辨率,但字体不合适。
private void ConvertDocToPNG(string startupPath, string filename1)
{
var docPath = Path.Combine(startupPath, filename1);
Application app = new Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
app.Visible = false;
doc = app.Documents.Open(docPath);
app.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMaximize;
app.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;
doc.ShowGrammaticalErrors = false;
doc.ShowRevisions = false;
doc.ShowSpellingErrors = false;
//Opens the word document and fetch each page and converts to image
foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
{
foreach (Microsoft.Office.Interop.Word.Pane pane in window.Panes)
{
for (var i = 1; i <= pane.Pages.Count; i++)
{
Microsoft.Office.Interop.Word.Page page = null;
bool populated = false;
while (!populated)
{
try
{
// This !@#$ variable won't always be ready to spill its pages. If you step through
// the code, it will always work. If you just execute it, it will crash. So what
// I am doing is letting the code catch up a little by letting the thread sleep
// for a microsecond. The second time around, this variable should populate ok.
page = pane.Pages[i];
populated = true;
}
catch (COMException ex)
{
Thread.Sleep(1);
}
}
var bits = page.EnhMetaFileBits;
var target = Path.Combine(startupPath + "\\", string.Format("{1}_page_{0}", i, filename1.Split('.')[0]));
try
{
using (var ms = new MemoryStream((byte[])(bits)))
{
var image = System.Drawing.Image.FromStream(ms);
var pngTarget = Path.ChangeExtension(target, "png");
// Method 2
image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
// Another way to save it using custom size
//float width = Convert.ToInt32(hfIdCardMaxWidth.Value);
//float height = Convert.ToInt32(hfIdCardMaxHeight.Value);
//float scale = Math.Min(width / image.Width, height / image.Height);
//int resizedWidth = (int)Math.Round(image.Width * scale);
//int resizedHeight = (int)Math.Round(image.Height * scale);
//Bitmap myBitmap = new Bitmap(image, new Size(resizedWidth, resizedHeight));
//myBitmap.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (System.Exception ex)
{
doc.Close(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(doc);
doc = null;
app.Quit(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(app);
app = null;
throw ex;
}
}
}
}
doc.Close(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(doc);
doc = null;
app.Quit(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(app);
app = null;
}
【问题讨论】:
-
假设您希望此代码以无人值守的方式运行是否安全?以这种方式使用 Word 互操作(或任何 MS office 互操作)是一个坏主意,但我们可以使用 OpenXML SDK 为您做一些事情。如果此代码以交互方式运行,则忽略此注释:)
-
确实是无人值守的时尚。
标签: c# asp.net office-interop