Hi Nitin,
Thanks for your queries.You are using an old version of Aspose.Words. Please note that every new release of Aspose.Words comes up with some new features, enhancements in the existing features and bug fixes. Please use the latest version of Aspose.Words for .NET.
The code shared in my previous post is a sample code for your Object Model. You can get idea from following code snippet for your Object Model. As per your queries, you want to do followings:
- Insert Table of Contents in document
- Set the styles of Table of Contents
- How to insert image into document and set its size
- How to insert RTF contents into your document
Sample Code:
<span style=“font-family: “Courier New”; color: rgb(43, 145, 175);” lang=“EN-GB”>Document<span style=“font-family: “Courier New”;” lang=“EN-GB”> FinalDoc = new
Document();<o:p></o:p>
DocumentBuilder builder = new DocumentBuilder(FinalDoc);
builder.MoveTo(FinalDoc.FirstSection.Body.FirstParagraph);
//Object 1: Filepath to an image (.jpg) file - This must appear on the FIRST page and resize and fit the page.
//forced page break here
builder.MoveToParagraph(0, 0);
//Read image from file to Image object
Image img = Image.FromFile("d:\\Chrysanthemum.jpg");
double targetHeight;
double targetWidth;
CalculateImageSize(builder, img, out targetHeight, out targetWidth);
//Add the image to the document and set it's position and size
Shape shp = builder.InsertImage(img, targetWidth, targetHeight);
shp.WrapType = WrapType.Inline;
//Object 2: RTF Content (Title and some Copyright information)
Document docChapter1Title = RtfStringToDocument("chapter 1 title RTF STring");
Paragraph paragraph = builder.InsertParagraph();
InsertDocument(paragraph, docChapter1Title);
builder.InsertBreak(BreakType.PageBreak);
//Object 3: RTF Content (This is title of the Chapter1 so this must be of style Heading 1 so that it will appear in the TOC)
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Font.Color = Color.Blue;
builder.Writeln("Table of Contents ");
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.Writeln("");
// Your Code......
//Object 4: RTF Content - This is the RTF content (contains text and images) that belongs to the above chapter1
Document Object4Doc = RtfStringToDocument("Object 4: RTF Content - This is the RTF content (contains text and images) that belongs to the above chapter1");
paragraph = builder.InsertParagraph();
InsertDocument(paragraph, Object4Doc);
//Your Code...
//Call the UpdateFields method before saving the document
FinalDoc.UpdateFields();
FinalDoc.Save(MyDir + "AsposeOut.doc", SaveFormat.Doc);
//
/// Calculates size of Image
///
/// DocumentBuilder is used to determine Height and Width of current Page
/// Original image
/// Height of the image
/// Width of the image
private void CalculateImageSize(DocumentBuilder builder, Image img, out double targetHeight, out double targetWidth)
{
//Calculate width and height of the page
PageSetup ps = builder.CurrentSection.PageSetup;
targetHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin;
targetWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
//Get size of an image
double imgHeight = ConvertUtil.PixelToPoint(img.Height);
double imgWidth = ConvertUtil.PixelToPoint(img.Width);
if (imgHeight < targetHeight && imgWidth < targetWidth)
{
targetHeight = imgHeight;
targetWidth = imgWidth;
}
else
{
//Calculate size of an image in the document
double ratioWidth = imgWidth / targetWidth;
double ratioHeight = imgHeight / targetHeight;
if (ratioWidth > ratioHeight)
targetHeight = (targetHeight * (ratioHeight / ratioWidth));
else
targetHeight = (targetWidth * (ratioWidth / ratioHeight));
}
}
Please read following forum link for your kind reference.
https://forum.aspose.com/t/52359