I'm new to Aspose.Words for .Net, and working on recreating some documents for a customer. I need to make a single word in a paragraph bold and underlined. I'm trying to achieve this by creating separate paragraph runs for the text before the bold word, the bold word itself, and the text after. Then I'm formatting the bold word's run and appending everything to the paragraph. This seems overly complicated. Is there a simple way to achieve this from within DocumentBuilder.WriteLn("some text")?
I kept working at it, and achieved the desired result by using DocumentBuilder.Write() instead of DocumentBuilder.Writeln():
builder.Write("The start of the paragraph ");
builder.Font.Bold = true;
builder.Font.Underline = Underline.Single;
builder.Write("underlined bolded text");
builder.Font.Bold = false;
builder.Font.Underline = Underline.None;
builder.Write(" the end of the paragraph.");
Related
I am working on a project where there is a Legal Word document in below Format and I want to extract the Paragraph titles along with the Paragraph and check if that paragraph has any track changes like Insert or Delete.
Heading
Paragraph 1 Text
Heading
Paragraph 2 Text
etc.,
I am able to extract the paragraphs individually and get the track changes from word/document.xml however the heading is also being coming as a paragraph when read with python-docx.
the Heading however is in bold text.
Is there a way to check when the paragraph 1 has any track changes, extract heading1 and assign it to a dictionary along with the paragraph text and track changes?
I tried to see if there is any particular style for headings but apparently there is nothing and the below output gave wrong results.
from docx import Document
#from docx.shared import Inches
document = Document("DOC_Example.docx")
headings = []
texts = []
for paragraph in document.paragraphs:
if paragraph.style.name == "Heading 2":
headings.append(paragraph.text)
elif paragraph.style.name == "Normal":
texts.append(paragraph.text)
for h, t in zip(headings, texts):
print(h, t)
I need to correlate and capture the Heading and Paragraph with the track changes in the same Dictionary.
I am a beginner in python and looking for some inputs.
Thanks in Advance.
Best Regards
I'm trying to add text runs to an existing paragraph in Word using the OpenXml SDK, but every time I do this it ends up "trimming" the text which I add.
For example
Run newRun = newRun();
newRun.AppendChild(new Text("Hello "));
paragraph.AppendChild(newRun);
Run newRun = newRun();
newRun.AppendChild(new Text(" World"));
paragraph.AppendChild(newRun);
// the resulting paragraph contains "HelloWorld" as the text
I've also checked the XML of the resulting Run which gets generated, which clearly includes the "space" character:
<w:t xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">Hello </w:t>
I've tried injecting '\u0020' unicode values, as well as an "empty" run which just contains a single space, but nothing seems to work.
Does anyone know the trick to this?
Rolling my eyes at how typical that I find the answer 5 minutes after posting (having been trying for hours)
The answer is to add the XML xml:space="preserve" attribute to the Text element, otherwise spaces are trimmed.
newRun.InnerXml = $"<w:t xml:space=\"preserve\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">{text}</w:t>";
I have a text field which I need to style for example with bold or italics parts.
I tried overridding the TextEditingController's buildTextSpan and formatting the text using annotation ranges with custom styles but the edge cases were too much and I really couldn't get it to work.
So, thought about using a formatter where before every change in format I'll add a custom character, like this:
This text is |bBOLD and this is |iITALICS. Would get me this:
This text is BOLD and this is ITALICS. So I override the buildTextSpan to build the TextSpan from a parse function where I split the text by the special characters and check the initial letter of each text for formatting info.
This works well except for the fact that when I press the right arrow to go the next character after the "This text is ", the cursor will stay fixed as it thinks there are two characters but being only for formatting, they aren't there on the render.
Is there any way I could tell the textfield to ignore certain characters when selecting, moving selection or typing?
I think this would work!
static const kCharToBEIgnored = 0x2C;
// Here 0x2C means ',' comma
// For complete list visit https://api.flutter.dev/flutter/charcode/charcode-library.html
String get text {
return String.fromCharCodes(
_value.text.codeUnits.where((ch) => ch != kCharToBEIgnored),
);
}
I have a Word VSTO addin which copies the Notes page for each slide in a PowerPoint file into a Word document. There are only certain lines in the Notes pane that I need. My code loops thru each slide, looks for any notes, then checks for the tags and copies the text between the start and end tags. It is important to maintain the formatting in the Notes page. However, when pasting into Word, the font copies over, but the size changes. For instance, the font in PowerPoint might be Times New Roman 12, but in Word it will sometimes randomly change to Times New Roman 14. It appears that even if pasting a single range of text, the font may change between paragraphs. Also, certain blocks of text in Word will have the Normal style, but others will have Normal (Web), which affects line spacing.
I tried using a method to retain original source formatting (commented out below), but that sometimes will create bullets before the text, besides changing the font size.
Anyone have an idea how to resolve this?
The abbreviated example code is below.
Dim notesRng, fndRng, endRng, copyRng as PowerPoint.TextRange
dim iStart, charLen as Integer
For i As Integer = 0 To pwrPointApp.ActivePresentation.Slides.Count - 1
oSlide = pwrPointApp.ActivePresentation.Slides(i)
If oSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Text.Length > 0 Then
notesRng = oSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange
fndRange = notesRng.Find(FindWhat:="START:")
If Not fndRange is Nothing then
iStart = notesRng.Text.IndexOf("START:") + 6 ' puts the start just after the colon:
endRng = notesRng.Find(FindWhat:="END:", After:=iStart)
If Not endRng is Nothing Then
charLen = notesRng.Text.IndexOf("END:", iStart) - iStart
copyRng = oSlide.NotesPage.Shapes.Placeholder(2).TextFrame.TextRange.Characters(iStart, charLen)
If Not copyRng is Nothing then
copyRng.Copy
ThisAddIn.WordApp.Selection.Paste()
'ThisAddIn.WordApp.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting)
ThisAddin.WordApp.Selection.Collapse()
End If
End If
End If
End If
Next I
I'm almost left with no time but facing a problem with DataDynamics.ActiveReports.
I have to replace some text for 500 reports so automating the task through code at run time.
The major problem I'm facing is on replacing text the original bold wont changes to normal font. center justified text will be left justified also Arial Narrow text changes to Arial.
Is there any way to replace text without disturbing the original format.
Here is the piece of code:
var textBox = (DataDynamics.ActiveReports.RichTextBox)reportSection.Controls[controlIdx];
if (textBox.Text.Contains("Babu"))
{
MessageBox.Show(textBox.Text);
var modifiedtext = (DataDynamics.ActiveReports.RichTextBox)reportSection.Controls[controlIdx];
modifiedtext.Text = modifiedtext.Text.Replace("Babu", "Mannu");
MessageBox.Show(modifiedtext.Text);
}
The modified report has a format different than the original. How to fix this issue??
its richtext, not plain text.
every rich text has a formatting associated with it.
try editing the original rtf that you are loading into the rtb control. This is what I would recommend.
Or, another approach could be to use richtextbox.rtf.replac instead of richtextbox.text.
At what time of the report processing are you doing this?