0

I am creating a UWP app ( windows 10 universal app ) with C#/XAML. it is a model release/ property release app. So I have the following template.

Click here to see the template

I want to do following with C#

  1. Read the template pdf file.
  2. Fill in the form info like ( name, age etc ).
  3. Insert an image in top right corner ( as seen in the template) save the pdf file into new location.

I have already tried syncfusion api for pdf, but it isn't compatible with UWP .NET CORE project for some reason.

Please let me know if there is any library for this task. I will really appreciate any kind of help

0

Syncfusion Essential PDF for UWP supports .NET Core as well. Please try this sample code.

            //Loading Pdf and image to stream

         Stream jpegImage = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("templateForm.DataUWP.images.png");
        Stream  docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("templateForm.DataUWP.Form.pdf");


        PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

        //Draw image 

        PdfImage img = new PdfBitmap(jpegImage);

        loadedDocument.Pages[0].Graphics.DrawImage(img, new PointF(380, 90), new SizeF(100, 100));

        //Get the loaded form.

        PdfLoadedForm loadedForm = loadedDocument.Form;

        PdfLoadedFormFieldCollection fieldCollection = loadedForm.Fields as PdfLoadedFormFieldCollection;

        PdfLoadedField loadedField = null;

        // Get the field using TryGetField Method and fill it.


        if (fieldCollection.TryGetField("name", out loadedField))

        {

            (loadedField as PdfLoadedTextBoxField).Text = "John";

        }

        //Save the modified document.
        MemoryStream streamOut = new MemoryStream();
        loadedDocument.Save(streamOut);

        //Close the document

        loadedDocument.Close(true);

        //Save document to file
        Save(streamOut, "sample.pdf");
1

XFINIUM.PDF library supports Windows 10 UWP.
Your template does not include any fields so I modified it. The code to fill the form looks like this:

private Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;

private async void btnFillDocumentTemplate_Click(object sender, RoutedEventArgs e)
{
    Stream pdfStream = assembly.GetManifestResourceStream("UWP_FillDocumentTemplate.ModelReleaseTemplate.pdf");
    PdfFixedDocument document = new PdfFixedDocument(pdfStream);
    pdfStream.Dispose();

    // Fill photographer info
    document.Form.Fields["PhotographerName"].Value = "John Doe";
    document.Form.Fields["PhotographerDateSigned"].Value = "09/03/2017";
    document.Form.Fields["PhotographerShootDate"].Value = "08/03/2017";
    document.Form.Fields["PhotographerShootCountry"].Value = "Italy";
    document.Form.Fields["PhotographerShootDescription"].Value = "Tuscany";
    // Draw photographer signature
    DrawImageAtFieldLocation(document.Form.Fields["PhotographerSignature"].Widgets[0], "PhotographerSignature.png");

    // Fill model info
    document.Form.Fields["ModelName"].Value = "Jane Doe";
    document.Form.Fields["ModelDOB"].Value = "01/01/1980";
    document.Form.Fields["ModelGender"].Value = "Male";
    document.Form.Fields["ModelAddress1"].Value = "via Romana no 1";
    document.Form.Fields["ModelCity"].Value = "Rome";
    document.Form.Fields["ModelState"].Value = "Rome";
    document.Form.Fields["ModelCountry"].Value = "Italy";
    document.Form.Fields["ModelZip"].Value = "12345";
    document.Form.Fields["ModelPhone"].Value = "0123456789";
    document.Form.Fields["ModelEmail"].Value = "jane.doe@nomail.com";
    document.Form.Fields["ModelDateSigned"].Value = "09/03/2017";
    document.Form.Fields["ModelParentName"].Value = "Parent Jane Doe";
    // Draw model signature
    DrawImageAtFieldLocation(document.Form.Fields["ModelSignature"].Widgets[0], "ModelSignature.png");
    // Draw model visual
    DrawImageAtFieldLocation(document.Form.Fields["ModelVisualReference"].Widgets[0], "XFINIUM.PDF.png");

    // Fill ethnicity
    (document.Form.Fields["EthnicityAsian"] as PdfCheckBoxField).Checked = true;

    // Fill witness info
    document.Form.Fields["WitnessName"].Value = "Witness John Doe";
    document.Form.Fields["WitnessDateSigned"].Value = "09/03/2017";
    // Draw photographer signature
    DrawImageAtFieldLocation(document.Form.Fields["WitnessSignature"].Widgets[0], "WitnessSignature.png");

    // If you want to prevent changes to filled data, you can flatten the form fields.
    document.Form.FlattenFields();

    // Save the filled form
    FileSavePicker fileSavePicker = new FileSavePicker();
    fileSavePicker.FileTypeChoices.Add("PDF files", new List<string>() { ".pdf" });
    fileSavePicker.SuggestedFileName = "ModelReleaseFilled";

    var outputFile = await fileSavePicker.PickSaveFileAsync();
    if (outputFile != null)
    {
        var destPdfStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite);
        using (Stream stm = destPdfStream.AsStream())
        {
            document.Save(stm);
            await stm.FlushAsync();
        }
        destPdfStream.Dispose();
    }
}

private void DrawImageAtFieldLocation(PdfFieldWidget fieldWidget, string imageName)
{
    Stream imageStream = assembly.GetManifestResourceStream("UWP_FillDocumentTemplate." + imageName);
    PdfPngImage image = new PdfPngImage(imageStream);
    PdfVisualRectangle fieldRect = fieldWidget.VisualRectangle;
    // Draw the image on the page where the field widget is located.
    fieldWidget.Page.Graphics.DrawImage(image,
        fieldRect.Left, fieldRect.Top, fieldRect.Width, fieldRect.Height);

    imageStream.Dispose();
}

You can download the filled form and the sample project.
Disclaimer: I work for the company that develops this library.

  • thanks a lot, can you please tell me how you modified the file to include fields? – touseefbsb Mar 12 '17 at 17:48
  • @touseef I used Adobe Acrobat to add the form fields. – Mihai Iancu Mar 12 '17 at 18:40
  • can you also please show me a way how I can add a custom text taken from the user and replace the text ( agreement writing ) in the pdf template? – touseefbsb Mar 13 '17 at 7:43
  • PDF is a fixed fomat so replacing text does not generate a recreation of page layout, like in MS Word. If you replace text, the new text can overlap the surrounding text or large spaces can appear in text. We do not support text replace yet. – Mihai Iancu Mar 13 '17 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.