Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active August 16, 2019 09:49
Show Gist options
  • Save bjoerntx/56764fca9ef02824566af7465f77d552 to your computer and use it in GitHub Desktop.
Save bjoerntx/56764fca9ef02824566af7465f77d552 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
if (args == null || args.Length < 2) return;
// get read and write pipe handles
// roles are reversed from how the other process is passing the handles
string pipeWriteHandle = args[0];
string pipeReadHandle = args[1];
// create 2 anonymous pipes for duplex communications
using (var pipeRead = new AnonymousPipeClientStream(PipeDirection.In, pipeReadHandle))
using (var pipeWrite = new AnonymousPipeClientStream(PipeDirection.Out, pipeWriteHandle))
{
try
{
var lsValues = new List<string>();
// get message from hosting process
using (var sr = new StreamReader(pipeRead))
{
string sTempMessage;
// wait for "sync message" from the other process
do
{
sTempMessage = sr.ReadLine();
} while (sTempMessage == null || !sTempMessage.StartsWith("SYNC"));
// read until "end message" from the server
while ((sTempMessage = sr.ReadLine()) != null && !sTempMessage.StartsWith("END"))
{
lsValues.Add(sTempMessage);
}
}
// send value to calling process
using (var sw = new StreamWriter(pipeWrite))
{
sw.AutoFlush = true;
// send a "sync message" and wait
sw.WriteLine("SYNC");
pipeWrite.WaitForPipeDrain(); // wait here
PassingObject dataObject =
JsonConvert.DeserializeObject<PassingObject>(lsValues[0]);
ReturningObject returnObject = new ReturningObject();
try
{
// create a new ServerTextControl for the document processing
using (TXTextControl.ServerTextControl tx =
new TXTextControl.ServerTextControl())
{
tx.Create();
tx.Load(dataObject.Document,
TXTextControl.BinaryStreamType.InternalUnicodeFormat);
using (MailMerge mailMerge = new MailMerge())
{
mailMerge.TextComponent = tx;
mailMerge.MergeJsonData(dataObject.Data.ToString());
}
byte[] data;
tx.Save(out data, TXTextControl.BinaryStreamType.AdobePDF);
returnObject.Document = data;
}
sw.WriteLine(JsonConvert.SerializeObject(returnObject));
sw.WriteLine("END");
}
catch (Exception exc)
{
returnObject.Error = exc.Message;
sw.WriteLine(JsonConvert.SerializeObject(returnObject));
sw.WriteLine("END");
}
}
}
catch
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment