public int getpage(object file) { try { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); object nullobj = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Document doc = app.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); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); IDataObject data = Clipboard.GetDataObject(); // get number of pages Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages; int pages = doc.ComputeStatistics(stat, Type.Missing); //string text = data.GetData(DataFormats.Text).ToString(); //获取具体内容,此项目不需要 doc.Close(ref nullobj, ref nullobj, ref nullobj); app.Quit(ref nullobj, ref nullobj, ref nullobj); return(pages); } catch (Exception ex) { ex.ToString(); return(0); } }
public List <string> readDocFile(string path) { List <string> data = new List <string>(); try { Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); object fileName = path; object missing = System.Type.Missing; object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage; object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute; object Start; object End; object CurrentPageNumber; object NextPageNumber; object Miss = System.Reflection.Missing.Value; doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); Microsoft.Office.Interop.Word.WdStatistic PagesCountStat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages; int PagesCount = doc.ComputeStatistics(PagesCountStat, ref missing); for (int Index = 1; Index <= PagesCount; Index++) { CurrentPageNumber = (Convert.ToInt32(Index.ToString())); NextPageNumber = (Convert.ToInt32((Index + 1).ToString())); //Lay vi tri bat dau cua trang Start = word.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref Miss).Start; //Lay vi tri cuoi cua trang End = word.Selection.GoTo(ref What, ref Which, ref NextPageNumber, ref Miss).End; //Lay text ra if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString())) { data.Add(doc.Range(ref Start, ref End).Text); } else { data.Add(doc.Range(ref Start).Text); } } doc.Close(); word.Quit(); } catch (Exception) { data = null; } return(data); }
/// <summary> /// 获取Word文件页数【格式为doc,docx】 /// </summary> public int GetWordPageCount(object filePath) { Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); object oMissing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Document myWordDoc = myWordApp.Documents.Open( ref filePath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); int pages = myWordDoc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, ref oMissing); myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing); myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing); return(pages); }
public static int WriteTiffPages(YellowstonePathology.Business.OrderIdParser orderIdParser, long reportDistributionLogId) { Microsoft.Office.Interop.Word.Application oWord; Object oMissing = System.Reflection.Missing.Value; Object oTrue = true; Object oFalse = false; oWord = new Microsoft.Office.Interop.Word.Application(); oWord.Visible = true; string currentPrinter = oWord.ActivePrinter; oWord.ActivePrinter = "Microsoft Office Document Image Writer"; Object docFileName = YellowstonePathology.Document.CaseDocumentPath.GetPath(orderIdParser) + orderIdParser.ReportNo + ".doc"; Object fileFormat = "wdFormatDocument"; Microsoft.Office.Interop.Word.Document doc = oWord.Documents.Open(ref docFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); object oOutputFile; object oPages; object oRange = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintRangeOfPages; int pages = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, ref oTrue); for (int i = 1; i <= pages; i++) { oPages = i.ToString(); oOutputFile = YellowstonePathology.Document.CaseDocumentPath.GetPath(orderIdParser) + @"Centricity\" + reportDistributionLogId.ToString() + "." + i.ToString().PadLeft(3, '0'); doc.PrintOut(ref oFalse, ref oFalse, ref oRange, ref oOutputFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oPages, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); } oWord.ActivePrinter = currentPrinter; CaseDocument.ReleaseComObject(oRange); CaseDocument.ReleaseComObject(doc); oWord.Quit(ref oFalse, ref oMissing, ref oMissing); CaseDocument.ReleaseComObject(oWord); return(pages); }
public void Merge() // Shall be called on a separate thread and shall make use of the member variables { object missing = System.Type.Missing; object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage; object outputFile = outputFilePath; // Create a new Word application Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application(); bSuccessfulOperation = true; try { // Create a new file Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.Add( ref missing , ref missing , ref missing , ref missing); // Make a Word selection object. Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection; //Count the number of documents to insert; int documentCount = fileList.Length; //A counter that signals that we shoudn't insert a page break at the end of document. int breakStop = 0; int iCounter = 0; int iFileCount = fileList.Length; // Iterate through the entire list of files foreach (string file in fileList) { Console.WriteLine("Merging file: " + file); breakStop++; // Insert the files to our template selection.InsertFile( file , ref missing , ref missing , ref missing , ref missing); //Do we want page breaks added after each documents? if (insertPageBreaks && breakStop != documentCount) { selection.InsertBreak(ref pageBreak); } iCounter++; UpdateProgressBar(((float)iCounter / iFileCount) * 100); } // Save the document to it's output file. wordDocument.SaveAs( ref outputFile , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing , ref missing); // Get some stats about the merged file lineCount = wordDocument.Sentences.Count; wordCount = wordDocument.Words.Count; Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages; pageCount = wordDocument.ComputeStatistics(stat, ref missing); wordDocument.Save(); // Clean up! wordDocument = null; } catch (Exception ex) { //I didn't include a default error handler so i'm just throwing the error Console.WriteLine(ex); bSuccessfulOperation = false; } finally { // Finally, Close our Word application wordApplication.Quit(ref missing, ref missing, ref missing); } DisplayResult(); // Done, show the results }
private void openFileDialog_FileOk(object sender, CancelEventArgs e) { try { var fileInfo = new System.IO.FileInfo(openFileDialog.FileName); // If the file is bigger than 30 mb if (fileInfo.Length * (9.35 * Math.Pow(10, -7)) > 30) MessageBox.Show("This is a relatively large file. Please allow load times in between 1 - 2 minutes.", "BIG FILE", MessageBoxButtons.OK, MessageBoxIcon.Information); loadedDoc = wordApp.Documents.Open(openFileDialog.FileName); tmpDoc = wordApp.Documents.Add(); loadedDoc.Content.Copy(); tmpDoc.Content.Paste(); hasLoaded = true; Size = new Size(Screen.GetWorkingArea(Bounds).Width, Screen.GetWorkingArea(Bounds).Height); Location = new Point(0, 0); lblLoadedFile.Text = "Loaded Doc: " + loadedDoc.Path + "\\" + loadedDoc.Name; stslblFileLoaded.Text = "File Loaded: True"; pgNums = loadedDoc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages); lblPagesInDoc.Text = "Pages in Document: " + pgNums.ToString(); pdfReader.Visible = true; SaveTempPDF(); pdfReader.Refresh(); System.Media.SystemSounds.Beep.Play(); } catch (System.Runtime.InteropServices.COMException ex) { PrintErrorMessage("ERROR! The document failed to load. Either" + " the document is in use or you don't have permission to open it.", ex); } catch (Exception ex) { PrintErrorMessage("ERROR! Could not open the file.", ex); } }
public void replaceContent(List <Segment> listSegs, string path) { try { Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); object fileName = path; object missing = System.Type.Missing; object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage; object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute; object Start; object End; object CurrentPageNumber; object NextPageNumber; object Miss = System.Reflection.Missing.Value; doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); Microsoft.Office.Interop.Word.WdStatistic PagesCountStat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages; int PagesCount = doc.ComputeStatistics(PagesCountStat, ref missing); for (int Index = 1; Index <= PagesCount; Index++) { CurrentPageNumber = (Convert.ToInt32(Index.ToString())); NextPageNumber = (Convert.ToInt32((Index + 1).ToString())); List <Segment> listSegInPage = new List <Segment>(); listSegInPage = getListSegsInPage(listSegs, Index); //Lay vi tri bat dau cua trang Start = word.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref Miss).Start; //Lay vi tri cuoi cua trang End = word.Selection.GoTo(ref What, ref Which, ref NextPageNumber, ref Miss).End; if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString())) { foreach (Segment seg in listSegInPage) { tm tmp = seg.getTM(); if (tmp.Target != null) { if (tmp.Target.Trim() != "") { findAndReplace(doc.Range(ref Start, ref End), tmp.Source, tmp.Target); } } } } else { foreach (Segment seg in listSegInPage) { tm tmp = seg.getTM(); if (tmp.Target != null) { if (tmp.Target.Trim() != "") { findAndReplace(doc.Range(ref Start), tmp.Source, tmp.Target); } } } } //Lay text ra //if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString())) //{ // data.Add(doc.Range(ref Start, ref End).Text); //} //else //{ // data.Add(doc.Range(ref Start).Text); //} } doc.Close(); word.Quit(); } catch (Exception) { } }