从具有多个页面的多个单词文档中读取并通过使用C#仅选择包含特定单词的特定页面来创建PDF。

问题描述 投票:0回答:1
我需要使用C#和office Interop库阅读多个带有多个页面的单词文档,检查每个页面是否包含特定单词,如果是,则从多个文档中添加包含相同单词的所有页面,并创建一个新文档并另存为pdf。例如,假设您在Word文档中每个月都有多个产品详细信息。例如香蕉,苹果,橙子等的详细信息。但是页面将​​仅包含有关一种产品的详细信息。因此,该程序将处理几个月的多个文档,并为每个产品创建单独的PDF。因此,到今天结束时,您将获得一个包含香蕉的香蕉详细信息的PDF格式,另一个包含苹果的PDF等等。我在这里查看了现有线程,并提出了一个原型,如下所示。我仍然有几个问题。1.如何扫描范围是否有特定单词,例如:在我们的示例中为Banana2.如何在一个文档中循环多个页面?我可以提取一个范围并创建一个PDF,但是当我尝试处理多个范围时,获取页数会替换使用word.Selection.Paste();时的先前内容。因此,我的pdf只能以一页显示。3.如何制作pdf Landscape和A5,每页之后都会有一个分页符。

我的示例程序

//创建一个新的Microsoft Word应用程序对象 Microsoft.Office.Interop.Word.Application字=新的Microsoft.Office.Interop.Word.Application(); word.Visible = true; // C#没有可选参数,因此我们需要一个虚拟值 对象oMissing = System.Reflection.Missing.Value; //获取指定目录下的Word文件列表 DirectoryInfo dirInfo =新的DirectoryInfo(@“ C:\ temp”); FileInfo [] wordFiles = dirInfo.GetFiles(“ *。doc”); //word.Visible = false; //word.ScreenUpdating = false; foreach(wordFiles中的FileInfo wordFile) { 如果(!wordFile.FullName.Contains(“ $”)) { //转换为Open方法的对象 对象文件名=(Object)wordFile.FullName; //将虚拟值用作可选参数的占位符 Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filename,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); //doc.Activate(); 对象是什么= WdGoToItem.wdGoToPage; 对象,其中= WdGoToDirection.wdGoToFirst; 对象计数= 1; 范围startRange = word.Selection.GoTo(ref what,ref which,ref count,ref oMissing); 对象count2 =(int)count +1; 范围endRange = word.Selection.GoTo(ref what,ref which,ref count2,ref oMissing); endRange.SetRange(startRange.Start,endRange.End-1); endRange.Select(); word.Selection.Copy(); //word.Documents.Close(); //word.Quit(); word.Documents.Add(); word.Selection.Paste(); //Microsoft.Office.Interop.Word.Application word1 =新的Microsoft.Office.Interop.Word.Application(); 对象outputFileName = wordFile.FullName.Replace(“。docx”,“ .pdf”); 对象fileFormat = WdSaveFormat.wdFormatPDF; //将文档保存为PDF格式 word.ActiveDocument.SaveAs(ref outputFileName, ref fileFormat,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); //关闭Word文档,但保持Word应用程序打开。 //必须将doc强制转换为_Document类型,以便找到 //正确的Close方法。 对象saveChanges = WdSaveOptions.wdDoNotSaveChanges; word.Documents.Close(ref saveChanges,ref oMissing,ref oMissing); doc = null; releaseObject(doc); } } //必须将单词强制转换为_Application类型,以便找到 //正确的Quit方法。 ((_Application)word).Quit(ref oMissing,ref oMissing,ref oMissing); 字=空; releaseObject(word);

另一种尝试循环并检查当前页面是否包含特定文本的尝试。但是两者都不起作用。  

long pageCount = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages); 对象是什么= WdGoToItem.wdGoToPage; 对象,其中= WdGoToDirection.wdGoToFirst; object count = 0; object count2 = (int)count + 1; for (long i = 1; i < pageCount; i++) { count = (int)count + 1; Range startRange = word.Selection.GoTo(ref what, ref which, ref count, ref oMissing); count2 = (int)count + 1; Range endRange = word.Selection.GoTo(ref what, ref which, ref count2, ref oMissing); endRange.SetRange(startRange.Start, endRange.End - 1); endRange.Select(); word.Selection.Copy(); word.Documents.Add(); word.Selection.Paste(); if (word.Selection.Find.Execute("Something")) { word.Selection.Copy(); word.Documents.Add(); word.Selection.Paste(); } }

感谢任何帮助。预先谢谢你。

我需要使用C#和office Interop库阅读多个单词文档,每个单词文档具有多个页面,请检查每个页面是否包含特定单词,如果是,则添加所有包含相同单词的页面...

c# pdf word multiple
1个回答
0
投票
不用担心。我知道了一切。因此,我在这里回答我自己的问题,因为它可以帮助遇到相同问题的其他人。问题是,当您同时循环和添加时,您必须动态选择活动文档,因为当您添加新文档时,活动文档会发生变化,因此程序试图从新添加的文档中进行选择,而不是从原始文件。分页符的秘密是将分节符放在下一页。而且,您可以使用PageSetup属性(例如WdPaperSize等)更改选择的页面大小,方向等。要查找某个范围内的内容,可以执行以下操作。
© www.soinside.com 2019 - 2024. All rights reserved.