Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created July 14, 2022 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/c34505e5aef4a7044a300ebf2ccbd153 to your computer and use it in GitHub Desktop.
Save bjoerntx/c34505e5aef4a7044a300ebf2ccbd153 to your computer and use it in GitHub Desktop.
[HttpPost]
public bool CreateDummyDocument(string CompanyName) {
// create a unique ID
string guid = Guid.NewGuid().ToString();
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
// create ServerTextControl and load template
tx.Create();
tx.Load("App_Data/vendor.tx", TXTextControl.StreamType.InternalUnicodeFormat);
using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge()) {
mailMerge.TextComponent = tx;
// flatten PDF
mailMerge.FormFieldMergeType = TXTextControl.DocumentServer.FormFieldMergeType.Replace;
// create dummy merge data with barcode URL
VendorData data = new VendorData() {
vendor_name = CompanyName,
document_qr = Url.Action("View", "Home", new { id = guid }, protocol: Request.Scheme)
};
// merge template
mailMerge.MergeObject(data);
}
// save results and store in database
byte[] results;
tx.Save(out results, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
using (MemoryStream ms = new MemoryStream(results)) {
using (var db = new LiteDatabase("App_Data/MyData.db")) {
var col = db.GetCollection<Document>("documents");
var document = new Document {
Name = "dummy_" + guid + ".tx",
DocumentId = guid,
Created = DateTime.Now
};
col.Insert(document);
var fs = db.FileStorage;
fs.Upload("$/documents/" + "dummy_" + guid + ".tx", "dummy_" + guid + ".tx", ms);
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment