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.
Mon Oct 26, 2015 8:00 am
Hi,
I've seen the new article :
http://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/Text/New-method-get-alias-tag-and-id-of-content-controls-in-a-Word-document-in-C.html.
I've modified the console output function to this :
- Code: Select all
StructureDocumentTagInline sdtil = structDoc as StructureDocumentTagInline;
Console.WriteLine("{0,20},{1,12},{2, 10}, {3},'{4}'", alias, id, STDType, tag, sdtil?.SDTContent.Text);
sdtil?.SDTContent.Text is read only. Why ? How modify it ?
Thanks,
Vincent
-

nolme
-
- Posts: 79
- Joined: Fri Apr 24, 2015 3:56 pm
Mon Oct 26, 2015 8:25 am
Hi Vincent,
Thanks for your inquiry.
Please use this code sdtil.OwnerParagraph.Text = "12345678";.
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3044
- Joined: Wed Jun 27, 2012 8:50 am
Mon Oct 26, 2015 10:38 am
Don't work in all cases. 'sdtil.OwnerParagraph' is null sometimes but not sdtil.SDTContent.Text
(and it destroy the ContentControl)
-

nolme
-
- Posts: 79
- Joined: Fri Apr 24, 2015 3:56 pm
Tue Oct 27, 2015 1:33 am
Hi,
Sorry that it didn't work. To help us solve your issue, please provide us your word document and the code you used.
Thank you.
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3044
- Joined: Wed Jun 27, 2012 8:50 am
Tue Oct 27, 2015 7:11 am
Oups.
Here it is.
Login to view the files attached to this post.
-

nolme
-
- Posts: 79
- Joined: Fri Apr 24, 2015 3:56 pm
Tue Oct 27, 2015 8:59 am
Hi,
Thanks for your providing. I will do an investigation and provide you solution to solve it.
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3044
- Joined: Wed Jun 27, 2012 8:50 am
Wed Oct 28, 2015 2:52 am
Hi,
I did some modification for your code, please check the attached code. Hope it can help you.
Please write to us if you have any problems.
Best Regards,
Amy
E-iceblue support team
Login to view the files attached to this post.
-


amy.zhao
-
- Posts: 3044
- Joined: Wed Jun 27, 2012 8:50 am
Wed Oct 28, 2015 7:33 am
Hi,
it's better but still have 2 problems :
- in the header, text format is not kept
- if we use \n or Environment.NewLine it will display an error message in Word.
-

nolme
-
- Posts: 79
- Joined: Fri Apr 24, 2015 3:56 pm
Wed Oct 28, 2015 8:09 am
Hi,
Please refer to the following code snippet to set the font same as original font:
- Code: Select all
if (tagInlines[i] is StructureDocumentTagInline)
{
StructureDocumentTagInline sdtil = tagInlines[i] as StructureDocumentTagInline;
Console.WriteLine("{0,20},{1,12},{2, 10}, {3},'{4}'", alias, id, STDType, tag, sdtil.SDTContent.Text);
if (dico.TryGetValue(tag, out valueToSet) && (!String.IsNullOrEmpty(valueToSet)))
{
if (sdtil.SDTProperties.SDTType == SdtType.Picture)
{
DocPicture picture = sdtil.ChildObjects.FirstItem as DocPicture;
if (picture == null)
{
picture = new DocPicture(doc);
picture.LoadImage(Image.FromFile(@".\code_EAN128.png"));
tagInlines[i].ChildObjects.Add(picture);
}
else
{
picture.LoadImage(Image.FromFile(@".\code_EAN128.png"));
}
}
else if (sdtil.SDTProperties.SDTType == SdtType.Text)
{
TextRange range = new TextRange(doc);
range.CharacterFormat.Font = (sdtil.ChildObjects[0] as TextRange).CharacterFormat.Font;
range.Text = valueToSet;
sdtil.ChildObjects.Add(range);
while (sdtil.ChildObjects.Count > 1)
{
sdtil.ChildObjects.RemoveAt(0);
}
}
}
}
else if (tagInlines[i] is StructureDocumentTag)
{
StructureDocumentTag sdt = tagInlines[i] as StructureDocumentTag;
StringBuilder strb = new StringBuilder();
foreach (Paragraph p in sdt.SDTContent.Paragraphs)
{
strb.Append(p.Text);
}
Console.WriteLine("{0,20},{1,12},{2, 10}, {3},'{4}'", alias, id, STDType, tag, strb.ToString());
Paragraph paragraph = new Paragraph(doc);
TextRange range = new TextRange(doc);
paragraph.ChildObjects.Add(range);
range.Text ="12345678";
if (sdt.ChildObjects.Count > 0)
{
//Get the font of the original content
Font font =((sdt.ChildObjects[0] as Paragraph).ChildObjects[0] as TextRange).CharacterFormat.Font;
//Set font of textRange
range.CharacterFormat.Font = font;
}
sdt.ChildObjects.Add(paragraph);
while (sdt.ChildObjects.Count > 1)
{
sdt.ChildObjects.RemoveAt(0);
}
}
Please provide us your code with \n or Environment.NewLine to help us reproduce your issue.
Thanks.
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3044
- Joined: Wed Jun 27, 2012 8:50 am
Wed Oct 28, 2015 9:12 am
- For new line : simply replace
- Code: Select all
range.Text ="12345678";
by
- Code: Select all
range.Text = "123456789\nABCDEF";
- Adding the snippet partially work but ContentControl is bigger than source.
Login to view the files attached to this post.
-

