How to set it, it can realize automatic adjustment of the spacing between Chinese and Western text, and automatically adjust the spacing between Chinese and Digit.
That is, the part where the red line is drawn in the figure(The redline section in the attachment file), how to set it through code?
@lovecomputer You can use ParagraphFormat
properties to specify these options: AddSpaceBetweenFarEastAndAlpha, AddSpaceBetweenFarEastAndDigit and FarEastLineBreakControl
I want to set line spacing, I want line spacing in line units instead of points, how can I modify the units of line spacing?
@lovecomputer You can specify LineSpacingRule to LineSpacingRule.Multiple
, then you use LineSpacing value, keeping in mind that 12 points corresponds one line. For example the following code specifies line spacing to 1.5 lines:
paragraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
paragraphFormat.LineSpacing = 1.5 * 12;
But the font size is different, for example, I have a first-level heading is No. 2 font, second-level headings are No. 3 fonts, how to achieve that the paragraph spacing of both first-level headings and second-level headings is 0.5 lines?With this method, how can you achieve line spacing of different sizes of fonts corresponding to 12 points? Because the font size is different
@lovecomputer When LineSpacingRule.Multiple
is used 12 points equals 1 line regardless font size set in the paragraph. So to set 0.5 line spacing you should use the following code:
paragraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
paragraphFormat.LineSpacing = 0.5 * 12;
I want to insert the PNG figure in attachment 1(pageheader.png) into the left side of the header of the word file, add some text to the far right of the header, and add a lower border line to the header text, the effect is as shown in the file of attachment 2(targetfile.docx), how to achieve it?
@lovecomputer You can achieve this using code like the following:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Move build into the document header.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
// Insert image
builder.InsertImage(@"C:\Temp\pageheader.png", 120, 30);
// Add right tabstop.
builder.ParagraphFormat.TabStops.Clear();
double tabPosition = builder.PageSetup.PageWidth - builder.PageSetup.LeftMargin - builder.PageSetup.RightMargin;
builder.ParagraphFormat.TabStops.Add(tabPosition, TabAlignment.Right, TabLeader.None);
// Insert text at right.
builder.Write(ControlChar.Tab); // Tab to shift text.
builder.Write("test12345");
// Add paragraph bottom border.
builder.ParagraphFormat.Borders.Bottom.LineStyle = LineStyle.Single;
doc.Save(@"C:\Temp\out.docx");
out.docx (13.1 KB)
I used the above example code, the paragraph line spacing in the modified document is to achieve 0.5 times the line spacing, but the spacing before and after the paragraph is not in line spacing, but in points, I hope that the unit of spacing before and after the paragraph is also line spacing, is there a way to implement it?<
@lovecomputer You can use LineUnitAfter and LineUnitBefore to specify before and after spacing in lines units.
In the word file, there are code snippets, these codes are included by 2 curly braces, how to achieve indent the code snippet in the middle of the curly braces by 4 characters?
An example of a code snippet is as follows:
void test(void)
{
int i=0;
i++;
}
Another question:
How to implement the insertion According to the 3-level headings of the main text, the requirements are: the font No. 4 of the first-level heading is bold, the font of the second-level heading No. 4 is not bolded and indented by 2 characters, and the third-level heading is not bold and indented by 4 characters; Line spacing is single line spacing
-
Yo indent paragraph you can use either
ParagraphFormat.LeftIndent
orParagraphFormat.CharacterUnitLeftIndent
. Alternatively, you can simply prepend aRun
with 4 spaces at the beginning of the paragraph. -
You can change properties of the appropriate styles in your document. Please see our documentation to lean how to work with styles:
https://docs.aspose.com/words/net/working-with-styles-and-themes/
When customizing the table of contents style, the primary contents and secondary contents styles have been added through the following code,
Aspose.Words.Style toc1Style = doc.Styles.Add(StyleType.Paragraph, "myTOC1");
toc1Style.Font.Size = 5;
toc1Style.Font.Bold = true;
Aspose.Words.Style toc2Style = doc.Styles.Add(StyleType.Paragraph, "myTOC2");
toc2Style.Font.Size = 5;
after defining the primary contents and secondary contenst styles, how to apply these styles to the existing body and automatically generate the table of contents?
@lovecomputer If you need to build TOC based on your custom styles, you can use \t
switch in TOC field. For example see the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Define styles
Aspose.Words.Style toc1Style = doc.Styles.Add(StyleType.Paragraph, "myTOC1");
toc1Style.Font.Size = 5;
toc1Style.Font.Bold = true;
Aspose.Words.Style toc2Style = doc.Styles.Add(StyleType.Paragraph, "myTOC2");
toc2Style.Font.Size = 5;
// Insert TOC based on the custom styles
builder.InsertTableOfContents("\\t \"myTOC1,1,myTOC2,2\"");
// Insert paragrpahs with newly created styles.
builder.Writeln();
builder.InsertBreak(BreakType.PageBreak);
builder.ParagraphFormat.Style = toc1Style;
builder.Writeln("Some First Level Paragraph");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("Some resular text.");
builder.ParagraphFormat.Style = toc2Style;
builder.Writeln("Some second Level Paragraph");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("Some resular text.");
// Update fields.
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");
Please see TOC field switches in MS documentaion:
https://support.microsoft.com/en-gb/office/field-codes-toc-table-of-contents-field-1f538bc4-60e6-4854-9f64-67754d78d05c