Generate PDF using xml data and xsl

In my requirement data should split into 2 columns in same page(left to right and top to botton but in 2 columns) if data both columns are filed then data should appear on next page…
Please share the code and approach using Aspose.abc.png (255.8 KB)

@jitendrapatil2514

Thank you for contacting support.

Aspose.PDF for .NET offers BindXml() method to create PDF using XML and XSLT style. Following code snippet can be used to generate PDF using both files provided that the XML and XSLT elements sequence is based on Aspose.PDF XML Schema:

Document pdfDocument = new Document();
pdfDocument.BindXml("input.xml", "input.xsl");
pdfDocument.Save("output.pdf");

In order to convert XML file into PDF, please visit following article(s) in our API documentation:

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thanks Farhan,
I have tried above code snippet but pdf is blank and getting error (please check attached snapshot)
data.PNG (8.9 KB)

I have used below XSL -

<?xml version="1.0" encoding="UTF-16" ?>

<xsl:stylesheet version=“1.0” xmlns:fo=“http://www.w3.org/1999/XSL/Format
xmlns:xsl=“XSLT Namespace”>
<xsl:output method=“xml” version=“1.0” indent=“yes” />
<xsl:template match=“doc”>
<fo:root xmlns:fo=“http://www.w3.org/1999/XSL/Format”>
fo:layout-master-set
<fo:simple-page-master page-height=“297mm” page-width=“210mm”
margin=“5mm 25mm 5mm 25mm” master-name=“PageMaster”>
<fo:region-body margin=“20mm 0mm 20mm 0mm” column-count=“2”/>
</fo:simple-page-master>
<fo:simple-page-master master-name=“intro”>
<fo:region-body margin=“5in” />
</fo:simple-page-master>
<fo:simple-page-master master-name=“left”>
<fo:region-body margin-left=“2in” margin-right=“3in” />
</fo:simple-page-master>
<fo:simple-page-master master-name=“right”>
<fo:region-body margin-left=“3in” margin-right=“2in” />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference=“PageMaster”>
<fo:flow flow-name=“xsl-region-body” >
fo:block
xsl:apply-templates/
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>

<xsl:template match=“p”>
<fo:block font-weight=“bold” background-color=“red” color=“blue”>
<xsl:apply-templates />
</fo:block>
</xsl:template>
<xsl:template match=“b”>
<fo:inline font-weight=“bold” background-color=“red” color=“blue”>
<xsl:apply-templates />
</fo:inline>
</xsl:template>
</xsl:stylesheet >

------------------------------------XML used -------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-16" ?> Simple

Hello World!

This is the firstSimpleDoc


while copy paste XSL and XML content is not appearing properly please check attached snapshots2.PNG (8.6 KB)
1.PNG (53.5 KB)

@jitendrapatil2514

Please note that the XML shared by you does not follow Aspose.PDF for .NET XML Schema so the issue is occurring. Kindly ensure sequence of elements in accordance with schema and then share your kind feedback with us.

Thank you so much…Can you please share your Skype ID? or you can connect me on jitanurag1425@gmail.com

If possible share the updated XML of above attached XML.

@jitendrapatil2514

Please note that support is offered via forums only so we may not contact you over other platforms. Moreover, a basic and Hello World level XML is already available in the documentation article Generate PDF from XML for your kind reference.

Thank you for reply

To generate the PDF, I have tried 2 approaches-

  1. Transformed XML data with XSL and converted into HTML then Using Aspose.HTML, converted HTML to PDF(It’s working as expected using same .xml and .xsl files)

  2. When I am trying to use Aspose.PDF please check below code. Pdf is generating and is not opening because it’s blank pdf. Even I have used same xml which suggested by you (Programmers Guide)
    and try to generate PDF using XML only it’s generating BLANK PDF.

Please share valid SAMPLE XML and XSL which follow Aspose.PDF for .NET XML Schema
so I can use below code for my requirement.

Document pdf = new Document();

        using (FileStream fs1 = new FileStream(xmlpath + "Examples.xml", FileMode.Open))
        using (FileStream fs2 = new FileStream(xmlpath + "Examples.xsl", FileMode.Open))
        {
            // Bind the XML file
            pdf.BindXml(fs1, fs2);
            // Save the resultant PDF
            pdf.Save(xmlpath + "Examples.pdf");
        }

I am achieving requirement using first approach but I don’t have ASPOSE.Html lic but I have for ASPOSE.Pdf.lic

Thanks in advance

@jitendrapatil2514

Please find attached below files for your kind reference. Attached PDF file has been generated with your code snippet.

XMLxslDemo.zip

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thank you so much for help. It’s working fine.

Hi @Farhan.Raza,

When I am trying to use Table in C# code,Table count is showing ZERO. Please look into below code. I have to apply colspan =2 on cell. I am using same XML and XSL which you shared above.

     Document pdf = new Document();
      // Bind the XML file
       pdf.BindXml(xmlpath + "DemoXml.xml", xmlpath + "DemoXslt.xsl");
        TableAbsorber absorber = new TableAbsorber();
         absorber.Visit(pdf.Pages[1]);
         foreach (AbsorbedTable table in absorber.TableList)
         {

}
I am using ASPOSE 18.12 version

Thank you for quick assistance.
sss.PNG (41.6 KB)ttt.PNG (24.2 KB)

@jitendrapatil2514

We have been able to notice the problem when extracting the table from generated PDF document. A ticket with ID PDFNET-45892 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

Thanks to let me know.

Please share the estimated time till when it will resolve or I have to try other option to achive same.
Any other stable ASPOSE PDF version where I can get Table and cell.

Thanks again for assistance

@jitendrapatil2514

Please note that support is provided based on latest version of the API because of it being the most stable version. About investigations of recently logged ticket, it will be scheduled on its due turn which may take few months owing to previously logged tickets. We will let you know as soon as some significant updates will be available.

Hi Farhan,

Thanks to update.
Can you please help on below requirement? How can I achieve using ASPOSE PDF?

https://stackoverflow.com/questions/54146720/data-flow-in-table-from-left-td-to-right-in-html-if-text-reach-to-end-of-page

otherwise I have to go with other PDF generation approaches.

Thanks again for assistance.

@jitendrapatil2514

Please always create separate posts for separate requirements.

Would you please share source XML and XSL file along with a PDF document as expected output from that data, so that we may investigate further to help you out.

Html-PDF.zip (54.4 KB)
Please find the attached XML, XSL , HTML and output PDF.
I am using below c# code -

using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.Core.Entities;
using TheArtOfDev.HtmlRenderer.PdfSharp;

string myStyleSheet = xmlpath + “Component.xsl”;
string myhtml = xmlpath + “Component.html”;

XPathDocument myXPathDoc = new XPathDocument(xmlpath + “Component.xml”);
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
using (XmlTextWriter myWriter = new XmlTextWriter(myhtml, null))
myXslTrans.Transform(myXPathDoc, null, myWriter);
string html = File.ReadAllText(myhtml);
PdfDocument pdf = PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A3, 15, null, null, imageLoad: OnImageLoad);

pdf.Save(xmlpath + “Component.pdf”);

2nd approach I have tried using Aspose.HTML
https://docs.aspose.com/html/net/convert-html-to-pdf/ instead of PdfSharp

Next time will create separate thread for different requirement, Please suggest how to get the multicolumn text flow in html using Aspose.

@jitendrapatil2514

Thank you for elaborating it further.

We would like to update you that once you generate the HTML file using System.XML namespace, you can also use Aspose.PDF for .NET API to convert that HTML file into a PDF document using below code snippet.

HtmlLoadOptions options = new HtmlLoadOptions();
Document document = new Document(myhtml , options);
document.Save(xmlpath + "Aspose_19.1.pdf");

Moreover, if you want to generate a multiple column PDF document from your HTML file then you may use below code snippet and then share your kind feedback.

string html = File.ReadAllText(dataDir + "Component.html");
Document doc = new Document();
// Specify the left margin info for the PDF file
doc.PageInfo.Margin.Left = 40;
// Specify the Right margin info for the PDF file
doc.PageInfo.Margin.Right = 40;
Aspose.Pdf.Page page = doc.Pages.Add();

Aspose.Pdf.Drawing.Graph graph1 = new Aspose.Pdf.Drawing.Graph(500, 2);
// Add the line to paraphraphs collection of section object
page.Paragraphs.Add(graph1);

