Bu adım adım öğreticide, Markdown’ı C# ile nasıl XPS’e dönüştüreceğinizi öğreneceksiniz. Önce Markdown dosyasını C#‘da HTML’ye dönüştüreceğiz ve ardından HTML’yi XPS dosya biçimine dönüştürmek için C# kodunu kullanacağız.
C#‘da Markdown’ı XPS’e Dönüştürme Adımları
- NuGet.org’dan Aspose.HTML for .NET paketini kurun
- Aspose.HTML API’sinin dört gerekli ad alanını ekleyin
- API lisansını uygulamak için SetLicense yöntemini kullanın
- Converter class kullanarak Markdown (MD) dosyasını HTMLDocument object içine yükleyin
- Çıktıyı bir HTML dosyası olarak kaydedin
- Çıktı HTML dosyasını HTMLDocument nesnesine yükleyin
- XpsRenderingOptions class kullanarak XPS seçeneklerini belirtin
- XPS çıktısı oluşturmak için bir XpsDevice class örneği oluşturun
- Yüklenen HTML dosyasını XPS dosya biçimi olarak işleyin
Markdown (MD) dosya formatından XPS’ye dönüştürme, tek bir Aspose.HTML for .NET API kullanılarak iki adımda yapılır. İlk adımda, MD dosyası HTML’ye dönüştürülür ve ardından HTML, XPS dosya biçimine dönüştürülür. Dönüştürme işlemi çok basit ve kolaydır.
C#’ta Markdown’ı XPS’e Dönüştürme Kodu
using System; | |
//Add reference to Aspose.HTML for .NET API | |
//Use following namespaces to convert markdown to HTML to XPS | |
using Aspose.Html; | |
using Aspose.Html.Converters; | |
using Aspose.Html.Rendering.Xps; | |
using Aspose.Html.Drawing; | |
namespace ConvertMDtoHTMLtoXPS | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before converting markdown to HTML to XPS | |
//using Aspose.HTML for .NET | |
Aspose.Html.License AsposeHtmlLicense = new Aspose.Html.License(); | |
AsposeHtmlLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Create HTMLDocument object and input markdown file | |
HTMLDocument MarkdownToHTMLDoc = Converter.ConvertMarkdown("InputMarkdownFile.md"); | |
//Save markdown (.md) file to HTML format | |
MarkdownToHTMLDoc.Save("MarkdownConvertedToHTML.html"); | |
//Load the MD to HTML converted file to an HTMLDocument object | |
HTMLDocument HTMLToXPSDoc = new HTMLDocument("MarkdownConvertedToHTML.html"); | |
//Setup required XPS output options like page size | |
XpsRenderingOptions OutputXPSOptions = new XpsRenderingOptions(); | |
OutputXPSOptions.PageSetup.AnyPage.Size = new Size(Unit.FromInches(8.5), Unit.FromInches(11)); | |
//Create an XPS Device which will render HTML to XPS | |
XpsDevice XPSDevice = new XpsDevice(OutputXPSOptions, "HTMLToXPS.xps"); | |
//Render input HTML file to XPS Device to save as XPS file format | |
HTMLToXPSDoc.RenderTo(XPSDevice); | |
} | |
} | |
} |
Yukarıdaki kod, her iki dönüşümün de HTMLDocument nesnesi gerektirdiğini gösterir. HTML conversion için Markdown’da Converter sınıfını kullanıyoruz. HTML’den XPS’ye dönüştürme için, HTML’yi XPS belgesi olarak oluşturmaya yardımcı olan bir XpsDevice nesnesi kullanıyoruz. Yukarıdaki C# kod örneği, herhangi bir .NET platformu ve aracıyla sorunsuz çalışır.