Bibliothek zum Bearbeiten von PDF-Dateien mit C# in UWP

Ich erstelle eine UWP-App (Windows 10 Universal App) mit C#/XAML. es ist eine Model-Release-/Property-Release-App. Also ich habe folgende Vorlage.

Klicken Sie hier, um die Vorlage anzuzeigen

Ich möchte folgendes mit C # tun

  1. Lesen Sie die PDF-Vorlagendatei.
  2. Füllen Sie die Formularinformationen wie (Name, Alter usw.) aus.
  3. Fügen Sie ein Bild in die obere rechte Ecke ein (wie in der Vorlage zu sehen), speichern Sie die PDF-Datei an einem neuen Ort.

Ich habe die Syncfusion-API für PDF bereits ausprobiert, aber sie ist aus irgendeinem Grund nicht mit dem UWP .NET CORE-Projekt kompatibel.

Bitte teilen Sie mir mit, ob es für diese Aufgabe eine Bibliothek gibt. Ich werde wirklich jede Art von Hilfe schätzen

Antworten (2)

Syncfusion Essential PDF für UWP unterstützt auch .NET Core. Bitte versuchen Sie diesen Beispielcode .

            //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");

Die XFINIUM.PDF- Bibliothek unterstützt Windows 10 UWP.
Ihre Vorlage enthält keine Felder, daher habe ich sie geändert. Der Code zum Ausfüllen des Formulars sieht folgendermaßen aus:

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();
}

Sie können das ausgefüllte Formular und das Beispielprojekt herunterladen .
Haftungsausschluss: Ich arbeite für das Unternehmen, das diese Bibliothek entwickelt.

Vielen Dank, können Sie mir bitte sagen, wie Sie die Datei so geändert haben, dass sie Felder enthält?
@touseef Ich habe Adobe Acrobat verwendet, um die Formularfelder hinzuzufügen.
Können Sie mir bitte auch einen Weg zeigen, wie ich einen benutzerdefinierten Text hinzufügen kann, der vom Benutzer genommen wurde, und den Text (Vereinbarungsschreiben) in der PDF-Vorlage ersetzen kann?
PDF ist ein festes Format, sodass das Ersetzen von Text keine Neuerstellung des Seitenlayouts wie in MS Word erzeugt. Wenn Sie Text ersetzen, kann der neue Text den umgebenden Text überlappen oder es können große Lücken im Text erscheinen. Wir unterstützen noch keine Textersetzung.