Example #1
0
        private void mailMerge1_DataRowMerged(object sender, TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e)
        {
            byte[] data;
            string sPlaceholderStartIF = "%%StartIF%%";
            string sPlaceholderEndIF   = "%%EndIF%%";

            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                tx.Load(e.MergedRow, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                foreach (TXTextControl.IFormattedText part in tx.TextParts)
                {
                    do
                    {
                        int start = part.Find(sPlaceholderStartIF, 0, TXTextControl.FindOptions.NoMessageBox);
                        int end   = part.Find(sPlaceholderEndIF, start, TXTextControl.FindOptions.NoMessageBox);

                        if (start == -1)
                        {
                            continue;
                        }

                        part.Selection.Start  = start;
                        part.Selection.Length = end - start + sPlaceholderEndIF.Length;
                        part.Selection.Text   = "";
                    } while (part.Find(sPlaceholderStartIF, 0, TXTextControl.FindOptions.NoMessageBox) != -1);
                }

                tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
            }

            e.MergedRow = data;
        }
Example #2
0
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            List <MergeBlock> blocks;

            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                byte[] data = null;

                TextControl1.SaveText(out data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);
                tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                blocks = MergeBlock.GetMergeBlocksFlattened(tx);
            }

            // select the selected block in the TextControl.Web
            foreach (MergeBlock block in blocks)
            {
                if (block.Name == TreeView1.SelectedValue)
                {
                    TextControl1.Selection = new TXTextControl.Web.Selection(block.StartMarker.Start, block.Length);
                    break;
                }
            }
        }
        public MergedDocument MergeDocument([FromBody] MergedDocument Document)
        {
            byte[] data;

            Customer customer = new Customer()
            {
                Firstname = "Klaus", Name = "Klaasen"
            };

            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                tx.Load(Convert.FromBase64String(Document.Document), TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                TXTextControl.DocumentServer.MailMerge mm = new TXTextControl.DocumentServer.MailMerge();
                mm.TextComponent = tx;

                mm.MergeObject(customer);

                tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                return(new MergedDocument()
                {
                    Document = Convert.ToBase64String(data)
                });
            }
        }
        public static string MergeWorkorder(Workorder workorder)
        {
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
                tx.Create();

                // load the work order template
                tx.Load(HttpContext.Current.Server.MapPath(
                            "~/App_Data/workorder.tx"),
                        TXTextControl.StreamType.InternalUnicodeFormat);

                MailMerge mm = new MailMerge()
                {
                    TextComponent = tx
                };

                // merge the data into the template
                mm.MergeObject(workorder);

                // generate a unique filename and save the work order
                var sFileName = Helpers.GenerateUID(5);
                tx.Save(HttpContext.Current.Server.MapPath("~/App_Data/" + sFileName + ".tx"), TXTextControl.StreamType.InternalUnicodeFormat);

                return(sFileName);
            }
        }
        public string Merge(DocumentViewModel model)
        {
            // convert the template to a byte array
            byte[] document = Convert.FromBase64String(model.BinaryDocument);

            // load the the data source
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("/App_Data/data/sample_db.xml"), XmlReadMode.Auto);

            byte[] sDocument;

            // create a new ServerTextControl and MailMerge component
            using (TXTextControl.ServerTextControl tx =
                new TXTextControl.ServerTextControl())
            {
                MailMerge mm = new MailMerge();
                mm.TextComponent = tx;

                // load the template and merge
                mm.LoadTemplateFromMemory(document, FileFormat.InternalUnicodeFormat);
                mm.Merge(ds.Tables[0], true);

                // save the document
                mm.SaveDocumentToMemory(out sDocument, TXTextControl.BinaryStreamType.InternalUnicodeFormat, null);
            }

            // encode and return the merged document
            return Convert.ToBase64String(sDocument);
        }
        public DataSelector(DataSet dataSet, byte[] template)
        {
            // use a temporary ServerTextControl instance to recognize blocks with
            // the sorting keyword 'ORDERBY'
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                // load the template
                tx.Load(template, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                // create a list of merge blocks
                List<MergeBlock> lMergeBlocks = MergeBlock.GetMergeBlocks(MergeBlock.GetBlockMarkersOrdered(tx), tx);

                // loop through all merge blocks to check whether they
                // have a sorting parameter
                foreach (MergeBlock mergeBlock in lMergeBlocks)
                {
                    string sBlockName = mergeBlock.StartMarker.TargetName;

                    // check for the unique sorting parameter
                    if (sBlockName.ToUpper().Contains("ORDERBY") == false)
                        continue;

                    // create a new SortedBlock object to store parameter
                    SortedBlock block = new SortedBlock(sBlockName);

                    // remove the sorting parameter from the block name
                    // so that MailMerge can find the matching data in the data source
                    mergeBlock.StartMarker.TargetName = block.Name;
                    mergeBlock.EndMarker.TargetName = "BlockEnd_" + mergeBlock.Name;

                    if (dataSet.Tables.Contains(mergeBlock.Name) == false)
                        continue;

                    // get all DataRows using LINQ
                    var query = from product in dataSet.Tables[mergeBlock.Name].AsEnumerable()
                                select product;

                    // create a new DataTable with the sorted DataRows
                    DataTable dtBoundTable = (block.SortType == SortType.ASC) ?
                        query.OrderBy(product => product.Field<String>(block.ColumnName)).CopyToDataTable() :
                        query.OrderByDescending(product => product.Field<String>(block.ColumnName)).CopyToDataTable();

                    // remove original rows and replace with sorted rows
                    dataSet.Tables[mergeBlock.Name].Rows.Clear();
                    dataSet.Tables[mergeBlock.Name].Merge(dtBoundTable);

                    dtBoundTable.Dispose();
                }

                // save the template
                byte[] data = null;
                tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
                this.Template = data;
            }
        }
        public static BarcodeView CreateBarcode(string Filename)
        {
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
                tx.Create();

                // assemble the barcode URL
                BarcodeData data = new BarcodeData()
                {
                    Barcode = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +
                              "/Home/ViewDocument?document=" +
                              Filename
                };

                // load the barcode template
                tx.Load(HttpContext.Current.Server.MapPath("~/App_Data/barcode.tx"),
                        TXTextControl.StreamType.InternalUnicodeFormat);

                MailMerge mm = new MailMerge()
                {
                    TextComponent = tx
                };

                // merge the barcode
                mm.MergeObject(data);

                BarcodeView view = new BarcodeView()
                {
                    Url = data.Barcode
                };

                // create an return the barcode image
                foreach (TXTextControl.DataVisualization.BarcodeFrame barcode in tx.Barcodes)
                {
                    byte[] imageArray;

                    MemoryStream ms = new MemoryStream();

                    ((TXTextControl.Barcode.TXBarcodeControl)barcode.Barcode).SaveImage(
                        ms, System.Drawing.Imaging.ImageFormat.Png);

                    imageArray = new byte[ms.Length];
                    ms.Seek(0, System.IO.SeekOrigin.Begin);
                    ms.Read(imageArray, 0, (int)ms.Length);

                    view.Image = "data:image/png;base64," + Convert.ToBase64String(imageArray);

                    return(view);
                }

                return(null);
            }
        }
        private void MailMerge_IncludeTextMerging(object sender,
                                                  TXTextControl.DocumentServer.MailMerge.IncludeTextMergingEventArgs e)
        {
            // custom handing of XLSX files
            switch (Path.GetExtension(e.IncludeTextField.Filename))
            {
            case ".xlsx":     // Excel file detected

                if (!File.Exists(e.IncludeTextField.Filename))
                {
                    return;
                }

                // load document into temp. ServerTextControl
                using (TXTextControl.ServerTextControl tx =
                           new TXTextControl.ServerTextControl())
                {
                    tx.Create();

                    // Bookmark name is the sheet name of the Excel document
                    TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings()
                    {
                        DocumentPartName = e.IncludeTextField.Bookmark
                    };

                    try
                    {
                        // load the Excel document
                        tx.Load(
                            e.IncludeTextField.Filename,
                            TXTextControl.StreamType.SpreadsheetML,
                            ls);
                    }
                    catch {
                        return;
                    }

                    byte[] data;

                    // save the document using the TX format
                    tx.Save(out data,
                            TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                    e.IncludeTextDocument = data;     // pass back to the field
                }

                break;
            }
        }
        private void mailMerge1_IncludeTextMerging(object sender, TXTextControl.DocumentServer.MailMerge.IncludeTextMergingEventArgs e)
        {
            byte[] data;

            // create blank document as byte array
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
            }

            // remove the include text field in case the data contains a "0"
            if (ds.Tables[0].Rows[iCurrentDataRow][e.Filename].ToString() == "0")
            {
                e.IncludeTextDocument = data;
            }
        }
        public ActionResult SignDocument()
        {
            Request.InputStream.Position = 0;
            System.IO.StreamReader str = new System.IO.StreamReader(Request.InputStream);
            string sBuf = str.ReadToEndAsync().Result;

            SignDocument jsonDocument = JsonConvert.DeserializeObject <SignDocument>(sBuf);

            byte[] doc = Convert.FromBase64String(jsonDocument.Document);

            // create a new ServerTextControl
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();

                // load the signed document
                tx.Load(doc, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                // save the document as the current signed documet
                tx.Save(Server.MapPath("~/App_Data/documentflows/" + jsonDocument.UniqueId + "/signed.tx"),
                        TXTextControl.StreamType.InternalUnicodeFormat);
            }

            // read the document flow structure
            string jsonFlow = System.IO.File.ReadAllText(
                Server.MapPath("~/App_Data/documentflows/" + jsonDocument.UniqueId + "/documentflow.json"));

            DocumentFlow documentflow = Newtonsoft.Json.JsonConvert.DeserializeObject <DocumentFlow>(jsonFlow);
            Signer       signer       = documentflow.Signers.Find(x => x.Id == int.Parse(jsonDocument.SignerId));

            signer.SignatureComplete = true; // mark as completed

            // save the flow structure
            System.IO.File.WriteAllText(
                Server.MapPath("~/App_Data/documentflows/" + jsonDocument.UniqueId + "/documentflow.json"),
                Newtonsoft.Json.JsonConvert.SerializeObject(documentflow));

            return(new JsonResult()
            {
                Data = true
            });
        }
