【发布时间】:2021-01-21 10:19:18
【问题描述】:
我只想将 word 文档的前 10 页打印为 PDF/TIFF 图像。我可以使用以下代码打印 word 文档。
Microsoft.Office.Interop.Word.Application WordApp = null;
Microsoft.Office.Interop.Word.Documents WordDocs = null;
Microsoft.Office.Interop.Word.Document WordDoc = null;
WordApp = new Microsoft.Office.Interop.Word.Application();
WordDocs = WordApp.Documents;
object MissingValue = System.Reflection.Missing.Value;
object fileName = @"file path here", oConfirmConversions = false, oReadOnly = true, oAddToRecentFiles = false, oRevert = true, oVisible = true, oOpenAndRepair = true, oNoEncodingDialog = true;
try
{
WordDoc = WordDocs.OpenNoRepairDialog(ref fileName, ref oConfirmConversions, ref oReadOnly, ref oAddToRecentFiles, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref oVisible, ref oOpenAndRepair, ref MissingValue,
ref oNoEncodingDialog, ref MissingValue);
object oBackground = false, oRange = WdPrintOutRange.wdPrintFromTo, oFrom = "1", oTo = "10", oCopies = 1, oPageType = 0, oPrintToFile = false, oItem = 0;
WordDoc.PrintOut(ref oBackground, ref MissingValue, ref oRange, ref MissingValue, ref oFrom, ref oTo, ref oItem, ref oCopies, ref MissingValue,
ref oPageType, ref oPrintToFile, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue);
}
catch (COMException e)
{
}
finally
{
try
{
if (WordDoc != null)
Marshal.FinalReleaseComObject(WordDoc);
if (WordDocs != null)
Marshal.FinalReleaseComObject(WordDocs);
if (WordApp != null)
Marshal.FinalReleaseComObject(WordApp);
}
catch { }
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
但是,当我在起始页不是 1 的 word 文档上进行测试时,我没有得到预期的结果。所以,我意识到我不应该从 1 开始打印文档。相反,我需要使用起始页码。
那么,如何获取 word 文档的起始页码?
我很难查看此处提供的文档。 https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word
【问题讨论】:
标签: c# ms-word interop office-interop