Šiame straipsnyje paaiškinama, kaip konvertuoti URL į PDF naudojant C#. Galite nustatyti aplinką, gauti veiksmų sąrašą ir pavyzdinį kodą, kad pakeistumėte URL į PDF naudodami C#, vadovaudamiesi čia pateiktomis instrukcijomis. Priskirti išvesties PDF puslapio nustatymai taip pat bendrinami norint sukurti pageidaujamą išvestį.
Veiksmai URL paversti PDF naudojant C#
- Nustatykite aplinką naudoti Aspose.PDF for .NET URL konvertavimui į PDF
- Apibrėžkite URL konvertavimui į PDF ir HtmlLoadOptions, kad sukonfigūruotumėte išvesties PDF puslapį
- Norėdami pateikti HTTP užklausą, sukurkite objektą HttpClient
- Išsiųskite užklausą Gauti URL adresu ir laukite atsakymo
- Užtikrinkite sėkmingą užklausą ir gaukite atsakymo turinį kaip srautą
- Sukurkite PDF dokumentą iš srauto ir save jį diske
Šie veiksmai apibūdina procesą, kaip konvertuoti svetainės puslapį į PDF naudojant C#. Apibrėžkite išvesties PDF puslapio parametrus, sukurkite HttpClient klasės egzempliorių, išsiųskite užklausą Gauti URL adresu, gaukite atsakymo srautą ir perkelkite į dokumento objektą naudodami puslapio nustatymus. Galiausiai išsaugokite išvestį PDF, sugeneruotą iš URL turinio diske.
Kodas URL nuorodos konvertavimui į PDF naudojant 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; | |
} | |
} | |
} |
Šis kodas parodė, kaip tinklalapį nuorodą paversti PDF formatu naudojant C#. Galite naudoti klasę HtmlLoadOptions, kad pritaikytumėte daugiau nustatymų, pvz., žymėjimą įterpiant šriftus, nustatytumėte įvesties kodavimą, puslapio išdėstymo parinktį, puslapio paraštes ir kt. Galite nustatyti įspėjimų apdorojimo atgalinį iškvietimą naudodami WarningHandler.
Ši instrukcija padėjo mums pakeisti nuorodą į PDF dokumentą naudojant C#. Norėdami pridėti hipersaitų į PDF failą, žr. straipsnį Kaip pridėti hipersaitą į PDF naudojant C#.