PDF for UWP | ComponentOne
PDF for UWP Quick Start / Step 2 of 4: Adding Content to the Page
In This Topic
    Step 2 of 4: Adding Content to the Page
    In This Topic

    In this step you'll use code to add content to the document and format it.

    1. Open the MainPage.xaml.cs file associated with your application. Add the following using statements to the top of your page:
    C#
    Copy Code
    using C1.Xaml.Pdf;
    using Windows.UI;
    using Windows.Storage;
    using Windows.Storage.Pickers;
    using Windows.UI.Popups;
    
    1. Create a C1PdfDocument by adding the following code directly below the page constructor:
    C#
    Copy Code
    C1PdfDocument pdf;
    
    1. Directly below the InitializeComponent() method, add a MainPage_Loaded method and create a new C1PdfDocument:
       
    C#
    Copy Code
    this.Loaded += MainPage_Loaded;
    pdf = new C1PdfDocument(PaperKind.Letter);
    pdf.Clear();
    
    1. Then add the MainPage_Loaded event:
    C#
    Copy Code
    async void MainPage_Loaded(object sender, RoutedEventArgs e)
    {            
        progressRing.IsActive = true;
        CreateDocumentText(pdf);
        await c1PdfViewer1.LoadDocumentAsync(PdfUtils.SaveToStream(pdf));
        progressRing.IsActive = false;         
    }
    
    1. Below the added event, add the code that will add content to your Pdf document and format it:
    C#
    Copy Code
    static void CreateDocumentText(C1PdfDocument pdf)
    {
        // use landscape for more impact
        pdf.Landscape = true;
        // measure and show some text
        var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmood tempor incididunt ut labore magna aliqua. Ut enim ad minim veniam, quis nostrud excecitation ullmanco laboris. nisi ut alquip ex ea commodo consequat.";
        var font = new Font("Segoe UI Light", 14, PdfFontStyle.Italic);
        // create StringFormat used to set text alignment and line spacing
        var fmt = new StringFormat();
        fmt.LineSpacing = -1.5; // 1.5 char height
        fmt.Alignment = HorizontalAlignment.Center;
        // measure it
        var sz = pdf.MeasureString(text, font, 72 * 3, fmt);
        var rc = new Rect(pdf.PageRectangle.Width / 2, 72, sz.Width, sz.Height);
        rc = PdfUtils.Offset(rc, 110, 0);
        // draw a rounded frame
        rc = PdfUtils.Inflate(rc, 0, 0);
        pdf.FillRectangle(Windows.UI.Colors.Teal, rc, new Size(0, 0));
        //pdf.DrawRectangle(new Pen(Colors.DarkGray, 5), rc, new Size(0, 0));
        rc = PdfUtils.Inflate(rc, -10, -10);
        // draw the text
        pdf.DrawString(text, font, Windows.UI.Colors.White, rc, fmt);
        // now draw some text rotating about the center of the page
        rc = pdf.PageRectangle;
        rc = PdfUtils.Offset(rc, rc.Width / 2.2, rc.Height / 2.5);
        // build StringFormat used to rotate the text
        fmt = new StringFormat();
        // rotate the string in small increments
        var step = 6;
        text = "C1PDF works in Windows Runtime!";
        for (int i = 0; i <= 360; i += step)
        {
            fmt.Angle = i;
            font = new Font("Courier New", 8 + i / 30.0, PdfFontStyle.Bold);
            byte b = (byte)(255 * (1 - i / 360.0));
            pdf.DrawString(text, font, Windows.UI.Color.FromArgb(0xff, b, b, b), rc, fmt);
        }
    }
    
    1. Add the code that will call the Save method you'll add in a separate code file:
    C#
    Copy Code
    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        PdfUtils.Save(pdf);
    }