Search code examples
c#pdfwindows-phone

How to draw a PDF or open a PDF and add annotation in C# windows phone programming?


I want to have a sample which shows me to open or create PDF with annotation facility in C#


Solution

  • You will need a 3rd party library for creating PDF files on Windows Phone as the .NET Framework does not include an API for working with PDF files.
    The code below shows how to create a PDF file and add a text annotation to it using XFINIUM.PDF library:

    // Create a new document and add a page to it
    PdfFixedDocument document = new PdfFixedDocument();
    PdfPage page = document.Pages.Add();
    
    // Create the text annotation and set its properties.
    PdfTextAnnotation ta = new PdfTextAnnotation();
    ta.Author = "John Doe";
    ta.Contents = "I am a text annotation.";
    ta.IconName = "Note";
    // Add the annotation to the page
    page.Annotations.Add(ta);
    ta.Location = new PdfPoint(50, 50);
    
    // Save the document
    // Get a local folder for the application.
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    
    StorageFile pdfFile = await storageFolder.CreateFileAsync("sample.pdf", CreationCollisionOption.ReplaceExisting);
    var pdfStream = await pdfFile.OpenAsync(FileAccessMode.ReadWrite);
    
    // Convert the random access stream to a .NET Stream and save the document.
    using (Stream stm = pdfStream.AsStream())
    {
        document.Save(stm);
        await stm.FlushAsync();
    }
    pdfStream.Dispose();
    
    MessageDialog messageDialog = new MessageDialog("File(s) saved with success to application's local folder.");
    await messageDialog.ShowAsync();
    

    Disclaimer: I work for the company that develops XFINIUM.PDF library.