发布网友 发布时间:2022-04-22 09:52
共1个回答
热心网友 时间:2023-10-09 08:16
using System; using MSWord = Microsoft.Office.Interop.Word; using System.IO; using System.Reflection; using org.pdfbox.pdmodel; using org.pdfbox.util; namespace TestApplication { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { object path; //文件路径 string strContent; //文本内容 MSWord.Application wordApp; //word应用程序 MSWord.Document wordDoc; //word文档 path = @"c:\test.docx"; wordApp = new MSWord.ApplicationClass(); if (File.Exists(path.ToString())) { File.Delete(path.ToString()); } Object nothing = Missing.Value; wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing); strContent = pdf2txt(new FileInfo(@"C:\Documents and Settings\Administrator\桌面\临时文件\qiu\WebApplication1\WebApplication1\test.pdf")); wordDoc.Paragraphs.Last.Range.Text = strContent; object format = MSWord.WdSaveFormat.wdFormatDocumentDefault; wordDoc.SaveAs(ref path, ref format, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing); wordDoc.Close(ref nothing, ref nothing, ref nothing); wordApp.Quit(ref nothing, ref nothing, ref nothing); } public string pdf2txt(FileInfo file) { PDDocument doc = PDDocument.load(file.FullName); PDFTextStripper pdfStripper = new PDFTextStripper(); string text = pdfStripper.getText(doc); return text; } } } 需要下载PDFBox-0.7.3.dll,并且把此dll同级的所有dll全部添加引用 类WordConvertPdf using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using Microsoft.Office.Interop.Word; namespace HouseManager.BaseWeb { public class BaseUI : System.Web.UI.Page { #region word转Pdf文件的方法 /// <summary> /// word转Pdf文件的方法 /// </summary> /// <returns>转换后文件的路径</returns> public string WordConvertPdf(string fileName) { ApplicationClass word = new ApplicationClass(); Type wordType = word.GetType(); //打开WORD文档 Documents docs = word.Documents; Type docsType = docs.GetType(); object objDocName = AppDomain.CurrentDomain.BaseDirectory + @"\" + fileName; Document doc = (Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true }); //打印输出到指定文件 Type docType = doc.GetType(); string guid = Guid.NewGuid().ToString(); object printFileName = AppDomain.CurrentDomain.BaseDirectory + @"\" + guid + ".ps"; docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, WdPrintOutRange.wdPrintAllDocument, printFileName }); //退出WORD wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); //object o1 = AppDomain.CurrentDomain.BaseDirectory + @"\DocToPdf.ps"; object o2 = AppDomain.CurrentDomain.BaseDirectory + @"\" + guid + ".pdf"; object o3 = ""; //引用将PS转换成PDF的对象 //可以使用 pdfConvert.FileToPDF("c: \\ test.ps ","c:\\test.pdf","");这样的转换方法,只是为了保持与WORD相同的调用方式 try { ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass(); Type pdfType = pdf.GetType(); pdfType.InvokeMember("FileToPDF", System.Reflection.BindingFlags.InvokeMethod, null, pdf, new object[] { printFileName, o2, o3 }); pdf = null; } catch (Exception ex) { Console.WriteLine(ex.Message); } //为防止本方法调用多次时发生错误,必须停止acrodist.exe进程 foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses()) { int begpos; int endpos; string sProcName = proc.ToString(); begpos = sProcName.IndexOf("(") + 1; endpos = sProcName.IndexOf(")"); sProcName = sProcName.Substring(begpos, endpos - begpos); if (sProcName.ToLower().CompareTo("acrodist") == 0) { try { proc.Kill(); //停止进程 } catch (Exception ex) { Console.WriteLine(ex.Message); } break; } } return o2.ToString(); } #endregion } } pdf转word using System; using MSWord = Microsoft.Office.Interop.Word; using System.IO; using System.Reflection; using org.pdfbox.pdmodel; using org.pdfbox.util; namespace TestApplication { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { object path; //文件路径 string strContent; //文本内容 MSWord.Application wordApp; //word应用程序 MSWord.Document wordDoc; //word文档 path = @"c:\test.docx"; wordApp = new MSWord.ApplicationClass(); if (File.Exists(path.ToString())) { File.Delete(path.ToString()); } Object nothing = Missing.Value; wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing); strContent = pdf2txt(new FileInfo(@"C:\Documents and Settings\Administrator\桌面\临时文件\qiu\WebApplication1\WebApplication1\test.pdf")); wordDoc.Paragraphs.Last.Range.Text = strContent; object format = MSWord.WdSaveFormat.wdFormatDocumentDefault; wordDoc.SaveAs(ref path, ref format, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing); wordDoc.Close(ref nothing, ref nothing, ref nothing); wordApp.Quit(ref nothing, ref nothing, ref nothing); } public string pdf2txt(FileInfo file) { PDDocument doc = PDDocument.load(file.FullName); PDFTextStripper pdfStripper = new PDFTextStripper(); string text = pdfStripper.getText(doc); return text; } } } 需要下载PDFBox-0.7.3.dll,并且把此dll同级的所有dll全部添加引用