高分巨型疑难问题,关于Office与PDF

cejay 2011-05-31 02:08:38
这个是真的遇到难题了,
现在需要一个功能,我作为一个小程序猿实在搞不定了,特来程序猿社区求助,救救小猿吧。

需求:

用asp.net做的项目,有一个功能要求是 上传word、excel、ppt、pdf 的时候需要在服务器生成一张文档的截图,
截图是截取文档第一页的一半。这个功能搞了两天了还没搞定。

求助
...全文
208 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cejay 2011-06-01
  • 打赏
  • 举报
回复
自己解决了:附上完整方案
前台页面:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成word缩略图" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="生成excel缩略图" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="生成ppt缩略图" />
<asp:Button ID="Button4" runat="server" Text="转换tif图片为jpg"
onclick="Button4_Click" />
<asp:Button ID="Button5" runat="server" Text="生成pdf缩略图"
onclick="Button5_Click" />


后台代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Management;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;

namespace CatchOffice
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
private void setprinter()
{
if (new PrintDocument().PrinterSettings.PrinterName != "Microsoft Office Document Image Writer")
{
bool isfindprinter = false;
query = new ManagementObjectSearcher(_classname);
queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (string.Compare(mo["Name"].ToString(), @"Microsoft Office Document Image Writer", true) == 0)
{
mo.InvokeMethod("SetDefaultPrinter", null);
isfindprinter = true;
break;
}
}
if (!isfindprinter)
{
throw new Exception("没有发现Microsoft Office Document Image Writer虚拟打印机 终止");
}
}
}

private ManagementObjectSearcher query;
private ManagementObjectCollection queryCollection;
string _classname = "SELECT * FROM Win32_Printer";


/// <summary>
/// word
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
setprinter();



Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();



object file = "E:\\自学资料\\ASP.NET_MVC_完全解析篇.docx";
object nullobj = System.Reflection.Missing.Value;
object oreadonly = true;


Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
ref file, ref nullobj, ref oreadonly
, ref nullobj, ref nullobj, ref nullobj, ref nullobj
, ref nullobj, ref nullobj, ref nullobj, ref nullobj
, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);


object outtiffile = "C:\\1.tif";

object copies = "1";
object pages = "1";
object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintCurrentPage;
object items = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
object oTrue = true;
object oFalse = false;
object missing = System.Reflection.Missing.Value;
object from = missing;
object to = missing;

doc.PrintOut(
ref oTrue, ref oFalse, ref range, ref outtiffile, ref from, ref to,
ref items, ref copies, ref pages, ref pageType, ref oTrue, ref oTrue,
ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
doc.Close(ref missing, ref missing, ref missing);
GC.Collect();
}





/// <summary>
/// excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Workbook mybook = excel.Workbooks.Add("G:\\Book1.xlsx");


Worksheet mysheet = (Microsoft.Office.Interop.Excel.Worksheet)mybook.Worksheets[1];
object oTrue = true;
object oFalse = false;
object outtiffile = "C:\\2.tif";
object missing = System.Reflection.Missing.Value;

mysheet.PrintOut(missing, missing, missing, oFalse, Type.Missing, oTrue, oFalse, outtiffile);//打印文件
mybook.Close(false, Type.Missing, Type.Missing);
excel.Quit();
mybook = null;
mysheet = null;
excel = null;
System.GC.Collect();
}


/// <summary>
/// ppt
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{
string sourceFile = "G:\\演示文稿2.pptx";

string outfile = "C:\\3.tif";
//定义powerpoint实例对象
Microsoft.Office.Interop.PowerPoint.Application varppt = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();

//定义powerpoint文档实例对象


Microsoft.Office.Interop.PowerPoint.Presentation ppt = varppt.Presentations.Open((string)sourceFile, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse);
ppt.PrintOptions.PrintInBackground = Microsoft.Office.Core.MsoTriState.msoTrue;
ppt.PrintOptions.OutputType = PpPrintOutputType.ppPrintOutputSlides; ppt.PrintOut(
1,
1,
outfile,
1, Microsoft.Office.Core.MsoTriState.msoTriStateMixed);
ppt.Close();
varppt.Quit();
System.GC.Collect();
}

protected void Button4_Click(object sender, EventArgs e)
{
string tifpath = "C:\\3.tif";
string jpgpath = "C:\\3.jpg";

System.IO.FileStream stream;

stream = File.OpenRead(tifpath);

Bitmap bmp = new Bitmap(stream);

System.Drawing.Image image = bmp;//得到原图

//创建指定大小的图

//System.Drawing.Image newImage = image.GetThumbnailImage(bmp.Width, bmp.Height, null, new IntPtr());
System.Drawing.Image newImage = image.GetThumbnailImage(50, 60, null, new IntPtr());


Graphics g = Graphics.FromImage(newImage);

g.DrawImage(newImage, 0, 0, newImage.Width, newImage.Height); //将原图画到指定的图上

g.Dispose();

stream.Close();

newImage.Save(jpgpath, ImageFormat.Jpeg);
newImage.Dispose();
}

protected void Button5_Click(object sender, EventArgs e)
{
IList<string> list = GenerateThumbnailImage("C:\\1.pdf", false, "C:\\pdf.jpg");
}

public IList<string> GenerateThumbnailImage(string InputFile, bool deletePDF, string filename)
{
return GenerateImage(InputFile, deletePDF, filename, "-dSAFER -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=1 -r150 -sDEVICE=jpeg -dGraphicsAlphaBits=4");
}



private IList<string> GenerateImage(string InputFile, bool deletePDF, string OutputFile, string Arguments)
{
IList<string> result = new List<string>();
string ExtOut = Path.GetExtension(OutputFile);
string partOut = OutputFile.Remove(OutputFile.Length - ExtOut.Length, ExtOut.Length);

OutputFile = partOut + ".jpg";
result.Add(OutputFile);
if (File.Exists(OutputFile))
{
File.Delete(OutputFile);
}

ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Normal;
//http://sourceforge.net/projects/ghostscript/files/
//info.WorkingDirectory = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"];
info.WorkingDirectory = @"C:\Program Files\gs\gs9.02\bin";
info.Arguments = Arguments + @" -sOutputFile=" + OutputFile + " " + InputFile;
info.FileName = @"gswin32c.exe";
Process subProcess = new Process();
subProcess.StartInfo = info;
subProcess.Start();
subProcess.WaitForExit(int.MaxValue);
if (deletePDF)
{
System.IO.File.Delete(InputFile);
}
return result;

}

}

}



office与pdf 截取第一页图片终于好了,哈
crackdung 2011-06-01
  • 打赏
  • 举报
回复
轉換到flash,非常簡單,而且通用。。。

網站


my blog
http://ufo-crackerx.blog.163.com/
失去乐山贼 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sp1234 的回复:]

程序猿就算了。一个团队如果不用外人,那么只能干个OA项目而已。
[/Quote]
+1
myhope88 2011-05-31
  • 打赏
  • 举报
回复
感觉不太好弄啊,帮顶下
weike021996 2011-05-31
  • 打赏
  • 举报
回复
学习学习
cejay 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wxr0323 的回复:]

PDF呢?
[/Quote]
PDF还没开始弄。。。
子夜__ 2011-05-31
  • 打赏
  • 举报
回复
PDF呢?

cejay 2011-05-31
  • 打赏
  • 举报
回复
对了还需要安装Microsoft Office Document Imaging
方法:http://support.microsoft.com/kb/926198/zh-cn
cejay 2011-05-31
  • 打赏
  • 举报
回复
已经成功生成出word 第一页图片:
代码:

using System;
using System.Drawing.Printing;
using System.Management;

namespace CatchOffice
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
private void setprinter()
{
if (new PrintDocument().PrinterSettings.PrinterName != "Microsoft Office Document Image Writer")
{
query = new ManagementObjectSearcher(_classname);
queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (string.Compare(mo["Name"].ToString(), @"Microsoft Office Document Image Writer", true) == 0)
{
mo.InvokeMethod("SetDefaultPrinter", null);
break;
}
}
}
}

private ManagementObjectSearcher query;
private ManagementObjectCollection queryCollection;
string _classname = "SELECT * FROM Win32_Printer";


/// <summary>
/// word
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
setprinter();

Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object file = "E:\\XXXXX.docx";
object nullobj = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
ref file, ref nullobj, ref nullobj
, ref nullobj, ref nullobj, ref nullobj, ref nullobj
, ref nullobj, ref nullobj, ref nullobj, ref nullobj
, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);


object outtiffile = "C:\\1.tif";

object copies = "1";
object pages = "1";
object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintCurrentPage;
object items = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
object oTrue = true;
object oFalse = false;
object missing = System.Reflection.Missing.Value;

doc.PrintOut(
ref oTrue, ref oFalse, ref range, ref outtiffile, ref missing, ref missing,
ref items, ref copies, ref pages, ref pageType, ref oTrue, ref oTrue,
ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
GC.Collect();
}





/// <summary>
/// excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{

}


/// <summary>
/// ppt
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{

}

}

}


除了引用office之外 还要引用System.Management
baobingbing2 2011-05-31
  • 打赏
  • 举报
回复
这想法不错啊,关注、、
心灵彩虹 2011-05-31
  • 打赏
  • 举报
回复
幽默很[Quote=引用 1 楼 sp1234 的回复:]
程序猿就算了。一个团队如果不用外人,那么只能干个OA项目而已。
[/Quote]
War3_Fan 2011-05-31
  • 打赏
  • 举报
回复
截图?真能折磨人
xiuyer 2011-05-31
  • 打赏
  • 举报
回复
关注。。。。帮顶。我也很想知道这个该怎么做。
cejay 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mockqi 的回复:]

到这看看
http://topic.csdn.net/u/20090304/14/833e7d98-07bf-4b72-a344-ef91bfa6812a.html
[/Quote]
谢啦,看过了,这个是截取屏幕,我怕服务器同时上传好多文档的时候会出问题,我再试试看
cejay 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sp1234 的回复:]

程序猿就算了。一个团队如果不用外人,那么只能干个OA项目而已。
[/Quote]
唉。。。
cejay 2011-05-31
  • 打赏
  • 举报
回复
还有,就是office 是有2003版和2007版的
  • 打赏
  • 举报
回复
程序猿就算了。一个团队如果不用外人,那么只能干个OA项目而已。

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