改篇 pdf 预览再本机没问题,发布再iis中 不行 ,(使用剪贴板的问题..excel和word 可以,)
pdf解决:请看我的博文 ----最终解决篇
详细配置及代码
word 转Html
1 /// <summary> 2 /// word转成html 3 /// </summary> 4 /// <param name="wordFileName"></param> 5 private void WordToHtml(object wordFileName,string htmlWord) 6 { 7 //在此处放置用户代码以初始化页面 8 Word.ApplicationClass word = new Word.ApplicationClass(); 9 Type wordType = word.GetType(); 10 Word.Documents docs = word.Documents; 11 //打开文件 12 Type docsType = docs.GetType(); 13 Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true }); 14 //转换格式,另存为 15 string a = doc.Comments.ToString(); 16 Type docType = doc.GetType(); 17 string wordSaveFileName = wordFileName.ToString(); 18 19 20 21 string strSaveFileName = htmlWord + "\\" + Path.GetFileNameWithoutExtension(wordSaveFileName) + ".html"; 22 23 object saveFileName = (object)strSaveFileName; 24 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML }); 25 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); 26 //退出 Word 27 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); 28 29 }
引入 Microsoft.Office.Interop.Word;
office转Html
/// <summary> /// Excel转成html /// </summary> public void ReadExcel(string rFilePath, string rHtmlFilePath) { Excel.Application repExcel = new Excel.Application(); Excel.Workbook workbook = null; //xlsFile为Excel文件路径 workbook = repExcel.Application.Workbooks.Open(rFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); //htmlFile 是要另存的html文件名 object ofmt = Excel.XlFileFormat.xlHtml; workbook.SaveAs(rHtmlFilePath, ofmt, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); object osave = false; workbook.Close(osave, Missing.Value, Missing.Value); }
引入Microsoft.Office.Interop.Excel;
pdf 转图片
说明:本方法 用 acrobat 的官方接口
需要安装 acrobat professional 8.0或更高版本
然后再vs中添加com组件 abode acrobat 版本号 type library 只有安装了上面软件 才回有
在web页 头部 添加 AspCompat="true" 控制台程序 要在main函数上加 [STAThread] 保证单线程模型下 才能访问 剪贴板
using System.IO;
using System.Reflection;
using DataHelp;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
/// <summary> /// Pdf导图片 /// </summary> /// <param name="pdfInputPathDirectory"></param> /// <param name="pngOutputPath"></param> /// <param name="format"></param> /// <returns></returns> public string PDFToPic(string pdfInputPathDirectory, string pngOutputPath, ImageFormat format) { StringBuilder b = new StringBuilder(); ImageFormat formate = format; // Acrobat objects Acrobat.CAcroPDDoc pdfDoc = null; Acrobat.CAcroPDPage pdfPage = null; Acrobat.CAcroRect pdfRect = null; Acrobat.CAcroPoint pdfPoint = null; try { string[] files = new string[] { pdfInputPathDirectory }; string dic = Path.GetDirectoryName(pdfInputPathDirectory); //string[] files = Directory.GetFiles(dic, "*.pdf"); for (int n = 0; n < files.Length; n++) { string inputFile = files[n].ToString(); string fileName = files[n].Substring(files[n].LastIndexOf(@"\") + 1).Replace(".pdf", ""); // Will always be available as .NET framework ships with all pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", ""); bool ret = pdfDoc.Open(inputFile); if (!ret) { throw new FileNotFoundException(); } else { b = b.AppendLine("<span>pdfDoc成功打开文件!!!</span>"); } // Get the number of pages (to be used later if you wanted to store that information) int pageCount = pdfDoc.GetNumPages(); b = b.AppendLine("<ul style='azimuth:center; list-style-type:none;' >"); for (int i = 0; i < pageCount; i++) { // Get the first page pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i); pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize(); b = b.AppendLine("<li> <span>pdfPoint.x=" + pdfPoint.x + ";pdfPoint.y=" + pdfPoint.y + "</span> </li>"); pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", ""); pdfRect.Left = 0; pdfRect.right = pdfPoint.x; pdfRect.Top = 0; pdfRect.bottom = pdfPoint.y; // Render to clipboard, scaled by 100 percent (ie. original size) // Even though we want a smaller image, better for us to scale in .NET // than Acrobat as it would greek out small text // see http://www.adobe.com/support/techdocs/1dd72.htm //清空剪贴板 //Clipboard.Clear(); //b = b.AppendLine("<li> <span>Clipboard.Clear();清空剪贴板 </span> </li>"); pdfPage.CopyToClipboard(pdfRect, 0, 0, 100); //b = b.AppendLine("<li> <span>pdfPage.CopyToClipboard(pdfRect, 0, 0, 100)=" +pdfPage.CopyToClipboard(pdfRect, 0, 0, 100)+ ";复制到剪贴板</span> </li>"); System.Windows.Forms.IDataObject clipboardData = Clipboard.GetDataObject(); bool c = false; if (clipboardData!=null ) { c = true; } b = b.AppendLine("<li> <span> Clipboard.GetDataObject()=" + c + "剪贴板中是否有数据;</span> </li>"); b = b.AppendLine("<li> <span>clipboardData.GetDataPresent(DataFormats.Bitmap)=" + clipboardData.GetDataPresent(DataFormats.Bitmap) + ";</span> </li>"); if (clipboardData.GetDataPresent(DataFormats.Bitmap)) { Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); b = b.AppendLine("<li> <span>生成图片的大小: pdfBitmap.Size.Height=" + pdfBitmap.Size.Height + "; pdfBitmap.Size.Width=" + pdfBitmap.Size.Width + "</span> </li>"); string dPath = Path.Combine(pngOutputPath, i.ToString("0000") + "." + formate.ToString()); b = b.AppendLine("<li> <span>生成图片的路径:" + dPath + ";</span> </li>"); pdfBitmap.Save( dPath, formate); pdfBitmap.Dispose(); } b = b.AppendLine("<li> <img src='..\\" + imgDire + "\\" + i.ToString("0000") + "." + formate.ToString() + "' /> </li><span>第" + (i + 1) + "页</span>"); } pdfDoc.Close(); // Not sure how why it is to do this, but Acrobat is not the best behaved COM object // see http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); } b = b.AppendLine("</ul>"); } catch (System.Exception ex) { Response.Write(ex.ToString()); } return b.ToString(); }
关于发布:
offic 组件发布问题
我的服务器环境是 windows servers 2008 64位操作系统
1、 在服务器上安装Office的Excel相关软件(推荐安装Office 2007);
2、 Cmd中输入命令 comexp.msc -32 启动组件服务(启动组件服务 方法 运行 mmc、mmc -32(32) 、DCOMCNFG.exe(64)) 不行一个一个
3、 依次双击"组件服务"->"计算机"->"我的电脑"->"DCOM配置";
4、 在"DCOM配置"中找到"Microsoft Excel Application",在它上面点击右键,然后点击"属性",弹出"Microsoft Excel应用程序属性"对话框;
5、 点击"标识"标签,选择 启动用户(默认选项) ;
6、 点击"安全"标签,在以下 三个项目中都选中 “自定义” 添加 ”everyone” 并分配最大权限(全选)
7、设置Web.config文件 在Web.config文件的<system.web>中加入<identity impersonate="true"/> (没有这一步 会报Excel进程无法打开相应的excel文件)。
Ok了
Word 配置同上。
关于pdf 发布: 需要在 服务器上安装 上诉软件
我在本机vs 和服务器vs中 测试没问题 ,发布到iis中 无法取到 剪贴板中的数据 也不报错 iis中应用程序池 的权限我也设置到最大 ,依然无果。
还是这种 iis中访问剪贴板的 方法 违反 安全性 或剪贴板只能在单线程模型下访问 与这个相悖 ????????????????
欢迎留言探讨》》》》》