Example #1
0
        private async void GenerateCertificatesStart()
        {
            var dylan = new AppreciationCertificateModel("2018", "Havana", "Dylan Kroft", "Rando Taiwanese",
                                                         "January 1-2, 2018");
            var terran = new AppreciationCertificateModel("2018", "Havana", "Terran Kroft", "Rando Taiwanese",
                                                          "January 1-2, 2018");
            var kk = new AppreciationCertificateModel("2018", "Havana", "Kevin Kuo", "Rando Taiwanese",
                                                      "January 1-2, 2018");

            await CreateCertificate(dylan, 1.ToString());
            await CreateCertificate(terran, 2.ToString());
            await CreateCertificate(kk, 3.ToString());
        }
Example #2
0
        private async Task CreateCertificate(AppreciationCertificateModel model, string name)
        {
            // make a copy of the read-only template
            var templateFile =
                await StorageFile.GetFileFromApplicationUriAsync(
                    new Uri("ms-appx:///Assets/AppreciationCertTemplate.docx"));

            var copiedFile = await templateFile.CopyAsync(ApplicationData.Current.LocalFolder, "Certificates.docx", NameCollisionOption.ReplaceExisting);

            // load it into a new document
            var stream = await copiedFile.OpenReadAsync();

            var templateDoc = new WordDocument(stream.AsStream(), FormatType.Docx);

            // mailmerge
            string[] fieldNames  = { "Year", "Round", "Name", "Title", "Date" };
            string[] fieldValues = { model.Year, model.Round, model.Name, model.Title, model.Date };
            templateDoc.MailMerge.Execute(fieldNames, fieldValues);

            // save the new certificate
            MemoryStream templateStream = new MemoryStream();

            templateDoc.Save(templateStream, FormatType.Docx);
            await SaveFile(templateStream, name + ".docx");

            // convert to PDF
            DocIORenderer renderer = new DocIORenderer();
            PdfDocument   pdf      = renderer.ConvertToPDF(templateDoc);

            renderer.Dispose();
            templateDoc.Close();

            // save the PDF
            MemoryStream outputStream = new MemoryStream();

            pdf.Save(outputStream);
            await SaveFile(outputStream, name + ".pdf");

            pdf.Close();
        }