I want to open a PDF, read all form fields and identify their position so I can add an annotation next to them.
I've found how to read the field names and to set their values. But I don't see a way to know where it is on the page so I can "write" something next to it.
Hi Keith,
On our further analysis, we have created a workaround for retrieving the form fields page and page index. Please refer to the following code snippet,
static void Main(string[] args) { PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"..\..\FormFields.pdf"); PdfLoadedForm loadedForm = loadedDocument.Form; foreach(var fields in loadedForm.Fields) { if(fields is PdfLoadedTextBoxField textBoxField) { RectangleF bounds = (fields as PdfLoadedTextBoxField).Bounds; //Get the textbox field page var page = textBoxField.Page; //Get text boxfields add page index int index = GetPageIndex(loadedDocument, page); } } } private static int GetPageIndex(PdfLoadedDocument document, PdfPageBase page) { int index = 0;
for (int i = 0; i < document.Pages.Count - 1; i++) { if ((document.Pages[i] as PdfPageBase) == page) { break; } index++; } return index; } |
Regards,
Karmegam
That's excellent. Thanks!!
Expanding on this, how can I identify the form field that a user clicks on in Viewer?
For example, what if I want to capture the field's name or value when the user clicks on it?
At present, we do not have “Form Fields support in WinForms PDF viewer”. However, by hosting our WPF PDF viewer control in WinForms application we can achieve your requirement.
In our PDF viewer WPF control, we have provided most of the Form Fields support. By hosting the PDF viewer WPF control in your WinForms application, we can achieve the requirement of Form Fields support in WinForms platform. Please refer the below KB link and documentation for your reference,
KB Link : https://www.syncfusion.com/kb/8972/how-to-host-pdfviewer-wpf-in-windows-forms-application
Documentation Link: https://help.syncfusion.com/wpf/pdf-viewer/form-filling-in-pdf#retrieve-the-form-field-details
To get the name or the value of form field, we can make use of WPF PdfViewer FormFieldClicked event and CurrentPageIndex API to get the index of the current page displayed in the WPF PdfViewerControl. We have also created a sample for the same and attached below for your reference.
Thank for that. I didn't know it was possible to host WPF controls in winforms.
I've looked at the sample code you linked to and I'm unclear as to how that code is identifying the WPF version of PDFViwer rather than the WinForms version.
PdfViewer wordEditor = new PdfViewer();I think I answered that last question. I just need to uninstall the winforms pdfviewer and install the wpf pdfviewer in Winforms.
But I'm still stuck trying to get my winforms project to work with the wpf control.
In my searching, I got the impression I needed these:
using System.Windows.Forms.Integration; // Namespace for ElementHost
using Syncfusion.UI.Xaml.PdfViewer; // Namespace for WPF PDF Viewer
But those throw errors:
Severity Code Description Project File Line Suppression State Details
Error CS0234 The type or namespace name 'Integration' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?) 1095C E:\@Project Files NEW\Visual Studio Projects 2022\Krueger 1095C\1095C\Form1.cs 6 Active
Severity Code Description Project File Line Suppression State Details
Error CS0234 The type or namespace name 'UI' does not exist in the namespace 'Syncfusion' (are you missing an assembly reference?) 1095C E:\@Project Files NEW\Visual Studio Projects 2022\Krueger 1095C\1095C\Form1.cs 7 Active
So, I'm stuck. Maybe those are the wrong usings? Or if not, there are other steps (references? nugets?) I need to take. But I don't know what those might be.
I guess I should note that I'm running Net Core. Is that supported for this?
After more troubleshooting and research, I THINK I have all the applicable wpf nugets installed.
Again, this is a .Net Core .Net 8 app.
I'm down to just this one error:
Severity Code Description Project File Line Suppression State Details
Error CS0246 The type or namespace name 'PdfViewer' could not be found (are you missing a using directive or an assembly reference?) 1095C E:\@Project Files NEW\Visual Studio Projects 2022\Krueger 1095C\1095C\Form1.cs 101 Active
Here's the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace _1095C;
public partial class Form1 : Form
{
ElementHost elementHost = null;
public Form1()
{
InitializeComponent();
}
void LoadPdfViewer()
{
// It will need to be loaded in the Reports tab as well as the Mappings tab.
// MAPPINGS
// Create the ElementHost control.
elementHost = new ElementHost();
elementHost.AutoSize = true;
elementHost.Size = panel1.Size;
elementHost.Dock = DockStyle.Fill;
panel1.Controls.Add(elementHost);
PdfViewer pdfViewer = new PdfViewer(); <<--- THIS IS THE LINE THROWING THE ERROR
elementHost.Child = pdfViewer;
elementHost.Margin = new Padding(0, 0, 0, 0);
elementHost.Region = new Region();
elementHost.BackColor = System.Drawing.Color.White;
}
}
Currently, we are preparing the sample for hosting the PDF viewer WPF control in your WinForms application for .Net core with .Net 8 project. We will provide the corresponding sample on tomorrow January 30th, 2024.
When hosting the WPF PdfViewer into a WinForms application, it is necessary to utilize the WPF PdfViewer NuGet packages. Similarly, when utilizing a .Net core application such as .Net6, .Net7, or .Net8, it is important to verify the presence of the following tags in the project file. If these tags are not found, the project file must be modified to include them, after which the project file should be reloaded.
|
<UseWindowsForms>true</UseWindowsForms> <UseWPF>true</UseWPF> |
Also, the Namespace for the WPF PdfViewer is “Syncfusion.Windows.PdfViewer” and not the “Syncfusion.UI.Xaml.PdfViewer”. We have also created a simple sample for the same and attached below for your reference.
Thanks for the project sample.
I see that you have this line in form1.
using PdfViewer_Hosted_Sample_7._0;
and that is the namespace of PdfView.cs.
and the related PdfView.xaml.
Do I need those files? Or is it just a sample pdf for loading into the viewer?
I tried running your sample project and got this:
System.Windows.Markup.XamlParseException
HResult=0x80131501
Message='The invocation of the constructor on type 'Syncfusion.Windows.PdfViewer.PdfViewerControl' that matches the specified binding constraints threw an exception.' Line number '10' and line position '10'.
Source=PresentationFramework
StackTrace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at PdfViewer_Hosted_Sample_7._0.PdfView.InitializeComponent() in F:\Downloads New\Syncfusion Samples\WPF_Hosted_PdfViewer_8.0_be990c11\WPF_Hosted_PdfViewer_8.0\PdfView.xaml:line 1
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
TypeInitializationException: The type initializer for 'Syncfusion.Windows.PdfViewer.PdfViewerControl' threw an exception.
Inner Exception 2:
NullReferenceException: Object reference not set to an instance of an object.
|
I see that you have this line in form1. using PdfViewer_Hosted_Sample_7._0; and that is the namespace of PdfView.cs. and the related PdfView.xaml.
Do I need those files? Or is it just a sample pdf for loading into the viewer? |
Yes, we need these files because it is a UserControl of the WPF Framework. In this UserControl file, we have added the WPF PdfViewer. Please note that it is not a sample PDF intended for loading into the viewer. |
|
I tried running your sample project and got this: System.Windows.Markup.XamlParseException HResult=0x80131501 Inner Exception 1: TypeInitializationException: The type initializer for 'Syncfusion.Windows.PdfViewer.PdfViewerControl' threw an exception.
Inner Exception 2: NullReferenceException: Object reference not set to an instance of an object. |
The reported issue occurs on our end when we use trial assemblies (non-licensed assemblies). It will not occur in the licensed assemblies. We confirmed the issue “NullReferenceException occurred due to License message in hosted WPF PdfViewer” as a defect in our product and fix will be included in our upcoming weekly NuGet release, which will be available on February 13th, 2024.
Use the below feedback link to track the status of the reported bug. Note: If you require a patch for the reported issue in any of our Essential Studio Main or SP release versions, then kindly let us know the version, so that we can provide a patch in that version based on our SLA policy.
Disclaimer: “Inclusion of this solution in the weekly release may change due to other factors including but not limited to QA checks and works reprioritization.”
|
We have included the fix for this issue “NullReferenceException occurred due to License message in hosted WPF PdfViewer” in our latest weekly release (24.2.5).
Upgrade to the latest version, to get the issue resolved.
Root Cause:
When hosting the WPF PdfViewer in a WinForms sample, the Window was not initialized properly, hence a crash occurs when attempting to access the Window. To mitigate this issue, we now check if the Window has been initialized before attempting to access it.
Please use the below link to download our latest NuGet,
Nuget Link:
https://www.nuget.org/packages/Syncfusion.PdfViewer.WPF/24.2.5
Feedback Link: