62,243
社区成员




<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;
}
}
}
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)
{
}
}
}