In order to transform XML into PDF, we’ll use Aspose.PDF for C++ API which is a feature-rich, powerful and easy to use document manipulation API for cpp platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.
PM > Install-Package Aspose.PDF.Cpp
Transform XML and load into PDF via C++
You need Aspose.PDF for C++ to try the code in your environment.
- Set page parameters.
- Upload XSLT file.
- Then Load and Transform.
Transform XML into PDF - C++
void WorkingWithXML::ExampleXSLTtoPDF()
{
String _dataDir("C:\\Samples\\");
auto XmlContent = System::IO::File::ReadAllText(u"XMLFile1.xml");
auto XsltContent = System::IO::File::ReadAllText(u"XSLTFile1.xslt");
auto options = MakeObject<Aspose::Pdf::HtmlLoadOptions>();
// set page size to A5
options->get_PageInfo()->set_Height(595);
options->get_PageInfo()->set_Width(420);
auto pdfDocument = MakeObject<Aspose::Pdf::Document>(TransformXmltoHtml(XmlContent, XsltContent), options);
pdfDocument->Save(_dataDir + u"data_xml.pdf");
}
System::SharedPtr<System::IO::MemoryStream> TransformXmltoHtml(String inputXml, String xsltString)
{
auto transform = MakeObject<System::Xml::Xsl::XslCompiledTransform>();
auto reader = System::Xml::XmlReader::Create(MakeObject<System::IO::StringReader>(xsltString));
transform->Load(reader);
auto memoryStream = MakeObject<System::IO::MemoryStream>();
auto results = System::Xml::XmlWriter::Create(memoryStream);
auto reader2 = System::Xml::XmlReader::Create(MakeObject<System::IO::StringReader>(inputXml));
transform->Transform(reader2,nullptr,results);
memoryStream->set_Position (0);
return memoryStream;
}