nolme
-
- Posts: 79
- Joined: Fri Apr 24, 2015 3:56 pm
Thu Oct 29, 2015 7:26 am
Hi,
1)Please set
sdt.SDTProperties.SDTType = SdtType.None for the text content control that you will enter multi-lines text into.
2)Please apply original format to new paragrah and new textRange.
Sample code:
- Code: Select all
if (tagInlines[i] is StructureDocumentTagInline)
{
StructureDocumentTagInline sdtil = tagInlines[i] as StructureDocumentTagInline;
Console.WriteLine("{0,20},{1,12},{2, 10}, {3},'{4}'", alias, id, STDType, tag, sdtil.SDTContent.Text);
if (dico.TryGetValue(tag, out valueToSet) && (!String.IsNullOrEmpty(valueToSet)))
{
if (sdtil.SDTProperties.SDTType == SdtType.Picture)
{
DocPicture picture = sdtil.ChildObjects.FirstItem as DocPicture;
if (picture == null)
{
picture = new DocPicture(doc);
picture.LoadImage(Image.FromFile(@".\code_EAN128.png"));
tagInlines[i].ChildObjects.Add(picture);
}
else
{
picture.LoadImage(Image.FromFile(@".\code_EAN128.png"));
}
}
else if (sdtil.SDTProperties.SDTType == SdtType.Text)
{
TextRange range = new TextRange(doc);
CharacterFormat origanlFormat = (sdtil.ChildObjects[0] as TextRange).CharacterFormat;
range.ApplyCharacterFormat(origanlFormat);
range.Text = valueToSet;
sdtil.ChildObjects.Add(range);
while (sdtil.ChildObjects.Count > 1)
{
sdtil.ChildObjects.RemoveAt(0);
}
}
}
}
else if (tagInlines[i] is StructureDocumentTag)
{
StructureDocumentTag sdt = tagInlines[i] as StructureDocumentTag;
sdt.SDTProperties.SDTType = Spire.Doc.Documents.SdtType.None;
StringBuilder strb = new StringBuilder();
foreach (Paragraph p in sdt.SDTContent.Paragraphs)
{
strb.Append(p.Text);
}
Console.WriteLine("{0,20},{1,12},{2, 10}, {3},'{4}'", alias, id, STDType, tag, strb.ToString());
Paragraph paragraph=new Paragraph(doc);
if (sdt.ChildObjects.Count > 0)
{
paragraph = sdt.ChildObjects[0].Clone() as Paragraph;
paragraph.Text = "1234567\nABCDE";
}
sdt.ChildObjects.Add(paragraph);
while (sdt.ChildObjects.Count > 1)
{
sdt.ChildObjects.RemoveAt(0);
}
}
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3044
- Joined: Wed Jun 27, 2012 8:50 am
Thu Oct 29, 2015 7:56 am
GREAAAAAAATTTT IT WORKS (and carriage return too)
You will make my day happy

-

nolme
-
- Posts: 79
- Joined: Fri Apr 24, 2015 3:56 pm
Thu Oct 29, 2015 8:50 am
Hi,
I am glad that your issues are resolved.
Welcome to write to us again for further problems. We are here to help you.
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3044
- Joined: Wed Jun 27, 2012 8:50 am