Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Oct 26, 2021 9:02 am

Hello!

We have a PDF file which we want to fill with customer info such as name etc. I'm trying to copy your example : Fill Form Fields in PDF File in C#

The problem is, I can't convert a PDFField (or even check which typeof it is) to another type (for example to PDFTextBoxFieldWidget). I get a syntax error saying "PDFField can never be of type PDFTextBoxFieldWidget". This is on row 014 to 015 in your example :

014 If TypeOf field Is PdfTextBoxFieldWidget Then
015 Dim textBoxField As PdfTextBoxFieldWidget = TryCast(field, PdfTextBoxFieldWidget)


Dim pdfMall As New PdfDocument
pdfMall.LoadFromFile("test.pdf")
Dim formWidget As PdfFormWidget = TryCast(pdfMall.Form, PdfFormWidget)


For i As Integer = 0 To formWidget.FieldsWidget.List.Count - 1
Dim field As PdfField = TryCast(formWidget.FieldsWidget.List(i), PdfField)

If TypeOf field Is PdfTextBoxFieldWidget Then <-- This is not working for me (since field already has a type)

End If

What am I doing wrong here?

Best,

Elin

elinarctic
 
Posts: 27
Joined: Thu Oct 21, 2021 11:39 am

Tue Oct 26, 2021 9:25 am

I managed to fix it myself :)

What i did instead was to check what the list element's type where instead:

Dim elementList = formWidget.FieldsWidget.List


For i As Integer = 0 To elementList.Count - 1
'Dim field As PdfField = TryCast(formWidget.FieldsWidget.List(i), PdfField)

If TypeOf elementList(i) Is PdfTextBoxFieldWidget Then

Dim textBoxField As PdfTextBoxFieldWidget = TryCast(elementList(i), PdfTextBoxFieldWidget)

Select Case textBoxField.Name
Case "fnr"
textBoxField.Text = fnr
Case "namn"
textBoxField.Text = kundnamn
Case "adress"
textBoxField.Text = adress & " " & postnr & " " & ort
End Select

End If
Next

elinarctic
 
Posts: 27
Joined: Thu Oct 21, 2021 11:39 am

Tue Oct 26, 2021 9:44 am

Hello Elin,

Thanks for using our Spire.PDF.
Glad to hear that you have solved your issue.

Feel free to contact us if you have any issues.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Oct 27, 2021 6:24 am

Hello again!

Now I seem to be running into another problem...

We're trying to merge some pdf files to one complete pdf file. One of these pdf files is this autofilled pdf (with customer info). The content in this one differs (meaning the customer name will be different etc). So let's say that we do this for 10 different customers. I've made a function that fills all the fields in the autofilled pdf and saves it as a temp pdf file. The first time in the loop this works fine, but for customer no 2 it says that the temp pdf file is being used by another process (the program crashes). I've been debugging this for a while now and it seems like the PdfDocumentBase doesn't get closed properly...

Here is my function that fills the pdf per customer:

Public Sub fillAGToPDFMergeSpire(dItems As DataGridViewRow)


Dim kundnamn = dItems.Cells("Kundnamn").Value.ToString
Dim adress = dItems.Cells("Adress").Value.ToString
Dim postnr = dItems.Cells("Postnr").Value.ToString
Dim ort = dItems.Cells("Ort").Value.ToString
Dim fnr = dItems.Cells("Fnr").Value.ToString
Dim template = "test.pdf"



Dim pdfMall As New PdfDocument
pdfMall.LoadFromFile(template)
Dim formWidget As PdfFormWidget = TryCast(pdfMall.Form, PdfFormWidget)

Dim elementList = formWidget.FieldsWidget.List


For i As Integer = 0 To elementList.Count - 1

If TypeOf elementList(i) Is PdfTextBoxFieldWidget Then

Dim textBoxField As PdfTextBoxFieldWidget = TryCast(elementList(i), PdfTextBoxFieldWidget)

Select Case textBoxField.Name
Case "fnr"
textBoxField.Text = fnr
Case "namn"
textBoxField.Text = kundnamn
Case "adress"
textBoxField.Text = adress & " " & postnr & " " & ort
End Select

End If
Next


pdfMall.SaveToFile("C:\testSpire\tmpAG.pdf")


pdfMall.Dispose()
pdfMall.Close()
pdfMall = Nothing
End Sub

And here's a piece of code that's doing the merge:

For filesToMerge = 0 To fileNames.Length - 1
filesAndAppendixes = New List(Of String)
fnVariant = SkaFörnyas.Rows(filesToMerge).Cells("förnyelseVariant").Value
serviceTyp = SkaFörnyas.Rows(filesToMerge).Cells("serviceombudstyp").Value

If IsDBNull(fnVariant) OrElse IsDBNull(serviceTyp) Then
villkor = "test.pdf"
Else
If fnVariant = 2 Then
fnVariant = 1
End If

If fnVariant = 3 Then
fnVariant = 0
End If
villkor = getVariableFromDB("Select villkor from dbo.tblVillkor where förnyelsevariant = " & fnVariant & " and serviceombudstyp = " & serviceTyp)
End If

filesAndAppendixes.Add(fileNames(filesToMerge))
filesAndAppendixes.Add(villkor)

If SkaFörnyas.Rows(filesToMerge).Cells("appendAg").Value = 1 Then
fillAGToPDFMergeSpire(SkaFörnyas.Rows(filesToMerge))

filesAndAppendixes.Add(autogiro)
filesAndAppendixes.Add(blanc)
End If

