I'll be back from the dead soon

In which we merge PDFs while rendering them via Ecrion's tools.

But first - I know it has been almost a year since I last posted. I even told myself I would post at least once a month this year. Well, we are 5 months into 2015, and I am not exactly setting the world on fire! As it happens, I am in the final stage of a multi year project that is becoming more "exciting" with each passing day. I have plenty of things to write about with none of the time required to do so. I hope that changes soon. In the meantime, here is a little snippet resulting from the project I am working on.

To make a rather long and winding story short, as part of the project I am working on, I have to generate PDFs of various types, and some of these PDFs are composed of multiple constituent PDFs.  To do this we are using Ecrion's Rendering Server and Data Aggregation Server.

Searching for information on how to merge PDFs with Ecrion's tools results in recommendations to purchase Ecrion's Digital Assembly Line or use some version of the code below.  The former is an expensive proposition just to merge PDFs, and the latter seems to be written against a previous version of Ecrion's tools.

using Ecrion.XF;
static void mergeOutputs(string[] inputFiles, string outputFile, 
                         Ecrion.XF.OutputFormat fmt)
{
    using(XFMergeContext ctx = new XFMergeContext())
    {
        ctx.OutputFormat = fmt;
        ctx.SetOutput(outputFile);
        for each(string inFile in inputFiles)
        {
            using (XFDocument doc = new XFDocument())
            {
                doc.LoadUrl(inFile);
                doc.ExportTo(ctx);
            }
		}
	}
}
In XF 2015, XFMergeContext no longer exists. Instead you need to
  1. Request a MergeContextId from the Engine
  2. Set the MergeContextId in each PDF's RenderingParameters to the Id from 1
  3. Set IsLastDocumentInMergeContext = true in the RenderingParameters for the last PDF
  4. Render all PDFs to the same stream

Here is a very simple merge of two PDFs to demonstrate this:

// Request your data diagrams from the server
// Not needed if you are not using the Data Aggregation Server
// For instance if you are using XML for your data source
var diagram1 = new ServerDiagram("[DIAGRAM_NAME]");
var jp1 = new JobParameters { Diagram = diagram1, Server = "[SERVER_NAME]" };
var dataSource1 = new DASDataSource(jp1);

var diagram2 = new ServerDiagram("[DIAGRAM_NAME]");
var jp2 = new JobParameters { Diagram = diagram2, Server = "[SERVER_NAME]" };
var dataSource2 = new DASDataSource(jp2);

var engine = new Ecrion.Ultrascale.Engine();
// All PDFs you want to render together need to be sent with the dame Id
var id = Ecrion.Ultrascale.Engine.GetNewMergeContextID();

var paramDiagram1 = new RenderingParameters { 
	OutputFormat = Ecrion.Ultrascale.Engine.OutputFormat.PDF,
	Template = new ServerDocumentTemplate("[TEMPLATE_NAME]"), Server = "[SERVER_NAME]",
	// Set the MergeContextID
	MergeContextID = id};

var paramDiagram2 = new RenderingParameters {
	OutputFormat = Ecrion.Ultrascale.Engine.OutputFormat.PDF,
	Template = new ServerDocumentTemplate("[TEMPLATE_NAME]"), Server = "[SERVER_NAME]",
	// Use the same MergeContextID as before
	MergeContextID = id,
	// Tell the engine this is the last one
	IsLastDocumentInMergeContext = true};

using (var outputStream = new FileStream(@"FileName.pdf", FileMode.Create))
{
	// Render both to the same stream
	engine.Render(dataSource1, outputStream, paramDiagram1);
	engine.Render(dataSource2, outputStream, paramDiagram2);
}
Please note: the above was written to (attempt to) clearly demonstrate the idea. It is not intended to be the best or most efficient code possible. That I leave to you dear reader.
Loading