本篇文章將介紹C#編程如何來處理Word分頁的方法。操作Word中的分頁這里分為幾種情況的來介紹:
處理工具:Spire.Doc for .NET 6.1
安裝該類庫后,在程序中引用Spire.Doc.dll文件即可(如下圖),dll文件在安裝路徑下Bin文件夾中獲取。
【C#】
using Spire.Doc;
using Spire.Doc.Documents;
namespace InsertPageBreak_Doc
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建實例,加載文件
Document document = new Document();
document.LoadFromFile("test.docx");
//在指定段落末尾,插入分頁
document.Sections[0].Paragraphs[1].AppendBreak(BreakType.PageBreak);
//保存文件并打開
document.SaveToFile("PageBreak.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("PageBreak.docx");
}
}
}
調(diào)試運行程序,生成文檔。
分頁前后效果對比添:
分頁前
分頁后
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace InsertPagebreak1_Doc
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建實例,加載文件
Document doc = new Document();
doc.LoadFromFile("test.docx");
//查找需要在其后插入分頁的字符
TextSelection[] selections = doc.FindAllString("guests", true, true);
//遍歷文檔,插入分頁
foreach (TextSelection ts in selections)
{
TextRange range = ts.GetAsOneRange();
Paragraph paragraph = range.OwnerParagraph;
int index = paragraph.ChildObjects.IndexOf(range);
Break pageBreak = new Break(doc, BreakType.PageBreak);
paragraph.ChildObjects.Insert(index + 1, pageBreak);
}
//保存并打開文檔
doc.SaveToFile("Break.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Break.docx");
}
}
}
測試結(jié)果:
C#
using Spire.Doc;
using Spire.Doc.Documents;
namespace RemovePagebreak_Doc
{
class Program
{
static void Main(string[] args)
{
{
//實例化Document類,加載文件
Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);
//遍歷第一節(jié)中的所有段落,移除分頁
for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++)
{
Paragraph p = document.Sections[0].Paragraphs[j];
for (int i = 0; i < p.ChildObjects.Count; i++)
{
DocumentObject obj = p.ChildObjects[i];
if (obj.DocumentObjectType == DocumentObjectType.Break)
{
Break b = obj as Break;
p.ChildObjects.Remove(b);
}
}
}
//保存并打開文件
document.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");
}
}
}
}
測試效果對比:
原文檔:
刪除分頁后:
測試文件如下:
方法一:將跨頁的表格重新定位放置在同一個頁面上
C#
using Spire.Doc;
using Spire.Doc.Documents;
namespace PreventPagebreak_Table__Doc
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建Document類實例,加載文檔
Document doc = new Document("test.docx");
//獲取表格
Table table = doc.Sections[0].Tables[0] as Table;
//設(shè)置表格的段落位置,保持表格在同一頁
foreach (TableRow row in table.Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (Paragraph p in cell.Paragraphs)
{
p.Format.KeepFollow = true;
}
}
}
//保存文件并打開
doc.SaveToFile("result.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("result.docx");
}
}
}
測試效果:
方法二:阻止同一行數(shù)據(jù)被強制分頁
C#
using Spire.Doc;
using Spire.Doc.Documents;
namespace PreventPagebreak_Table__Doc
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建實例,加載文件
Document doc = new Document("test.docx");
//獲取指定表格
Table table = doc.Sections[0].Tables[0] as Table;
//設(shè)置表格分頁屬性
table.TableFormat.IsBreakAcrossPages = false;
//保存并打開文件
doc.SaveToFile("output.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("output.docx");
}
}
}
測試效果:
以上全部是本次關(guān)于如何操作Word中的分頁符的方法。如需轉(zhuǎn)載,請注明出處。
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
網(wǎng)站題目:C#如何處理Word文檔分頁——插入、刪除、阻止分頁-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://www.rwnh.cn/article8/doedop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、Google、軟件開發(fā)、微信公眾號、用戶體驗、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容