Hi all,
When I insert an image to my existing document, it works as expected.
But when I insert an image and a table of content to my existing document, it doesn’t. There are 2 images in my document. I don’t know why. Please help me in this case. I am using Aspose.Words: 17.7.0
Please take a look on 2 lines:
builder.InsertTableOfContents("\o “1-3” \h \z \u");
dstDoc.UpdateFields();
If I comment 2 above lines, it works. But I also want to have a table of content in my document, then it doesn’t.
This is my function. I also upload my 2 generated documents. Please help to take a look.
Thank you so much.
public async Task<IActionResult> GenerateBooklet2()
{
try
{
var documentIds = new List<int> {21};
var bookletName = $"Generated Booklet_{DateTime.Now.ToString("ddMMyyyyHHmm")}.docx";
Document dstDoc = new Document("bookletCoverTemplate.docx");
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Table of Contents");
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
IAzureAdTokenService tokenCache = (IAzureAdTokenService)HttpContext.RequestServices.GetService(typeof(IAzureAdTokenService));
ClientContext context = await tokenCache.GetExpd8SiteClientContextAsync();
var docs = _sharePointService.GetDocumentByIds(context, Constants.DocumentTemplates, documentIds.ToArray());
var fileName = string.Empty;
foreach (var doc in docs)
{
var binaryStream = doc.File.OpenBinaryStream();
context.Load(doc);
context.ExecuteQuery();
fileName = doc["FileLeafRef"].ToString();
AddItem(builder, "Item " + fileName);
if (fileName.IsImage())
{
ExtensionMethod.CreateFileStream(binaryStream.Value, fileName);
builder.MoveToDocumentEnd();
builder.InsertImage(fileName);
System.IO.File.Delete(fileName);
}
}
dstDoc.UpdateFields();
dstDoc.Save("C:\\" + bookletName);
return Ok(1);
}
catch (Exception ex)
{
_telemetry.TrackException(ex);
return new StatusCodeResult(500);
}
}
private void AddItem(DocumentBuilder builder, string text)
{
Aspose.Words.Font font = builder.Font;
font.Size = 26;
font.Bold = true;
font.Name = "Arial";
builder.InsertBreak(BreakType.LineBreak);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln(text);
}
Denerated Documents.zip (492.1 KB)