If SkaFörnyas.Rows(filesToMerge).Cells("appendIdd").Value = 1 Then
filesAndAppendixes.Add(idd)
filesAndAppendixes.Add(blanc)
End If

If SkaFörnyas.Rows(filesToMerge).Cells("appendFk").Value = 1 Then
filesAndAppendixes.Add(förköp)
End If

If SkaFörnyas.Rows(filesToMerge).Cells("appendGdpr").Value = 1 Then
filesAndAppendixes.Add(gdpr)
End If

Dim pdfDoc As PdfDocument = New PdfDocument
Dim doc As PdfDocumentBase = pdfDoc.MergeFiles(filesAndAppendixes.ToArray)

doc.Save(pathToSave & "\merged(" & filesToMerge & ").pdf")

pdfDoc.Dispose()
pdfDoc.Close()
doc.Dispose()
doc.Close(True)

If filesAndAppendixes IsNot Nothing Then
filesAndAppendixes = Nothing
End If
'mergeFiles(filesToMerge)

Next

I've tried to debug to see if the tmpAG file is still open by another process after the call to fillAGToPDFMergeSpire-function, but it's not. It seems like the PdfDocumentBase doesn't closes properly.

What am I doing wrong here?

Best,

Elin

elinarctic
 
Posts: 27
Joined: Thu Oct 21, 2021 11:39 am

Wed Oct 27, 2021 10:07 am

Hello,

Thank you for your feedback.
According to the code you provided, I guess that multiple people are using one PDF file simultaneously in your project.If my guess is correct, please try to load PDF stream by pdfMall.LoadFromStream(inputStream). If the issue still exists after trying, please provide your project and test PDF file for our further investigation. You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1647
Joined: Wed Apr 07, 2021 2:50 am

Thu Oct 28, 2021 6:18 am

There aren't multiple persons using the same pdf the same time, it's only me running the program. What I have noticed, is that it seems like it runs multiple things at the same time (meaning it starts with customer no 2 before it has finished customer no 1, and therefore it seems like the PDF is still used by another process).

What I did to solve it for now, is that I do all the steps per customer, meaning I will finish the merging to one complete pdf for one customer at a time before I start with the next one. But I might try with the PDF stream as you mentioned and see if that work as well.

Best,

Elin

elinarctic
 
Posts: 27
Joined: Thu Oct 21, 2021 11:39 am

Thu Oct 28, 2021 10:12 am

Hi,

Thanks for your response.
I'm looking forward to your further test feedback.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1647
Joined: Wed Apr 07, 2021 2:50 am

Fri Oct 29, 2021 7:57 am

Hello again. I tried to load the template from a PDF stream as well and I get the same error. See the attached picture (the error is in swedish but it says that the file is being used by another process).


My program seems to be working with multiple customers in parallell. I run a loop from a datatable, and each row is one customer. So let's say I have 10 customers, then it should take the frontpage for that customer and add the appendixes. I add all the pdf files to a list and when the list is filled with all the pdf pages I want for that particular customer, I do the merge (and then I clear the list and do the same for next customer and so on). As I said earlier, one of these PDF pages is this autofilled one (which I sent you my function for). It seems like that when I save the autofilled PDF as a tmp file, it hasn't been closed for the next customer who wants to use it.

/ Elin

elinarctic
 
Posts: 27
Joined: Thu Oct 21, 2021 11:39 am

Fri Oct 29, 2021 9:37 am

Hello,

Thank you for your further response.
I checked the code you provided and combined it with the information you provided earlier. I suggest you use a different file name when saving the file in the fillAGToPDFMergeSpire function, or save it directly as a PDF stream and return there. Below is the reference code.
Code: Select all
//Save the file as a different file name
Dim nonceStr As String = Guid.NewGuid.ToString
pdfMall. SaveToFile(("C:\testSpire\"+ (nonceStr + "tmpAG.pdf")))

//Save as a stream file
Dim memoryStream As MemoryStream = New MemoryStream
pdfMall. SaveToStream(memoryStream, FileFormat.PDF)
Return memoryStream

//Merge PDF stream
Dim doc As PdfDocumentBase = PdfDocument.MergeFiles(Stream(), stream)

If the issue still exists after trying, please provide your project and test PDF file for our further investigation. You could upload them here or send them to us via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1647
Joined: Wed Apr 07, 2021 2:50 am

Tue Nov 02, 2021 9:23 am

Hello again!

We managed to make it work with another solution; now we do the merging per customer instead (and not in parallell). This works fine. I was also wondering, do you know anything/have any updates on SPIREDOC-6794 (I saw now that my errand has the code SPIREDOC-6849) errand? This one was about elements getting out of context when saving word file to PDF.

Best,

Elin

elinarctic
 
Posts: 27
Joined: Thu Oct 21, 2021 11:39 am

Tue Nov 02, 2021 9:51 am

Hello,

Thank you for your feedback.
Glad to hear that you solved this issue. The two issues SPIREDOC-6794 and SPIREDOC-6849 have been resolved, and they are currently in the testing phase. If the tests go well, we will provide a hotfix for you as soon as possible. Please give us more time, thanks for your understanding.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1647
Joined: Wed Apr 07, 2021 2:50 am

Tue Nov 02, 2021 12:08 pm

Okay, super! Thank you so much for the update :)

Best,

Elin

elinarctic
 
Posts: 27
Joined: Thu Oct 21, 2021 11:39 am

Wed Nov 03, 2021 1:53 am

Hello,

You're welcome.
If you have other questions about using our products in the future, please feel free to contact us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1647
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF