PDF for UWP | ComponentOne
PDF for UWP Quick Start / Step 3 of 4: Adding a Code File
In This Topic
    Step 3 of 4: Adding a Code File
    In This Topic

    In this step you'll add a code file that will contain the Save method, as well as code that defines both the Offset and the Inflate methods referenced in creating the PDF document's text.

    1. Right-select the name of your application and select Add | Class from the context menu. The Add New Item dialog box appears.
    2. Select Class from the list, and name it PdfUtils.cs. Click OK.
    3. Add the following code in PdfUtils.cs file. The sample below adds the appropriate using statements, and all the code you'll need for the extension methods:
    C#
    Copy Code
    using C1.Xaml.Pdf;
    using System;
    using System.Collections.Generic;
    using Windows.Storage;
    using Windows.Storage.Pickers;
    using Windows.UI.Popups;
    using System.IO;
    using Windows.Foundation;
    
    namespace Pdf_UWP_QS
    {
        public static class PdfUtils
        {
            public static async void Save(this C1PdfDocument pdf)
            {
                FileSavePicker picker = new FileSavePicker();
                picker.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" });
                picker.DefaultFileExtension = ".pdf";
                picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                StorageFile file = await picker.PickSaveFileAsync();
                if (file != null)
                {
                    await pdf.SaveAsync(file);
                    MessageDialog dlg = new MessageDialog("Pdf Document saved to " + file.Path, "PdfSamples");
                    await dlg.ShowAsync();
                }
            }
            public static MemoryStream SaveToStream(this C1PdfDocument pdf)
            {
                MemoryStream ms = new MemoryStream();
                pdf.Save(ms);
                ms.Seek(0, SeekOrigin.Begin);
                return ms;
            }
            // ***********************************************************************
            // Extension methods for Rect
            // ***********************************************************************
            public static Rect Inflate(this Rect rc, double dx, double dy)
            {
                rc.X -= dx;
                rc.Y -= dy;
                rc.Width += 2 * dx;
                rc.Height += 2 * dy;
                return rc;
            }
            public static Rect Offset(this Rect rc, double dx, double dy)
            {
                rc.X += dx;
                rc.Y += dy;
                return rc;
            }
        }
    }
    

    In this step, you added a code file and the code for the extension methods. In the next step, you'll run your application.