Create E-Tax Invoice
program.cs
using CreateETDAInvoiceFromXml;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Saxon.Api;
using System;
using System.IO;
namespace CreateEtaxInvoiceUsingXslt2
{
class Program
{
static void Main(string[] args)
{
string xsltfilename = @"ExampleDesignForETDA_1p0_c1p0_s1p0_2.xsl";
string inputfilename = @"Example_TaxInvoice_v2-0.xml";
string outputpdfilename = "result.pdf";
var xslt = new FileInfo(xsltfilename);
var input = new FileInfo(inputfilename);
var output = new FileInfo(@"result.html");
var outputpdf = new FileInfo(outputpdfilename);
Processor proc = new Processor();
proc.RegisterExtensionFunction(new BahtText());
XsltCompiler xsltc = proc.NewXsltCompiler();
var executable = xsltc.Compile(new Uri(xslt.FullName));
// Do transformation to a destination
var destination = new DomDestination();
using (var inputStream = input.OpenRead())
{
var transformer = executable.Load();
transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));
transformer.Run(destination);
}
File.WriteAllBytes("result.pdf", createPDF(destination.XmlDocument.OuterXml).ToArray());
// Save result to a file (or whatever else you wanna do)
destination.XmlDocument.Save(output.FullName);
System.Diagnostics.Process.Start("result.pdf");
}
private static MemoryStream createPDF(string html)
{
MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(html);
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
// step 3: we create a worker parse the document
// var worker = new XMLWorker(document);
/**************************************************
* Example #2 *
* *
* Use the XMLWorker to parse the HTML. *
* Only inline CSS and absolutely linked *
* CSS is supported *
* ************************************************/
document.Open();
//XMLWorker also reads from a TextReader and not directly from a string
//Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);
// step 4: we open document and start the worker on the document
//worker.StartDocument();
//// step 5: parse the html into the document
//worker.Parse(reader);
//// step 6: close the document and the worker
//worker.EndDocument();
// worker.Close();
document.Close();
return msOutput;
}
}
}