// Specify the coordinates for the line
float[] posArr = new float[] { 1, 2, 500, 2 };
Aspose.Pdf.Drawing.Line l1 = new Aspose.Pdf.Drawing.Line(posArr);
graph1.Shapes.Add(l1);
// Create string variables with text containing html tags

string s = "<font face=\"Times New Roman\" size=4>" +

"<strong> How to Steer Clear of money scams</<strong> "
+ "</font>";
// Create text paragraphs containing HTML text

HtmlFragment heading_text = new HtmlFragment(s);
page.Paragraphs.Add(heading_text);

Aspose.Pdf.FloatingBox box = new Aspose.Pdf.FloatingBox();
// Add four columns in the section
box.ColumnInfo.ColumnCount = 2;
// Set the spacing between the columns
box.ColumnInfo.ColumnSpacing = "5";

box.ColumnInfo.ColumnWidths = "105 105";
TextFragment text1 = new TextFragment("By A Googler (The Official Google Blog)");
text1.TextState.FontSize = 8;
text1.TextState.LineSpacing = 2;
box.Paragraphs.Add(text1);
text1.TextState.FontSize = 10;

text1.TextState.FontStyle = FontStyles.Italic;
// Create a graphs object to draw a line
Aspose.Pdf.Drawing.Graph graph2 = new Aspose.Pdf.Drawing.Graph(50, 10);
// Specify the coordinates for the line
float[] posArr2 = new float[] { 1, 10, 100, 10 };
Aspose.Pdf.Drawing.Line l2 = new Aspose.Pdf.Drawing.Line(posArr2);
graph2.Shapes.Add(l2);

// Add the line to paragraphs collection of section object
box.Paragraphs.Add(graph2);

HtmlFragment text2 = new HtmlFragment(html);
box.Paragraphs.Add(text2);

page.Paragraphs.Add(box);

dataDir = dataDir + "CreateMultiColumnPdf_19.1.pdf";
// Save PDF file
doc.Save(dataDir);

You may also visit Create Multi-Column PDF document for your kind reference. Above code snippet creates a PDF document having two columns on each page as a demonstration. You may modify size or number of columns as per your requirements.

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Hi Farhan,

Thanks to reply.
I have tried to below code as you suggested using Aspose.PDF. I am able to generate PDF but output is not as expected. Please check the attached xml , pdf, xsl (in xsl I am applying styling as column-count: 2; which is dividing text into 2 column) but text appear in generated PDF is in single column I have tried with Aspose.HTML as well, same issue using Aspose.HTML.

string myStyleSheet = xmlpath + “Component.xsl”;
string myhtml = xmlpath + “Component.html”;

XPathDocument myXPathDoc = new XPathDocument(xmlpath + “Component.xml”);
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
using (XmlTextWriter myWriter = new XmlTextWriter(myhtml, null))
myXslTrans.Transform(myXPathDoc, null, myWriter);

HtmlLoadOptions options = new HtmlLoadOptions();
Aspose.Pdf.Document document = new Aspose.Pdf.Document(myhtml, options);
document.Save(xmlpath + “Aspose_19.1.pdf”);

test1.zip (73.9 KB)

Regarding create Multi-clumn PDF document issue, using above code it’s working fine but problem is I have to handle all scenarios (In my requirement this is not only one scenario many scenarios are there please check attached PDF template UNICEF_SFA_Adolescent Programming_28.09.2018.pdf (349.4 KB)
) in C# code with Aspose which little bit challanging than handle logic in XSL(please check in attached xsl where I am applying all styling and logic to split text into multi-column)

Can you please share tentative dates to resolve this below issue? If this below issue will get resolve ASAP I will very thankful to you and Aspose team. I am able to generate the same styling and formatting as in HTML using (column-count :2 and other styling attribute) but now PDF is not generating as same as HTML.

I am not happy to use Aspose.PDF and Aspose.HTML for my requirement.

@jitendrapatil2514

We have been able to notice the difference while rendering the HTML to PDF with Aspose.PDF for .NET API as well. A ticket with ID PDFNET-45961 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

Moreover, we will be able to share the ETA once the issues will be investigated. Please be patient and spare us some time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-45892) have been fixed in Aspose.PDF for .NET 23.8.