PDF for WinRT
Step 3 of 4: Adding a Code File

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, in this case, the YourApplicationName.Windows (Windows 8.1) file, and select Add | New Item from the context menu. The Add New Item dialog box will appear.
Note: In a UniversalApp application, you'll need to select either the Windows 8.1 or Windows Phone app to add the code file. The code file option is not available to add to the entire application.
  1. Select Code File from the list, and name it PdfUtils.cs. Click OK.
  2. When the code file opens, add the following code. 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 System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml;
namespace PDFNewQSTest
{
    /// <summary>
    /// Utility class on top of C1.Xaml.Pdf.
    /// </summary>
    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.

 

 

 

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback