Altsoft Xml2PDF Server Library

Generally, you first create the APSource object that represents your source document. It can be done using one of Create factory methods in APSource class that accepts any stream (memory, zip archive, etc) or URI (local file, web reference, etc) as an input parameter:

    APSource src1 = APSource.Create("1.fo");
    APSource src2 = APSource.Create("http://www.w3c.org");

    System.IO.Stream stream = new System.IO.MemoryStream();
    APSource src3 = APSource.Create(stream, APSourceFormat.FO);

Optionally, you can apply an XSL transformation to any source XML document. In this case you first create a new APSource with final (after applying XSL transformation) source format, than create a separate APSource for your XSL transformation and apply this XSLT to the source document using ApplyXSLT. Note that XSLT documents are also represented as APSource objects, but an XSLT source can be used only as an input parameter for APSource.ApplyXSLT method:

    APSource source = APSource.CreateFO("1.xml");
    APSource xslt = APSource.CreateXSLT("1.xsl");
    source.ApplyXSLT(xslt);

You can create required APDestination object using one of the factory Create methods in APDestination class. The destination document can be created either in memory, or as a local file. You can configure Altsoft formatting engine with APConfig object specified as one of the Create method parameters.

Additionally, you can specify security options (at the moment only PDF encryption) using properties UserPassword, OwnerPassword and Permissions:

    APDestionation pdf = APDestination.CreatePDF("1.pdf");
    pdf.OwnerPassword = "password";
    pdf.Permissions.CanPrint = false;

To format the source document and append it to the destination object you simply use Append method of APEngine class and save the destination object:

    using(APDestination pdf = APDestination.CreatePDF("result.pdf"))
    {
        using(APSource src1 = APSource.CreateFO("1.fo"))
            APEngine.Append(src1, pdf);
        pdf.Save();
    }

This method can be called an arbitrary number of times merging the formatting results of different source documents into one destination object:

    System.IO.MemoryStream pdfStream = new System.IO.MemoryStream();

    using(APDestination pdf = APDestination.CreatePDF(pdfStream))
    {
        using(APSource src1 = APSource.CreateFO("1.fo"))
            APEngine.Append(src1, pdf);

        using(APSource src2 = APSource.CreateFO("2.fo"))
            APEngine.Append(src2, pdf);

        pdf.Save();
    }

Alternatively, any source document can be used as a backgound via AddBackground method:

    using(APDestination xps = APDestination.CreateXPS("result.xps"))
    {
        using(APSource src = APSource.CreateDocX("source.docx"))
            APEngine.Append(src, xps);

        using(APSource background = APSource.CreateSVG("background.svg"))
            APEngine.AddBackground(src, xps, 0, xps.PageCount-1, true);

        xps.Save();
    }

GDIPlus destination object represents a special output object that can be used for direct print or preview of the formatting results. To print source document, append it to the GDIPlus object using APEngine.Append call and send it directly to a print device (represented by the standard System.Drawing.Printing.PrintDocument object) using GDIPlusPrint call:

    using(APDestination gdiPlus = APDestination.CreateGDIPlus())
    {
        using(APSource src1 = APSource.CreateFO("1.fo"))
            APEngine.Append(src1, gdiPlus);

        System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();

        // ... configure printDoc
        APEngine.GDIPlusPrint(gdiPlus, printDoc);
        printDoc.Print();
    }