MS Word Interop,将段落和表格与 headers 保持在一起,防止它们拆分到下一页
MS Word Interop, Keeping Paragraphs and tables with headers together keeping them from splitting to the next page
我正在使用下面的代码,它可以完美地合并排队的 html 文件列表,并使用 MS Word Interop 将它们保存为 PDF 或 DOCX。我 运行 遇到了分页符问题。我无法弄清楚如何防止段落和 tables 在中间分页。我的目标是将段落中的文本和 table 放在一起。大多数 table 的正上方还有一个标题文本。如果可能的话,最好也把它们放在一起。有没有办法以编程方式将这些项目放在一起?正在使用的文档没有静态措辞或格式。它们都是动态创建的,并且可以根据情况完全不同。此代码是在 .NET 2.0 环境中开发的。
public static void MergeA(string[] filesToMerge, string outputFilename, bool insertPageBreaks, bool pdf)
{
//object defaultTemplate = documentTemplate;
object missing = System.Type.Missing;
object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
object outputFile = outputFilename;
object oFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
if (pdf)
{
oFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
}
// Create a new Word application
Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();
wordApplication.Visible = false;
try
{
// Create a new file based on our template
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;
// Loop thru each of the Word documents
foreach (string file in filesToMerge)
{
// 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)
{
selection.InsertBreak(ref pageBreak);
}
}
// Save the document to it’s output file.
wordDocument.SaveAs2(
ref outputFile
, ref oFileFormat
, 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);
// Clean up!
wordDocument = null;
}
catch (Exception ex)
{
//I didn’t include a default error handler so i’m just throwing the error
throw ex;
}
finally
{
// Finally, Close our Word application
wordApplication.Quit(ref missing, ref missing, ref missing);
}
}
我快到了。我在 SaveAs2 之前的插入分页符 if 语句之后添加了下面的代码。这看起来像我希望的那样工作,但我仍然 运行 遇到它在 table header 上中断的问题。我在想我可能需要将 header 标签封装在 table 中,但是对于我们如何使用它会非常困难,因为原始文件 (filesToMerge) 是在 [=19 中动态创建的=].我还认为我需要减少字体,因为这似乎也导致一些文本被截断或截成两半。它切断文本似乎有点奇怪。在进一步检查保存的文档后,我很幸运原始 html 文件将文本封装在 table 中。这很有帮助。看起来我需要修复截断的文本并将 header 文本与分页符上的 table 放在一起,我现在已经解决了这个问题。任何想法都会很棒。我希望这个问题对其他人有所帮助,因为有一些较旧的帖子,但它们不是很详细。
//Format tables so that they do not split up on page breaks.
foreach (Microsoft.Office.Interop.Word.Table oTable in wordDocument.Tables)
{
oTable.AllowPageBreaks = false;
oTable.Rows.AllowBreakAcrossPages = 0;
}
经过进一步研究,我感到困惑。 table header 似乎在 html 中的 TR TD 标签中,当保存为 word 文档时,它实际上在 table 中,但它没有保留它一起。对于上面的循环,我不确定为什么会发生这种情况。
它可能不会给你想要的答案,但是...
Microsoft 目前不推荐也不支持从任何无人值守的非交互式客户端应用程序或组件(包括 ASP、ASP.NET 自动化 Microsoft Office 应用程序、DCOM 和 NT 服务),因为当 Office 在此环境中为 运行 时,Office 可能表现出不稳定的行为 and/or 死锁。
如果您要在服务器端上下文中构建 运行 的解决方案,您应该尝试使用已针对无人值守执行安全处理的组件。或者,您应该尝试找到至少允许 运行 客户端部分代码的替代方案。如果您从服务器端解决方案使用 Office 应用程序,该应用程序将缺少许多 运行 成功所必需的功能。此外,您将承担整体解决方案稳定性的风险。在 Considerations for server-side Automation of Office 文章中阅读更多相关信息。
您可以考虑使用 Open XML SDK 或为服务器端执行而设计的任何第三方组件。有关详细信息,请参阅 Welcome to the Open XML SDK 2.5 for Office。
我忘记了这个问题,但我确实解决了它,因为它收到了很多意见,我觉得展示我的有效解决方案会很有帮助。
foreach (Microsoft.Office.Interop.Word.Table oTable in wordDocument.Tables)
{
oTable.AllowPageBreaks = false;
oTable.Rows.AllowBreakAcrossPages = 0;
}
我在这个问题上兜了一圈。现在我需要弄清楚如何在 table 上方包含标签以打破 table。
可能有更好的方法来完成这一切,因为原始格式是 HTML 并且业务需要将 HTML 格式的页面保存在 Word 和 PDF 中。我 运行 遇到的问题是所有编程保存的格式看起来与 HTML 不一样,而且看起来也不是最好看的。问题出在table的大小,文字,分页不当
.
我正在使用下面的代码,它可以完美地合并排队的 html 文件列表,并使用 MS Word Interop 将它们保存为 PDF 或 DOCX。我 运行 遇到了分页符问题。我无法弄清楚如何防止段落和 tables 在中间分页。我的目标是将段落中的文本和 table 放在一起。大多数 table 的正上方还有一个标题文本。如果可能的话,最好也把它们放在一起。有没有办法以编程方式将这些项目放在一起?正在使用的文档没有静态措辞或格式。它们都是动态创建的,并且可以根据情况完全不同。此代码是在 .NET 2.0 环境中开发的。
public static void MergeA(string[] filesToMerge, string outputFilename, bool insertPageBreaks, bool pdf)
{
//object defaultTemplate = documentTemplate;
object missing = System.Type.Missing;
object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
object outputFile = outputFilename;
object oFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
if (pdf)
{
oFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
}
// Create a new Word application
Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();
wordApplication.Visible = false;
try
{
// Create a new file based on our template
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;
// Loop thru each of the Word documents
foreach (string file in filesToMerge)
{
// 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)
{
selection.InsertBreak(ref pageBreak);
}
}
// Save the document to it’s output file.
wordDocument.SaveAs2(
ref outputFile
, ref oFileFormat
, 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);
// Clean up!
wordDocument = null;
}
catch (Exception ex)
{
//I didn’t include a default error handler so i’m just throwing the error
throw ex;
}
finally
{
// Finally, Close our Word application
wordApplication.Quit(ref missing, ref missing, ref missing);
}
}
我快到了。我在 SaveAs2 之前的插入分页符 if 语句之后添加了下面的代码。这看起来像我希望的那样工作,但我仍然 运行 遇到它在 table header 上中断的问题。我在想我可能需要将 header 标签封装在 table 中,但是对于我们如何使用它会非常困难,因为原始文件 (filesToMerge) 是在 [=19 中动态创建的=].我还认为我需要减少字体,因为这似乎也导致一些文本被截断或截成两半。它切断文本似乎有点奇怪。在进一步检查保存的文档后,我很幸运原始 html 文件将文本封装在 table 中。这很有帮助。看起来我需要修复截断的文本并将 header 文本与分页符上的 table 放在一起,我现在已经解决了这个问题。任何想法都会很棒。我希望这个问题对其他人有所帮助,因为有一些较旧的帖子,但它们不是很详细。
//Format tables so that they do not split up on page breaks.
foreach (Microsoft.Office.Interop.Word.Table oTable in wordDocument.Tables)
{
oTable.AllowPageBreaks = false;
oTable.Rows.AllowBreakAcrossPages = 0;
}
经过进一步研究,我感到困惑。 table header 似乎在 html 中的 TR TD 标签中,当保存为 word 文档时,它实际上在 table 中,但它没有保留它一起。对于上面的循环,我不确定为什么会发生这种情况。
它可能不会给你想要的答案,但是...
Microsoft 目前不推荐也不支持从任何无人值守的非交互式客户端应用程序或组件(包括 ASP、ASP.NET 自动化 Microsoft Office 应用程序、DCOM 和 NT 服务),因为当 Office 在此环境中为 运行 时,Office 可能表现出不稳定的行为 and/or 死锁。
如果您要在服务器端上下文中构建 运行 的解决方案,您应该尝试使用已针对无人值守执行安全处理的组件。或者,您应该尝试找到至少允许 运行 客户端部分代码的替代方案。如果您从服务器端解决方案使用 Office 应用程序,该应用程序将缺少许多 运行 成功所必需的功能。此外,您将承担整体解决方案稳定性的风险。在 Considerations for server-side Automation of Office 文章中阅读更多相关信息。
您可以考虑使用 Open XML SDK 或为服务器端执行而设计的任何第三方组件。有关详细信息,请参阅 Welcome to the Open XML SDK 2.5 for Office。
我忘记了这个问题,但我确实解决了它,因为它收到了很多意见,我觉得展示我的有效解决方案会很有帮助。
foreach (Microsoft.Office.Interop.Word.Table oTable in wordDocument.Tables)
{
oTable.AllowPageBreaks = false;
oTable.Rows.AllowBreakAcrossPages = 0;
}
我在这个问题上兜了一圈。现在我需要弄清楚如何在 table 上方包含标签以打破 table。
可能有更好的方法来完成这一切,因为原始格式是 HTML 并且业务需要将 HTML 格式的页面保存在 Word 和 PDF 中。我 运行 遇到的问题是所有编程保存的格式看起来与 HTML 不一样,而且看起来也不是最好看的。问题出在table的大小,文字,分页不当 .