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.

Thu May 16, 2024 5:43 pm

I've followed the example from the page at /Knowledgebase/Spire.PDF/Spire.PDF-Program-Guide/Image/How-to-change-the-image-on-button-field-in-C, but the button field background is never updated.

I've tried on v4.11.3, 7.2.5,8.6.1, and 10.5.5.

I can create a new PdfButtonField with the Icon property set to an image. But I would like the ability to change it with the PdfButtonWidgetFieldWidget.SetButtonImage method.

CitiesDigital_AD
 
Posts: 5
Joined: Thu May 16, 2024 5:16 pm

Fri May 17, 2024 3:46 am

Hello,

Thanks for the message.
I tested the demo you mentioned with the latest version 10.5.5 and it works on my side.
Please note that in the demo there is a line of code to verify the field name (if (field.Name == "Image")), in your PDF document the field name is not necessarily "Image", it is recommended to use the following code to get the name of the field.

Debug.WriteLine(field.Name);

If there's still problem, please provide your PDF document for our further investigation. You can attach it here or send it to 'Support@e-iceblue.com', thanks in advance!

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Mon May 20, 2024 8:36 pm

Thanks Doris.

Here is the code I'm using to add a button field, then attempting to set the background image, both following the examples:
Code: Select all
void Main()
{
   
   var src= @"C:\Project Files\Test documents\PDF Image Test\Page One.pdf";
   var img = @"C:\Project Files\Test documents\PDF Image Test\top_logo_white.png";
   var fieldAddedPath = $@"C:\Project Files\Test documents\PDF Image Test\{Guid.NewGuid()}.pdf";
   var finalOutputPath = $@"C:\Project Files\Test documents\PDF Image Test\{Guid.NewGuid()}.pdf";
   var fieldName = "btnTest";
   
   CreateButtonField(src,fieldAddedPath, 1, fieldName);
   
   UpdateFields(fieldAddedPath, img, finalOutputPath, fieldName);
   
}


private void CreateButtonField(string src, string dest, int page, string fieldName)
{
   PdfDocument pdf = new PdfDocument(src);         
   pdf.AllowCreateForm = (pdf.Form == null) ? true : false;
   
   if (pdf.Pages.Count < page)
      throw new Exception($"PDF does not contain page {page}.");
      
   int pageInd = page-1;
   var pdfPage = pdf.Pages[pageInd];
      
    var btnField = new PdfButtonField (pdfPage, fieldName);
    btnField.Bounds = new RectangleF(100, 400, 200, 50);            
   btnField.Text ="Hello?";
   pdf.Form.Fields.Add(btnField);

   pdf.SaveToFile(dest);
}



public void UpdateFields(string src, string img, string dest, string fieldName)
{
   PdfDocument pdf = new PdfDocument();
    pdf.LoadFromFile(src, FileFormat.PDF);

    PdfFormWidget form = pdf.Form as PdfFormWidget;

    for (int i = 0; i < form.FieldsWidget.Count; i++)
    {
        if (form.FieldsWidget[i] is PdfButtonWidgetFieldWidget)
        {
            PdfButtonWidgetFieldWidget field = form.FieldsWidget[i] as PdfButtonWidgetFieldWidget;
            if (field.Name == fieldName)
            {
            Console.WriteLine(field.FullName);
            field.SetButtonImage(PdfImage.FromFile(img));
         }
        }
    }

    pdf.SaveToFile(dest);
}

           
private PdfButtonWidgetFieldWidget GetTargetedField(string fieldName, PdfFormWidget form)
{
    for (int i = 0; i < form.FieldsWidget.Count; i++)
    {
        if (form.FieldsWidget[i] is PdfButtonWidgetFieldWidget)
        {
            var field = form.FieldsWidget[i] as PdfButtonWidgetFieldWidget;
            if (field.Name == fieldName)
                return field;
        }
    }

    return null;
}

CitiesDigital_AD
 
Posts: 5
Joined: Thu May 16, 2024 5:16 pm

Mon May 20, 2024 8:38 pm

By the way, I tried to upload the PDF and PNG but got an error message:

403 ERROR
The request could not be satisfied.
Request blocked.

CitiesDigital_AD
 
Posts: 5
Joined: Thu May 16, 2024 5:16 pm

Tue May 21, 2024 7:04 am

Hi,

Thanks for your feedback.
Can you send the test files to email 'support@e-iceblue.com'? Thanks in advance for your assistance.

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Tue May 21, 2024 1:39 pm

Thank you!

I emailed both source files and one of the sample output files.

CitiesDigital_AD
 
Posts: 5
Joined: Thu May 16, 2024 5:16 pm

Wed May 22, 2024 8:08 am

Hi,

Thanks for your sharing.
I have reproduced this issue and logged it into our bug tracking system with SPIREPDF-6775. Our dev team will further look into it. And once's there's any update, I will inform you ASAP.

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Fri Jun 28, 2024 11:29 am

Hi,

Thank you for your patience
Glad to inform you we just released Spire.PDF 10.6.25, which contains the fix of SPIREPDF-6775. Please download it from the following link.

website: https://www.e-iceblue.com/Download/down ... t-now.html
nuget: https://www.nuget.org/packages/Spire.PDF/10.6.25

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.PDF