Example #11
0
        // read all blocks from the current document and fill the TreeView
        private void updateNavigationPanel()
        {
            List <MergeBlock> blocks;

            try
            {
                using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
                {
                    tx.Create();
                    byte[] data = null;

                    TextControl1.SaveText(out data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);
                    tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                    blocks = MergeBlock.GetMergeBlocks(MergeBlock.GetBlockMarkersOrdered(tx), tx);
                }

                TreeView1.Nodes.Clear();
                fillTreeView(blocks);
                TreeView1.ExpandAll();
            }
            catch { }
        }
Example #12
0
        /*-------------------------------------------------------------------------------------------------------
        ** ImportForms method
        **-----------------------------------------------------------------------------------------------------*/
        public static AcroForm[] ImportForms(byte[] Data)
        {
            // check, if the file exists
            if (Data == null)
            {
                throw new InvalidDataException("Data is not valid.");
            }

            string sAcroFormsXml;

            // create a temporary ServerTextControl to import the PDF form XML
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
                ls.PDFImportSettings = TXTextControl.PDFImportSettings.GenerateXML;

                tx.Load(Data, TXTextControl.BinaryStreamType.AdobePDF, ls);

                sAcroFormsXml = tx.Text;
            }

            return(ProcessForm(sAcroFormsXml));
        }
