Bu makale, URL’yi C# kullanarak PDF’e dönüştürme konusunda yol gösterir. Buradaki talimatları izleyerek ortamı ayarlayabilir, adımların bir listesini ve URL’yi C# kullanarak PDF’ye dönüştürmek için bir örnek kod alabilirsiniz. İstenilen çıktıyı oluşturmak için çıktı PDF sayfası için özel ayarlar da paylaşılır.
C# Kullanarak URL’yi PDF’e Dönüştürme Adımları
- URL’yi PDF’ye dönüştürmek için Aspose.PDF for .NET‘i kullanacak ortamı ayarlayın
- PDF’ye dönüştürme için URL’yi ve çıktı PDF sayfasını yapılandırmak için HtmlLoadOptions‘i tanımlayın
- HTTP isteğini yapmak için HttpClient nesnesini oluşturun
- URL’ye bir Get isteği gönderin ve yanıtı bekleyin
- İsteğin başarılı olduğundan emin olun ve yanıt içeriğini bir akış olarak alın
- Akıştan PDF belgesini oluşturun ve onu diske save olarak ekleyin
Bu adımlar, C# kullanarak web sitesi sayfasını PDF’ye dönüştürme sürecini açıklar. Çıktı PDF sayfa ayarlarını tanımlayın, HttpClient sınıfının bir örneğini oluşturun, URL’ye bir Get isteği gönderin, yanıt akışını alın ve sayfa ayarlarını kullanarak Belge nesnesine geçirin. Son olarak, URL içeriklerinden oluşturulan çıktı PDF’sini diske kaydedin.
C# Kullanarak URL Bağlantısını PDF’e Dönüştürme Kodu
// 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; | |
} | |
} | |
} |
Bu kod, bir web sayfası link’ini C# kullanarak PDF’e nasıl dönüştüreceğinizi göstermiştir. Yazı tiplerini yerleştirmek için bayrak, giriş kodlamasını ayarlama, sayfa düzeni seçeneği, sayfa kenar boşlukları vb. gibi daha fazla ayar uygulamak için HtmlLoadOptions sınıfını kullanabilirsiniz. Uyarıları işlemek için bir geri aramayı WarningHandler kullanarak ayarlayabilirsiniz.
Bu eğitim bize C#* kullanarak bir *PDF belgesine bağlantıyı değiştirme konusunda rehberlik etti. Bir PDF dosyasına köprüler eklemek için C# kullanarak PDF’e köprü metni nasıl eklenir makalesine bakın.