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
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
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.
touseefbsb
Mihai Lancu
touseefbsb
Mihai Lancu