Example #13
0
        /*-------------------------------------------------------------------------------------------------------
        ** ImportForms method
        **-----------------------------------------------------------------------------------------------------*/
        public static AcroForm[] ImportForms(string Filename)
        {
            // check, if the file exists
            if (File.Exists(Filename) == false)
            {
                throw new FileNotFoundException("The specified file could not be found.", Filename);
            }

            string sAcroFormsXml;

            // create a temporary ServerTextControl to import the PDF form XML
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();
                TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
                ls.PDFImportSettings = TXTextControl.PDFImportSettings.GenerateXML;

                tx.Load(Filename, TXTextControl.StreamType.AdobePDF, ls);

                sAcroFormsXml = tx.Text;
            }

            return(ProcessForm(sAcroFormsXml));
        }
Example #14
0
        static void Main(string[] args)
        {
            if (args == null || args.Length < 2)
            {
                return;
            }

            // get read and write pipe handles
            // note: Roles are now reversed from how the other process is passing the handles in
            string pipeWriteHandle = args[0];
            string pipeReadHandle  = args[1];

            // create 2 anonymous pipes (read and write) for duplex communications
            // (each pipe is one-way)
            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 other 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 for the calling process to receive it
                            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
                    {
                    }
                }
        }
Example #15
0
        public ActionResult StoreDocument(
            [FromBody] string Document,
            [FromBody] string UniqueId,
            [FromBody] string SignerName)
        {
            byte[] bPDF;

            // create temporary ServerTextControl
            using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
            {
                tx.Create();

                // load the document
                tx.Load(Convert.FromBase64String(Document),
                        TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings()
                {
                    CreatorApplication = "TX Text Control Sample Application",
                };

                // save the document as PDF
                tx.Save(out bPDF, TXTextControl.BinaryStreamType.AdobePDF, saveSettings);
            }

            // calculate the MD5 checksum of the binary data
            // and store in SignedDocument object
            SignedDocument document = new SignedDocument()
            {
                Checksum = Checksum.CalculateMD5(bPDF)
            };

            // define a Blockchain object
            Blockchain bcDocument;

            // if the blockchain exists, load it
            if (System.IO.File.Exists(
                    Server.MapPath("~/App_Data/Blockchains/" + UniqueId + ".bc")))
            {
                string bc = System.IO.File.ReadAllText(
                    Server.MapPath("~/App_Data/Blockchains/" + UniqueId + ".bc"));

                bcDocument = JsonConvert.DeserializeObject <Blockchain>(bc);
            }
            else
            {
                bcDocument = new Blockchain(true); // otherwise create a new blockchain
            }
            // add a new block to the blockchain and store the SignedDocument object
            bcDocument.AddBlock(new Block(DateTime.Now, null, JsonConvert.SerializeObject(document)));

            // store the blockchain as a file
            System.IO.File.WriteAllText(
                Server.MapPath("~/App_Data/Blockchains/" + UniqueId + ".bc"),
                JsonConvert.SerializeObject(bcDocument));

            // create and return a view model with the PDF and the unique document ID
            StoredDocument storedDocument = new StoredDocument()
            {
                PDF        = Convert.ToBase64String(bPDF),
                DocumentId = UniqueId
            };

            return(new JsonResult()
            {
                Data = storedDocument,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }