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 Jul 04, 2019 7:06 am

Hello,

is there a way to replace text with a Checkbox in a PDF using Spire.PDF ?

Basically it would be similar to this acticle which describes the same thing using Spire.Doc:
replace-text-with-checkbox-t7311.html


So in the PDF there would be several placeholders ("X") where I need to insert a Checkbox, similar to this:

Code: Select all
X Insert signature
X Insert image
X Print after save


I can find the "X" - but how could I replace them with a Checkbox ?

Code: Select all
PdfDocument pdf = new PdfDocument(@"c:\temp\test.pdf");
PdfTextFind[] result = null;
foreach (PdfPageBase page in pdf.Pages)
{
   result = page.FindText("X").Finds;   
   foreach (PdfTextFind find in result)
   {
      //Replace "X" with Checkbox here
   }
}
pdf.SaveToFile(@"c:\temp\test_with_Checkboxes.pdf");


Thank you

Heinz4711
 
Posts: 20
Joined: Thu Feb 07, 2019 5:22 pm

Thu Jul 04, 2019 8:45 am

Hello,

Thanks for your inquiry.
Sorry that Spire.PDF doesn't support replacing text with a checkbox directly like the same performance as using Spire.DOC, since all the contents of PDF are drawn on canvas. However, our Spire.PDF supports getting the location information of a searched string. As a workaround, you can try to cover the text with an empty string and then add check box field at the same location like the following sample code. Here, I also attached my input PDF file and output PDF file for your reference. If there is any question, just feel free to write back.
Code: Select all
 PdfDocument pdf = new PdfDocument(@"input.pdf");
 pdf.AllowCreateForm = true;
 PdfTextFind[] result = null;
 foreach (PdfPageBase page in pdf.Pages)
 {
     result = page.FindText("X").Finds;
     for (int i = 1; i <= result.Length; )
     {
         foreach (PdfTextFind find in result)
         {
             string checkBoxName = string.Format("TextBox{0}", i);
             PdfCheckBoxField checkbox = new PdfCheckBoxField(page, checkBoxName);
             //cover "X" with empty string
             find.ApplyRecoverString(string.Empty, System.Drawing.Color.White, true);
             //get the location information
             float x = find.Position.X;
             float y = find.Position.Y;
             checkbox.Bounds = new RectangleF(x, y, 10, 15);
             //add check box field
             pdf.Form.Fields.Add(checkbox);
             i++;
         }
     }
 }
 pdf.SaveToFile(@"output.pdf");

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Wed Jul 10, 2019 2:20 am

Hello,

Greetings from E-iceblue.
Did the workaround solution help you? Thanks in advance for your feedback and time.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF

cron