Dear Sir
I have different section in my document and different object in each section for instance in a section I inserted a FormField or Table in other section.I want to change font of each section seperately according to selection of user via my application.I did it but it didn’t work .There is my code for changing the font of my section with a FormField .
Please tell me where is the problem.
Thanks
Mehdi Mokhtari
Dim builder As New Aspose.Words.DocumentBuilder(m_document)
Dim objsection As Aspose.Words.Section
builder.MoveTo(objsection.Range.FormFields("From"))
builder.Font.Name = PrpFontName
builder.Font.NameBi = PrpFontName
builder.Font.Size = PrpFontSize
builder.Font.SizeBi = PrpFontSize
If PrpFontStyle = "Bold" Then
builder.Font.Bold = True
builder.Font.Italic = False
ElseIf PrpFontStyle = "Italic" Then
builder.Font.Italic = True
builder.Font.Bold = False
End If
m_document.Save("out.doc")
Hi
Thanks for your inquiry. It is not enough to change font of whole section. If you need to change font of whole section then you should change font of each node in this section. For example see the following code:
Dim doc As Document = New Document("in.doc")
'Get collection of Runs
Dim nodes As NodeCollection = doc.Sections(1).GetChildNodes(NodeType.Run, True)
'Change font of aech run in the section
For Each currentRun As Run In nodes
currentRun.Font.Size = 14
currentRun.Font.Bold = True
Next
'Change font of each formfield in the section
For Each field As FormField In doc.Sections(1).Range.FormFields
field.Font.Size = 14
field.Font.Bold = True
Next
doc.Save("out.doc")
Hope this helps.
Best regards.
I have used this code to change the font of the document as well, however I have noticed that spurious font information is left behind immediately following any fields in the source document. Here is my sample code.
using System;
using System.Collections.Generic;
using System.Text;
using Aspose.Words;
using Aspose.Words.Fields;
namespace ConsoleApplication1
{
public class AsposeHandler
{
public void DoStuff()
{
Document sourceDoc = new Document("c:\TestFolder\TestDoc.doc");
NodeCollection runs = sourceDoc.GetChildNodes(NodeType.Run, true);
foreach (Run run in runs)
{
run.Font.Name = "Arial";
run.Font.Size = 12;
}
foreach (Section section in sourceDoc.Sections)
{
foreach (FormField field in section.Range.FormFields)
{
field.Font.Name = "Arial";
field.Font.Size = 12;
}
}
sourceDoc.Save("c:\TestFolder\TestOutput.doc");
}
}
class Program
{
static void Main(string[] args)
{
AsposeHandler handler = new AsposeHandler();
handler.DoStuff();
}
}
}
The original document (TestDoc.doc) is in the Lucida font, I run this code over it to produce the output document (TestOutput.doc), changing all font information in the process, however if i use the cursor keys to move through the document I note that after every field there is still Lucida font information left behind. This would not normally be much of an issue but we then do something extra to those fields which then changes the font of the field back to Lucida. I have attached both the source document and the output document for you to have a look at.
(We use version 6.0.0.1 of Aspose.Words)
Hi
Thanks for your request. It is better to use DocumentVisitor to achieve this. Here is code:
// Open document
Document sourceDoc = new Document(@"Test123\in.doc");
// Create an object that inherits from the DocumentVisitor class.
FontChanger changer = new FontChanger("Arial");
// Get the model to accept a visitor.
// The model will iterate through itself by calling the corresponding methods
// on the visitor object (this is called visiting).
sourceDoc.Accept(changer);
// Save output document
sourceDoc.Save(@"Test123\out.doc");
FontChanger class is attached.
Best regards.
Thanks for that, works a treat!
Okay, but is it also possible to find and replace (for example) all text with font “Arial size 10” by “Times New Roman size 12”?
No matter if the text is in a textbox, header or footer?
Do you have an example of this?
regards Marco
Hi
Thanks for your request. Of course, you can achieve this. Here you can find another version of FontChanger class, which allows also changing font size:
https://forum.aspose.com/t/fw-changing-the-font-while-saving-the-html-file/75158
In FontChanger, you can also check the font of the original node.
Hope this helps. Please let me know if you need more assistance, I will be glad to help you.
Best regards.