แปลง URL เป็น PDF โดยใช้ C#

บทความนี้จะแนะนำวิธีแปลง URL เป็น PDF โดยใช้ C#** คุณสามารถตั้งค่าสภาพแวดล้อม รับรายการขั้นตอน และตัวอย่างโค้ดเพื่อแปลง URL เป็น PDF โดยใช้ C#** ได้โดยทำตามคำแนะนำที่นี่ การตั้งค่าแบบกำหนดเองสำหรับหน้าเอาต์พุต PDF ยังแชร์ไว้สำหรับการสร้างเอาต์พุตตามต้องการอีกด้วย

ขั้นตอนการเปลี่ยน URL เป็น PDF โดยใช้ C#

  1. ตั้งค่าสภาพแวดล้อมที่จะใช้ Aspose.PDF for .NET เพื่อแปลง URL เป็น PDF
  2. กำหนด URL สำหรับการแปลงเป็น PDF และ HtmlLoadOptions สำหรับการกำหนดค่าหน้า PDF เอาท์พุต
  3. สร้างวัตถุ HttpClient เพื่อสร้างคำขอ HTTP
  4. ส่งคำขอรับไปยัง URL และรอการตอบกลับ
  5. ให้แน่ใจว่าคำขอประสบความสำเร็จและรับเนื้อหาการตอบกลับเป็นสตรีม
  6. สร้างเอกสาร PDF จากสตรีมและ save ลงในดิสก์

ขั้นตอนเหล่านี้อธิบายกระบวนการแปลงหน้าเว็บไซต์เป็น PDF โดยใช้ C#* กำหนดการตั้งค่าหน้า PDF เอาต์พุต สร้างอินสแตนซ์ของคลาส HttpClient ส่งคำขอ Get ไปยัง URL ดึงสตรีมการตอบสนองและส่งไปยังอ็อบเจ็กต์ Document โดยใช้การตั้งค่าหน้า สุดท้าย ให้บันทึก PDF เอาต์พุตที่สร้างจากเนื้อหา URL ลงในดิสก์

โค้ดสำหรับแปลงลิงก์ URL เป็น PDF โดยใช้ C#

// Importing required namespaces
using System;
using System.IO;
using System.Net.Http;
using Aspose.Pdf;
// Defining a namespace for the project
namespace HtmlToPdfConverter
{
// Main class of the program
class ConverterApp
{
// Entry point of the program
static void Main(string[] args)
{
// Initialize and apply Aspose.PDF license
License pdfLicense = new License();
pdfLicense.SetLicense("Aspose_License.lic");
// Convert an online HTML page to PDF
GeneratePdfFromWebPage();
}
// Method to fetch and convert an HTML webpage to a PDF document
private static void GeneratePdfFromWebPage()
{
// Define the webpage URL to be converted
const string webpageUrl = "https://docs.aspose.com/";
// Configure PDF page settings for conversion
var pdfOptions = new HtmlLoadOptions(webpageUrl)
{
PageInfo =
{
Width = 1200, // Setting custom page width
Height = 850, // Setting custom page height
IsLandscape = false // Keeping portrait orientation
}
};
// Fetch the webpage content and create a PDF document
using (var pdfDocument = new Document(FetchWebContentAsStream(webpageUrl), pdfOptions))
{
// Save the generated PDF file
pdfDocument.Save("Converted_WebPage.pdf");
}
}
// Method to retrieve the content of a webpage as a stream
static Stream FetchWebContentAsStream(string webpageUrl)
{
// Initialize HTTP client to make web requests
HttpClient httpClient = new HttpClient();
// Send a GET request and retrieve the response
HttpResponseMessage webResponse = httpClient.GetAsync(webpageUrl, HttpCompletionOption.ResponseHeadersRead).Result;
// Ensure the response was successful
webResponse.EnsureSuccessStatusCode();
// Return the webpage content as a stream
return webResponse.Content.ReadAsStreamAsync().Result;
}
}
}

โค้ดนี้สาธิตวิธีการแปลงลิงก์ของเว็บเพจเป็น PDF โดยใช้ C#* คุณสามารถใช้คลาส HtmlLoadOptions เพื่อใช้การตั้งค่าเพิ่มเติม เช่น การกำหนดแฟล็กเพื่อฝังฟอนต์ ตั้งค่าการเข้ารหัสอินพุต ตัวเลือกเค้าโครงหน้า ขอบหน้า ฯลฯ คุณสามารถตั้งค่าการเรียกกลับเพื่อจัดการคำเตือนโดยใช้ WarningHandler

บทช่วยสอนนี้แนะนำเราในการเปลี่ยน ลิงก์ไปยังเอกสาร PDF โดยใช้ C# หากต้องการเพิ่มไฮเปอร์ลิงก์ในไฟล์ PDF โปรดดูบทความใน วิธีการเพิ่มไฮเปอร์ลิงก์ใน PDF โดยใช้ C#

 ไทย