Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Wed May 31, 2017 8:01 pm
I found that the following code makes all sections inside the document to be protected
Dim doc As New Document()
'protect all sections
doc.Protect(ProtectionType.AllowOnlyFormFields, "123")
Then, I can set some sections individually to override that protection
Dim section As Section = doc.AddSection()
section.ProtectForm = False
However, I have some paragraphs for which I need to be able to set some text to be protected (or readonly) and some other text to be not-proptected .
Example:
Company Background (Protected)
Founded in 1998 by Joe Doe (un-protected)
I tried adding two paragraphs and two sections but each section starts in a new page, which doesn't fit my purposes.
Question:
Is there any way to set the protection to the paragraph and/or text level?
if not,
How can I keep all sections in the same page?
-
mtabares
-
- Posts: 11
- Joined: Wed May 31, 2017 7:54 pm
Thu Jun 01, 2017 7:42 am
Dear mtabares,
Thanks for your inquiry.
Sorry that Spire.Doc doesn't support making some text read only at present. And you can use following code to insert new section which is continuous to previous section instead of creating new page.
- Code: Select all
Section.AddParagraph().InsertSectionBreak(SectionBreakType.NoBreak);
Hope this helps. If there is any question, please let me know.
Sincerely,
Betsy
E-iceblue support team
-
Betsy.jiang
-
- Posts: 3099
- Joined: Tue Sep 06, 2016 8:30 am
Thu Jun 01, 2017 12:38 pm
Thank you for your reply. Unfortunately the solution doesn't seem to be working for me. After adding the SectionBreakType.NoBreak it still makes each section in one separate page.
here is my full code
- Code: Select all
Dim doc As New Document()
'protect all sections
doc.Protect(ProtectionType.AllowOnlyFormFields, "123")
'First Section - read only text
Dim ReadOnlySection As Section = doc.AddSection()
Dim Para As Spire.Doc.Documents.Paragraph = ReadOnlySection.AddParagraph()
Para.InsertSectionBreak(SectionBreakType.NoBreak)
Para.AppendText("This text CAN NOT be modified")
'Second Section - Editable text
Dim EditableSection As Section = doc.AddSection()
EditableSection.ProtectForm = False 'Remove protection
Dim Para2 As Spire.Doc.Documents.Paragraph = EditableSection.AddParagraph()
Para2.InsertSectionBreak(SectionBreakType.NoBreak)
Para2.AppendText("This text CAN be modified")
Dim stream As New MemoryStream
doc.SaveToStream(stream, FileFormat.Doc)
'dViewer.LoadFromStream(stream, Spire.Doc.FileFormat.Auto)
'System.Diagnostics.Process.Start("Sample.doc")
Dim toArray As Byte() = stream.ToArray
stream.Close()
doc.Close()
Response.ContentType = "application/msword"
Response.AddHeader("content-disposition", "attachment; filename=Doc.doc")
Response.BinaryWrite(toArray)
Response.Flush()
Response.End()
-
mtabares
-
- Posts: 11
- Joined: Wed May 31, 2017 7:54 pm
Thu Jun 01, 2017 2:33 pm
I found a workaround for this issue by using the textformfield for those paragraphs that I want to be able to edit.
Here is my entire code in case someone is looking for something like this
- Code: Select all
Dim doc As New Document()
'protect all sections
doc.Protect(ProtectionType.AllowOnlyFormFields, "123")
'First Section - read only text
Dim docSection As Section = doc.AddSection()
Dim Para As Spire.Doc.Documents.Paragraph = docSection.AddParagraph()
Para.AppendText("This text is a title and CAN NOT be modified")
'Editable text
Dim ParaField As Spire.Doc.Documents.Paragraph = docSection.AddParagraph()
ParaField.ApplyStyle(BuiltinStyle.BodyText)
Dim txtField As TextFormField = ParaField.AppendTextFormField("txtField1", "This text CAN be modified")
txtField.TextFieldType = TextFormFieldType.RegularText
Dim stream As New MemoryStream
doc.SaveToStream(stream, FileFormat.Doc)
'dViewer.LoadFromStream(stream, Spire.Doc.FileFormat.Auto)
'System.Diagnostics.Process.Start("Sample.doc")
Dim toArray As Byte() = stream.ToArray
stream.Close()
doc.Close()
Response.ContentType = "application/msword"
Response.AddHeader("content-disposition", "attachment; filename=Doc.doc")
Response.BinaryWrite(toArray)
Response.Flush()
Response.End()
-
mtabares
-
- Posts: 11
- Joined: Wed May 31, 2017 7:54 pm
Fri Jun 02, 2017 2:05 am
Hello,
Thanks for sharing your wonderful workaround!
If there's any doubt in the future, please feel free to post.
Sincerely,
Jane
E-iceblue support team
-
Jane.Bai
-
- Posts: 1156
- Joined: Tue Nov 29, 2016 1:47 am