Hi Team,
We are inserting an image into a document using ShapeType.TextBox.
Using ShapeType.Image without ImageData occupies space, and using ShapeType.TextBox allows text to be written over the image, but we want to prevent editing within that text box.
Is there any way we can restrict textbox from editing while allowing other parts of the document to remain editable?
Below is some sample code which we are using.
Shape imageTextBox = new(document, ShapeType.TextBox)
{
Height = ImageHeight(),
Width = ImageWidth(),
WrapType = WrapType.Inline
};
Paragraph imagePara = new(document);
imagePara.AppendChild(run);
// Insert paragraph into the image textbox.
imageTextBox.AppendChild(imagePara);
if (controlRenderOptions.Level == MarkupLevel.Inline)
{
return imageTextBox;
}
Paragraph outerImagePara = new(document);
outerImagePara.AppendChild(imageTextBox);
return outerImagePara;
The syntax of rendering image is:
<<image [image-expression(byte array)] -fitSize>>
Thank you,
Rajmal Dhakad
@RajmalConga
To insert an image into a document using ShapeType.TextBox
and restrict editing within that text box while allowing other parts of the document to remain editable, you can utilize the document protection features provided by Aspose.Words.
Here’s how you can achieve this:
-
Create the TextBox Shape: You can create a Shape
of type TextBox
and insert your image into it.
-
Restrict Editing: After creating the text box, you can apply document protection to restrict editing. You can specify that only certain parts of the document are editable, while the text box remains protected.
Here’s a sample code snippet demonstrating this:
// Create a new document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a TextBox shape
Shape imageTextBox = new Shape(doc, ShapeType.TextBox)
{
Height = ImageHeight(),
Width = ImageWidth(),
WrapType = WrapType.Inline
};
// Insert an image into the TextBox (assuming you have a method to get the image)
imageTextBox.ImageData.SetImage(imageBytes); // imageBytes is your image byte array
// Append the TextBox to the document
builder.InsertNode(imageTextBox);
// Protect the document to restrict editing
doc.Protect(ProtectionType.AllowOnlyFormFields, "yourPassword");
// Optionally, specify editable regions if needed
EditableRangeStart edRangeStart = builder.StartEditableRange();
EditableRange editableRange = edRangeStart.EditableRange;
EditableRangeEnd edRangeEnd = builder.EndEditableRange();
// Save the document
doc.Save("ProtectedDocument.docx");
Explanation:
- The
Shape
object is created as a TextBox
and an image is inserted into it.
- The
Protect
method is called on the document to restrict editing. You can choose the type of protection that suits your needs, such as allowing only form fields or making the document read-only.
- If you want to allow editing in specific regions, you can use the
StartEditableRange
and EndEditableRange
methods to define those areas.
This approach will ensure that the text box containing the image is not editable, while other parts of the document can remain editable as per your requirements.
For more detailed information on document protection and editing restrictions, you can refer to the official documentation on restricting document editing in Aspose.Words here.
Sources:
[1]: Restrict Document Editing in C#|Aspose.Words for .NET
Thanks for the quick response but it doesn’t helped in our scenario. We need to make textbox as read-only so it won’t allow to enter any text.
Do we have any such property or method by using that we can mark ShapeType.TextBox as readonly?
Thanks
@RajmalConga
Could you please elaborate this point? What works unexpectedly when ShapeType.Image
is used?
Hi @ivan.lyagin,
Using ShapeType.Image
without ImageData
occupies space - this means, if we have image data (byte array) available then it will display image perfectly fine but in case of no image data (byte array) present then it will show a blank box in the document of a specific size.
For same behavior, with ShapeType.TextBox
, if no image data (byte array) present then we will not have an blank space in the generated document and same behavior we are expecting.
Thanks,
Rajmal
@RajmalConga Why do not you simply remove the Image shape without image data? If you are generating the document through the code you can add a simple condition and simply skip inserting image without image data. Could you please elaborate your scenario and provide simple code example, your output and expected documents here for our reference?
Hi @alexey.noskov,
When there is no data then we can remove post document generation blank image space and that works with fine with ShapeType.Imgae but with ShapeType.Image, the width and height we are setting is not working as expected even after making aspect ratio locked to false.
We are providing different-different height and width but it always consider the first field “height” and width its considering same ah height and ignoring width which we have provided.
Thanks
@RajmalConga You should disable aspect ratio locked before setting the shape size. In your code you are disabling it after setting shape size.