บทความนี้อธิบายกระบวนการ สร้างสารบัญใน PDF โดยใช้ C# โดยมีรายละเอียดการตั้งค่าสภาพแวดล้อมการพัฒนา รายการขั้นตอน และโค้ดตัวอย่างเพื่อ เพิ่มสารบัญในรูปแบบ PDF โดยใช้ C# คุณจะได้เรียนรู้การกำหนดค่าทั้งหมดสำหรับสารบัญ รวมถึงข้อความ ไฮเปอร์ลิงก์ และการเชื่อมต่อกับหน้าต่างๆ จากไฟล์ PDF
ขั้นตอนในการเพิ่มสารบัญลงใน PDF โดยใช้ C#
- ตั้งค่าสภาพแวดล้อมเพื่อใช้ Aspose.PDF for .NET เพื่อเพิ่มสารบัญ
- โหลดต้นฉบับ PDF document และแทรกหน้าที่เริ่มต้นเพื่อเพิ่ม TOC
- สร้างออบเจ็กต์ของคลาส TocInfo และ TextFragment เพื่อตั้งชื่อ TOC
- สร้างข้อความส่วนหัวเพื่อเพิ่มลงในสารบัญ
- เรียกใช้การวนซ้ำเพื่อเพิ่มส่วนหัว TOC สำหรับแต่ละหน้าใน PDF ที่โหลด
- ตั้งค่าหน้าปลายทาง พิกัด และข้อความของส่วนหัวในการวนซ้ำแต่ละครั้ง
- บันทึกไฟล์ PDF เอาต์พุตที่มี TOC ในหน้าแรก
ขั้นตอนเหล่านี้อธิบายกระบวนการ สร้างสารบัญที่สามารถคลิกได้ในรูปแบบ PDF โดยใช้ C# โหลดไฟล์ PDF แทรกหน้าที่จุดเริ่มต้นของเอกสารสำหรับสารบัญ และใช้ TocInfo และ TextFragment เพื่อกำหนดคุณลักษณะของ TOC สำหรับแต่ละหน้าในเอกสารต้นฉบับ ให้เพิ่มไฮเปอร์ลิงก์ในสารบัญ ตั้งค่าข้อความไฮเปอร์ลิงก์ และลิงก์หน้า
รหัสเพื่อเพิ่มสารบัญที่คลิกได้ลงใน PDF โดยใช้ C
using System; | |
using System.Collections.Generic; | |
using Aspose.Pdf; | |
using Aspose.Pdf.Text; | |
class Program | |
{ | |
static void Main(string[] args) // Table of content added in PDF | |
{ | |
new License().SetLicense("License.lic"); | |
// Load the pdf document | |
Document inputDoc = new Document("Document1.pdf"); | |
// Get count of pages in the PDF | |
int count = inputDoc.Pages.Count; | |
// Insert a page for table of contents | |
Page pageTOC = inputDoc.Pages.Insert(1); | |
// Instantiate an object of TocInfo for TOC information | |
TocInfo tocInfo = new TocInfo(); | |
// Create an object of TextFragment for setting TOC title | |
TextFragment title = new TextFragment("Table Of Contents"); | |
title.TextState.FontSize = 20; | |
// Set the title for Table of contents | |
tocInfo.Title = title; | |
pageTOC.TocInfo = tocInfo; | |
// Create a list of strings for TOC | |
List<string> tocTitles = new List<string>(); | |
for(int j = 1; j < count; j++) | |
tocTitles.Add($"Page {j + 1}"); | |
int i = 0; | |
while (i < count) | |
{ | |
// Instantiate an object of the Heading class | |
Heading heading = new Heading(1); | |
TextSegment textSegment = new TextSegment(); | |
heading.TocPage = pageTOC; | |
heading.Segments.Add(textSegment); | |
// Set the destination page for the heading object | |
heading.DestinationPage = inputDoc.Pages[i + 2]; | |
// Set the destination coordinates for TOC item | |
heading.Top = inputDoc.Pages[i + 2].Rect.Height; | |
// Set TOC item text | |
textSegment.Text = tocTitles[i]; | |
// Add heading to the TOC page | |
pageTOC.Paragraphs.Add(heading); | |
i += 1; | |
} | |
// Save PDF Document | |
inputDoc.Save("TOC.pdf"); | |
Console.WriteLine("TOC added successfully"); | |
} | |
} |
ตัวอย่างโค้ดนี้สาธิต วิธีเพิ่มสารบัญใน PDF โดยใช้ C# ในตัวอย่างนี้ เนื้อหา TOC จะถูกสร้างขึ้นด้วยตนเองโดยการสร้างรายการสตริงโดยใช้หมายเลขหน้า อย่างไรก็ตาม คุณอาจแยกวิเคราะห์เนื้อหาไฟล์และใช้ในลักษณะเดียวกับสารบัญมาตรฐานที่ใช้ส่วนหัวจากเนื้อหา PDF ใน TOC และเชื่อมโยงกับเนื้อหาที่ต้องการในไฟล์ PDF
บทความนี้ได้สอนเรา วิธีสร้างสารบัญ PDF โดยใช้ C# หากต้องการเพิ่มไฮเปอร์ลิงก์ในเนื้อหาของไฟล์ PDF โปรดดูบทความเกี่ยวกับ วิธีเพิ่มไฮเปอร์ลิงก์ใน PDF โดยใช้ C#