How to make Textboxes editable

Hi Team,

I have been trying to make few section of my word document editable. With protect method, it protects the entire page . I also tried with doc.getSections().get(0).setProtectedForForms(false);

It is allowing me to make the section editable only if its a paragraph, but it is not allowing me to edit a Textbox if I place in the same section. Please assist me on how to make Objects like Textboxes,Charts to make them editable on a protected Document.

Thanks,
Prasanna

@pshanmuganathan,

You can define different Editable Ranges in your document to meet this requirement. The following sample code demonstrates how you can protect document and then define some Editable Range. You can incorporate the same functionality in your project. Hope, this helps.

Document doc = new Document();
doc.removeAllChildren();

Section section = new Section(doc);
doc.appendChild(section);

Body body = new Body(doc);
section.appendChild(body);

Paragraph para = new Paragraph(doc);
body.appendChild(para);

Run run = new Run(doc);
run.setText("Hello ");
run.getFont().setColor(Color.RED);
para.appendChild(run);

Run run2 = new Run(doc);
run2.setText("World!");
para.appendChild(run2);

doc.protect(ProtectionType.READ_ONLY);

DocumentBuilder builder = new DocumentBuilder(doc);

EditableRangeStart start = builder.startEditableRange();
EditableRangeEnd end = builder.endEditableRange();

run2.getParentNode().insertBefore(start, run2);
run2.getParentNode().insertAfter(end, run2);

doc.save("D:\\temp\\awjava-18.10.0.docx");

Hi Awais,

Thanks for the above. However, objects like Textboxes becomes uneditable though I place them between editable ranges. Please get back if you have more questions.

@pshanmuganathan,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 18.11 generated output DOCX file showing the undesired behavior
  • Your expected DOCX document showing the correct output. You can create expected document by using MS Word.
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information. Thanks for your cooperation.

Aspose Support.zip (24.4 KB)
Hi Awais,

I have attached the required. Please check the Input and Output File. I am trying to create editable regions between the words “Check1” and “Check2”. Between them we have the contents below,

Text content : asdsadsadsadsa
and a Text box.

In the output rendered document, I am able to edit the text content but i am not able to edit the Text inside the Textbox as it gets locked .

Please check my Java sample application as well . I have tried to used the start and end editable regions method of the builder to make the contents editable.

Get back if you have any questions.

Thanks,
Prasanna

@pshanmuganathan,

We tested the scenario and managed to reproduce the same problem on our end. For the sake of any correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-17756. We will further look into the details of this problem and will keep you updated on the status of this issue. We apologize for your inconvenience.

Hi Awais,

Any Update on this ? Please let me know

Thanks,
Prasanna

@pshanmuganathan,

Unfortunately, this issue is not resolved yet. This issue is currently pending for analysis and is in the queue. We will inform you via this thread as soon as this issue is resolved. We apologize for your inconvenience.

Hi Awais,

I just wanted to follow up and check if you have any update for this issue.

Thanks,
Prasanna Shanmuganathan

@pshanmuganathan,

There is no further update available at the moment. This issue is currently pending for analysis and is in the queue. We will inform you via this thread as soon as this issue is resolved. We apologize for your inconvenience.

@pshanmuganathan,

Regarding WORDSNET-17756, we suggest you the following code to workaround this problem:

public void Example()
{
    Document doc = new Document("D:\\Temp\\Aspose Support\\input.docx");
    DocumentBuilder builder = new DocumentBuilder(doc);

    doc.Protect(ProtectionType.ReadOnly);
    NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);

    foreach (Run run in runs)
    {
        if (run.Text.Equals("Check1"))
        {
            Console.WriteLine("Check1");
            builder.MoveTo(run);
            builder.StartEditableRange();
        }
        if (run.Text.Equals("Check2"))
        {
            Console.WriteLine("Check2");
            builder.MoveTo(run);
            MakeTextBoxesEditable(builder.EndEditableRange(), builder);
        }
    }

    doc.Save("D:\\Temp\\Aspose Support\\18.11.docx");
}

private void MakeTextBoxesEditable(EditableRangeEnd editableRangeEnd, DocumentBuilder builder)
{
    EditableRangeStart editableRangeStart = editableRangeEnd.EditableRangeStart;
    Node next = editableRangeStart.NextSibling;

    while (next != editableRangeEnd)
    {
        if (next.IsComposite)
        {
            CompositeNode composite = next as CompositeNode;
            Shape shape = composite as Shape;

            if (shape == null)
            {
                next = composite.FirstChild != null ? composite.FirstChild : composite.NextSibling;
                continue;
            }

            if (shape.HasTextbox)
                MakeTextBoxEditable(shape, builder);
        }

        next = next.NextSibling == null ? next.ParentNode.NextSibling : next.NextSibling;
    }
}

private void MakeTextBoxEditable(Shape shape, DocumentBuilder builder)
{
    Node firstNode = shape.FirstParagraph.FirstChild == null ? shape.FirstParagraph : shape.FirstParagraph.FirstChild;
    Node lastNode = shape.LastParagraph.LastChild == null ? shape.LastParagraph : shape.LastParagraph.LastChild;

    builder.MoveTo(firstNode);
    builder.StartEditableRange();
    builder.MoveTo(lastNode);
    builder.EndEditableRange();
}

We currently do not mark textboxes to copy MS Word’s behavior. Please let us know if this workaround is acceptable for you?

@pshanmuganathan,

Regarding WORDSNET-17756, we have completed the work on your issue and concluded to close this issue as ‘Not a Bug’. Please see my previous post for analysis details and workaround.