如何获取word文档的起始页码?
How to get starting page number of a word document?
我只想将 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
我得到了我需要的答案。感谢 post How to change the starting page number in word in microsoft interop?
起初解决方案不是很清楚,因为我希望获得部分的索引 0。但是发现index从1开始不是0.
工作代码
public int getStartingNumber(Document WordDoc)
{
var sections = WordDoc.Sections;
for (int i = 1; i <= sections.Count; i++)
{
var section = sections[i];
var headers = section.Headers;
foreach (HeaderFooter headerfooter in headers)
{
var pageNumbers = headerfooter.PageNumbers;
return pageNumbers.StartingNumber;
}
}
return -1;
}
我只想将 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
我得到了我需要的答案。感谢 post How to change the starting page number in word in microsoft interop? 起初解决方案不是很清楚,因为我希望获得部分的索引 0。但是发现index从1开始不是0.
工作代码
public int getStartingNumber(Document WordDoc)
{
var sections = WordDoc.Sections;
for (int i = 1; i <= sections.Count; i++)
{
var section = sections[i];
var headers = section.Headers;
foreach (HeaderFooter headerfooter in headers)
{
var pageNumbers = headerfooter.PageNumbers;
return pageNumbers.StartingNumber;
}
}
return -1;
}