We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy paragraph with specific style into another document

Hi,


I am using aspose.words for coping paragraph from first page of document to other.

i am new to aspose, can it be done using nodeimporter? if yes how?

please give example how this can be done.

first page also contains tables and images.

code:
foreach (Paragraph p in src.FirstSection.Body.Paragraphs)
{
if (p.ParagraphFormat.StyleName == “Title”)
{
}
foreach (Run r in p.Runs)
{
if (r.Text.Contains(ControlChar.PageBreak))
{
//count++;
break;
}
}
NodeImporter n =new NodeImporter(p.Document, docBuilderdes.CurrentParagraph.Document, ImportFormatMode.KeepSourceFormatting);
n.ImportNode(docBuilderdes.CurrentParagraph.Document, true);
//docBuilderdes.CurrentParagraph.AppendChild§;
}

Thanks
Hi Amey,

Thanks for your inquiry. DocumentBase.ImportNode method imports a node from another document to the current document with an option to control formatting. Following code example copies paragraphs from one document into another.

Could you please share your input and expected output word documents here for our reference? We will then provide you more information about your query along with code.

Document dstDoc = new Document(MyDir + "dstDoc.docx");

Document srcDoc = new Document(MyDir + "srcDoc.docx");

NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);

foreach (Paragraph node in srcDoc.GetChildNodes(NodeType.Paragraph, true))

{

Node importNode = importer.ImportNode(node, true);

dstDoc.FirstSection.Body.AppendChild(importNode);

}

dstDoc.Save(MyDir + "Out.docx");

Hi Tahir,


Thanks for the reply. I want to identify the text from the document as tittle for the document by using style.name as “title”. and replace the content of that paragraph with new text is is possible to replace the content of the text while using importNode method?

also suggest if there is any other method which i can use to identify the text as the title of the document.

Hi Amey,

Thanks for your inquiry. Unfortunately, your question isn't clear enough therefore we request you to please elaborate your inquiry further along with input and expected output word documents. We will then provide you more information about your query along with code.

If you want to import nodes from one document into another, you need to use NodeImporter.ImportNode method. If you just want to replace the paragraph text, please remove all Run nodes from paragraph and insert new Run node with some text in same paragraph. Note that all text of the document is stored in runs of text.

Following code example shows how to get the paragraphs having Title style.

Document doc = new Document(MyDir + "in.docx");

var paragraphs = doc.GetChildNodes(NodeType.Paragraph, true)

.Cast<Paragraph>().ToArray().Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Title);

foreach (Paragraph para in paragraphs)

{

//Your code...

}

Hi Tahir ,

I have a question here.

My Requirement.

Copy Data from one doc to another document which is a layout with 6 columns.So when you fill data paragraph after paragraph first first column will be filled and then 2nd column and so on.
THis approach is copying text from one doc to another as expected in columns.
But there are 2 problems

1 . When copying from one doc to another Table structure isn’t copying. Just the data inside of it is copying.
2. Images Width are big in source. When copying to multi column Layout due to bigger size not entire image is fitting in column

To conclude.

  1. While lopping I need maintain table structure and if table width is bigger than column width I should be able to auto fit Table

  2. If Images Height and Width are bigger than Layout column width then it should automatically fit.

Can you please let me know if its possible ?

@TejKamal_Thotakuri

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

HI Tahir,

Please find attached for documents asked.Aspose.zip (3.9 MB)

I have used the same code mentioned above

" Document dstDoc = new Document(MyDir + “dstDoc.docx”);

Document srcDoc = new Document(MyDir + “srcDoc.docx”);

NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);

foreach (Paragraph node in srcDoc.GetChildNodes(NodeType.Paragraph, true))

{

Node importNode = importer.ImportNode(node, true);

dstDoc.FirstSection.Body.AppendChild(importNode);

}

dstDoc.Save(MyDir + “Out.docx”);"

I don’t mind saving the response in PDF or Word(Apose.word or pdf both are ok ). But I need in that format attached.

"Our requirement is to convert source word to PDF(Both in ZIP) "

Hoping for positive response.

@TejKamal_Thotakuri

Thanks for sharing the detail. In your code, you are importing only paragraph nodes from one document into another. We suggest you please use following code example to join documents.

Document dstDoc = new Document(MyDir + "DestinationInWord.docx");
Document srcDoc = new Document(MyDir + "Source.doc");

dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);

dstDoc.Save(MyDir + "Out.docx");

Please also read the following article. Hope this helps you.
Joining and Appending Documents

Hi Tahir,
Your suggested code just appends pages to template.
That’s not what I want .I want source doc to be formatted as destination doc in attachment.

Please look again at source and destination word doc you should understand.

@TejKamal_Thotakuri

Thanks for your inquiry. You can use TextColumnCollection.SetCount method to arrange text into the specified number of text columns. To change the orientation of the page, please use PageSetup.Orientation property.

Please use following code snippet to fit the table in page’s column.

foreach (Table table in doc.FirstSection.Body.Tables)
{
    table.AutoFit(AutoFitBehavior.AutoFitToWindow);
}

In this case, you need to resize the image according to page column width. Please get the width of text column using TextColumn.Width property.

Thanks Tahir for the hint.
I was able reduce the width of image which resolved problem .
But i still have issues with table.
Even with autofit some tables are not fitting to textColumn

Is there a way to reduce font size till table autofits ?

@TejKamal_Thotakuri

Thanks for your inquiry. You can change the font size of table’s text but you can not check that table is fit to text column. Have you tried the Table.AutoFit method? Please share the output document that shows the undesired behavior.

Yes I have tried using AUtoFit(). Doc.zip (917.1 KB)
Please find attached document.

@TejKamal_Thotakuri

Thanks for sharing the document. You can use TextColumnCollection.Spacing, Table.PreferredWidth, and CompatibilityOptions.GrowAutofit properties as shown below to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + @"1212.doc");

foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    table.PreferredWidth = PreferredWidth.FromPercent(90);
}

foreach (Section section in doc.Sections)
{
    section.PageSetup.TextColumns.Spacing = 20;
}

doc.CompatibilityOptions.GrowAutofit = false;
doc.Save(MyDir + "18.8.docx");

Thanks for all support.
I was able to resolve the issue :slight_smile:
Have a great day

@TejKamal_Thotakuri

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.