I want to process word data in such a way that it should be extracted statement by statement and at the same time it should preserve formatting also.
Source Word Doc:
This is demo file…
1. First Line
2. Second LIne
End of Demo file
I want to extract each of the statement (not line because statement might contain more than one line) want to precess it and save it to some other file.
This is demo file…
1. Modified First Line
2. Modified Second LIne
End of Demo file
In this case numbered list is preserved and it should preserve all fonts,style and everything. If this is possible then can you please give me some direction to achieve it or kind of sample code for it.
Thanks for the reply. I have already gone through the document model generated by Aspose. The example i gave was just to explain you the concern. It’s not that i just want to work with lists. Let me explain it in brief.
Say i have a large word doc with say 100-200 pages long or may be more.
I want to create a new document which is the copy of the source doc but with few modification like i might change one of the bullet value or might change title of some paragraph or something like that.
So what exactly i need is to process source doc statement by statement and at the same time it should preserve all those format and if i change something in the fetched statement, it should affect only to the text and not to indentation and format and now modified statement i want to save it to the new doc.
doc 1 doc 2
my doc1 mydoc2
1. first 1. first
2. second 2. second
this is in italic this is also in italic
If possible can you please share some code that performs similar kind of functionality. like reading from a doc while preserving all format and saving to another doc with modification.
To add further, the link which you suggested shows how to create list. What i want is when i am reading a statement from source doc, is it possible to know whether that statement has bullet or any other property and if it is i should preserve the same just by changing text value.
There is something called FormattedText property in Interop API which allows to copy statement in the same format but when i change the text value it losses the format and i am not able to save it with bullet, number etc. I want to preserve that.
// Open the stream.
Read only access is enough for Aspose.Words to load a document.<o:p></o:p>
Stream stream = File.OpenRead(@"C:\test\demofile.docx");
// Load the entire document into memory.
Document docCopy = new Document(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
DocumentBuilder builder = new DocumentBuilder(docCopy);
NodeCollection paras = docCopy.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paras)
Thanks for the post again. That helped a lot but still i have few doubts. As mentioned in my previous post, a source document may contain anything supported by Word. What i am concerned is, i can easily check for each of the para whether it’s list or not using IsListItem which perfectly fine but how should i check for others like font type, style, everything need to be preserved. In that case i’ll have to put if-else for each of them.
As mentioned earlier, suppose i use para.range.FormattedText to copy to another doc. so it’ll preserve all format for that para now i just want to replace para text by something else but should preserve the format but when i do para.range.text=“modofied”, it’s not preserving format.
So instead of creating a clone using stream what i want is, open source doc, read statement (not line because statement can me made up of multiple lines) copy it to target doc using formattedText then get modified string and replace with the one just copied. So do i need to check for those list, title of para and all those properties? Is there any other way to achieve this? Is there a way that i can find out formatting for a sentence also?
Thanks for the reply. I was unable to attach file here so i’ve uploaded sample docs on a website. Can you please download it so it’ll give clear idea on what i want.
There are 2 doc files in zip file. One is source.doc and another is destination.doc. Now i want to know is it possible using Aspose word API to get the formatting, structure information for page,char,table,line,images etc so that i can modify content and produce the same structure and format in destination doc. If your API supports that feature then we are ready to purchase both word and pdf.
If you have got the idea of what i want then now can you please guide on how to retrieve pargraph wise or statement wise from source.doc and create destination.doc with modified text but preserving format. Please let me know if you want some other information.
Also wanted to know what languages it supports? I mean Unicode,English , Asian languages etc…
Aspose.Words e.g. C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Demos\CSharp\DocumentExplorer\bin\DocumentExplorer.exe.
Document doc = new
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
Table table = (Table)tables;
Cell cell = table.Rows.Cells;
Run run = new Run(doc);
run.Text = "Some modification I want to do";
Aspose.Words.Font font = run.Font;
font.Size = 12;
font.Bold = false;
font.Color = System.Drawing.Color.Red;
font.Name = "Verdana";
Paragraph para = new Paragraph(doc);
ParagraphFormat paragraphFormat = para.ParagraphFormat;
paragraphFormat.FirstLineIndent = 8;
paragraphFormat.Alignment = ParagraphAlignment.Justify;
paragraphFormat.KeepTogether = true;
Also, Aspose.Words does support all languages.
Thanks again for the reply. That was really helpful for me. I went through Document Explorer and it’s nice. Got cleared few of my doubts. After going through all of these i came up with my own idea of how i will go further please let me know if i am missing something.
1. Open source doc.
2. Read paragraphs one by one (But how will i get format information about each paragraph, the one you have described in your previous post shows how to create new one. I want to know font style, size, whether it’s a bullet or shape(image) because both bullet and shape comes under paragraph as what i came to know from DocumentExplorer.exe. Also how will i know their layout info and all… It would be really helpful if you can give some kind of sample example like you gave for creating new one in your earlier post.)
3. Modify paragraph if needed based on requirement.
4. Write to destination.doc. Apply all those formats we read in step 2 so that destination.doc will have same layout.
One more thing, as what you said that Aspose supports all the languages but when i tried to create new doc with some other language it was showing boxes and not in that particular language.
I tried to create the same using interop where i was able to see in the language i created so i guess there is no issue of font, that font is already installed in the system.
Again thanks a lot for your support so far.
Also please let me know how to add shape(normal shape like rectangle,flow chart etc and image) to paragraph. I just want to copy/paste those things from source doc to destination but couldn’t find out how to do it like how to detect that it’s a shape from source and how to add it to destination doc.
From DocumentExplorer, i could find that it stores image and other shapes as Shape type so i was able to detect but how to retrieve it from source doc and put into destination doc because i dont think we can create Shape object in Aspose.
Thanks for the reply. I understood that paragraph can contain text in the form of Run.Text. My doubts are
1. Pargraph-> Run-> Text
Paragraph-> Shape-> Paragraph-> Run -> Text
These are the three ways i found for paragraph for my source doc. Now what i am concerned is if i encountered a shape and without modifying anything if i want to copy/paste to target document is it possible? Like if there is some drawing object in source doc without modifying just cop/paste or if there is some image copy/paste to target doc.
2. Copying paragraph is possible or not?
Paragraph p=new Paragraph(targetDoc);
I tried this but it’s giving exception saying node is created from some different doc.
If this is not possible is there any way just to copy/paste paragraph because in some cases i don’t want to modify paragraphs from source doc so will just read from source doc and will paste to target doc.
It would help me if you can give some kind of code for it just to get an idea.
You can refer source.doc and destination.doc file i sent you earlier. to get an idea of my 2 issues mentioned above.
Regarding Language translation: Just to check whether it works or not i created sample code as below
String fileName = “C:\testing.doc”;
Document source = new Document(fileName);
Document tt = new Document();
//Create section for target doc
Section section = new Section(tt);
//Create body for target doc
Body body = new Body(tt);
Paragraph p1=new Paragraph(tt);
Run r =new Run(tt);
r.Text = “some text in other language”
<!–[if gte mso 9]>
<m:mathFont m:val=“Cambria Math”/>
<![endif]–><!–[if gte mso 10]>
The Service Location Protocol (SLP) is a service discovery protocol that allows computers and other devices to find services in a LAN without prior configuration. Since the current implementation of SLP is limited to LAN and is not scable to internet, we offer an implementation that provides similar functionality as that of SLP in a cloud that helps client to select the most appropriate service.
Destination File Content:
<!–[if gte mso 9]>
<m:mathFont m:val=“Cambria Math”/>
<![endif]–><!–[if gte mso 10]>
Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd.
सेवा स्थान प्रोटोकॉल (SLP) एक सेवा डिस्कवरी प्रोटोकॉल है कि
अनुमति देता है (In place of this i am getting boxes which should not be as when i am using interop API, it's able to show me the same text in the language i have translated like shown here)
So it’s able to convert but not able to show…
Also one more thing, even though i am using entire paragraph range, it’s taking only first line of it.
this one should return entire paragraph but its giving only first line : <span style=“font-family:“LMRoman12-Regular”,“serif”;
mso-bidi-font-family:LMRoman12-Regular”>The Service Location Protocol (SLP<span style=“font-family:“LMRoman12-Bold”,“serif”;mso-bidi-font-family:LMRoman12-Bold;
mso-bidi-font-weight:bold”>) <span style=“font-family:“LMRoman12-Regular”,“serif”;mso-bidi-font-family:LMRoman12-Regular”>is a service discovery protocol that
Thanks a lot for the reply. NodeImported helped a lot. Can you please elaborate more on paragraph break.
I have written some code and trying to copy/paste only those paragraph having shape but not getting correct output. I guess it’s overlapping with previous one and that is because of paragraph break i guess. Can you please look into it once.
for (int i = 0; i < source.Sections.Count; i++)
for (int l = 0; l < source.Sections[i].Body.ChildNodes.Count; l++)
Node n = source.Sections[i].Body.ChildNodes[l];
Paragraph p = (Paragraph)n;
for (int m = 0; m < p.ChildNodes.Count; m++)
Paragraph para = new Paragraph(tt);
NodeImporter ni = new NodeImporter(source, tt, ImportFormatMode.KeepSourceFormatting);
Node ins=ni.ImportNode(p.ChildNodes[m], true);
Console.WriteLine(“Child:” + p.ChildNodes[m].NodeType);
else if (n.NodeType.ToString().Equals(“Table”))
Console.WriteLine(l + “:Table”);
Thanks for the help.
Document source = new
Document destination = new Document();
NodeImporter ni = new NodeImporter(source, destination, ImportFormatMode.KeepSourceFormatting);
Body destBody = destination.FirstSection.Body;
NodeCollection paragraphs = source.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph paragraph in paragraphs)
if (paragraph.GetChildNodes(NodeType.Shape, true).Count > 0)
Node node = ni.ImportNode(paragraph, true);
Moreover,to get a better understanding of Paragraphs, I would suggest you to visit the following API link:
I hope, this will help.
Thanks a lot for your reply. That helped me a lot. I will go through the link you have provided and straight away start working. If you don’t mind i’ll keep on posting queries if i come across while going further.
Thanks for the help. I got stuck with another issue. As i mentioned earlier, i am interested in working only with text from the source doc and don’t want to deal with formatting. My source doc is not fixed. It can change so i have to make my code generalize to fit to all kinds of docs.
Each time when i fetch text from source doc, i don’t want it formatting to be disturbed. Like suppose i have a table with 4 rows and 2 columns having background color, font style and all. Also i have some paragraph with different color, then i just want to work with the text by preserving format,background color information and all.
One way is to read formatting info for all node say if text is bold,italic,has underline or cell has background color etc .and then while writing to text doc apply the same format in destination doc. but i need to check for all kinds of possible formatting as i don’t know what my source doc can contain.
Is there any other way to achieve this. May be something like
Something similar to NodeImporter but with option to change the text for that node
Node may be shape which may contain text, table with text or something else.
One another way i am thinking of is: I do copy all nodes one by one using NodeImporter to destination doc and just replace the run.text for each of the node once it’s appended to body of destination.doc.
which takes care of all those format,font size,style etc and allow me to work only on text so i don’t need to check for all those formatting issue and all…