يرشدك هذا المقال إلى كيفية تحويل عنوان URL إلى PDF باستخدام C#. يمكنك ضبط البيئة والحصول على قائمة بالخطوات ونموذج التعليمات البرمجية لتحويل عنوان URL إلى PDF باستخدام C# باتباع الإرشادات الواردة هنا. تتم أيضًا مشاركة الإعدادات المخصصة لصفحة PDF الناتجة لإنشاء الناتج المطلوب.
خطوات تحويل الرابط إلى PDF باستخدام C#
- قم بتعيين البيئة لاستخدام Aspose.PDF for .NET لتحويل عنوان URL إلى PDF
- قم بتحديد عنوان URL للتحويل إلى PDF وHtmlLoadOptions لتكوين صفحة PDF الناتجة
- إنشاء كائن HttpClient لإجراء طلب HTTP
- أرسل طلب الحصول على عنوان URL وانتظر الرد
- تأكد من نجاح الطلب واحصل على محتوى الاستجابة كدفق
- إنشاء مستند 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#.