Hii Team,
the above code is not working for me.
I put my full code below. could you please check it??
public List<byte[]> Convert(ContentsDto content, IDictionary<string, string> options, System.Func<DependentContent, Task<ContentsDto>> GetDependency = null)
{
License htmlLicense1 = new License();
htmlLicense1.SetLicense("Aspose.Words.NET.lic");
HtmlDocument htmlDocument = new HtmlDocument();
using (var htmlStream = new MemoryStream(content.Data))
{
htmlDocument.Load(htmlStream);
}
var byteArray = new List<byte[]>();
using (var dataStream = new MemoryStream(content.Data))
{
HtmlLoadOptions docOptions = new HtmlLoadOptions();
docOptions.ResourceLoadingCallback = new AsposeWordsAuthHandler(_settingsProvider, _authSettings, _logger);
var document = new Aspose.Words.Document(dataStream, docOptions);
using (var outputStream = new MemoryStream())
{
using (var htmlStream = new MemoryStream(content.Data))
{
htmlDocument.Load(htmlStream);
}
ApplyImageFormatting(document, htmlDocument);
FormatOutput(GetDependency, document, htmlDocument);
document.Save(outputStream, SaveFormat.Docx);
byteArray.Add(outputStream.ToArray());
}
}
return byteArray;
}
private static void FormatOutput(Func<DependentContent, Task<ContentsDto>> GetDependency, Aspose.Words.Document document, HtmlDocument htmlDocument)
{
document.Sections[0].PageSetup.LeftMargin = 50f;
document.Sections[0].PageSetup.RightMargin = 50f;
document.Sections[0].PageSetup.TopMargin = 50f;
document.Sections[0].PageSetup.BottomMargin = 60f;
AddHeader(document, GetDependency);
AddFooter(document);
}
private static void AddHeader(Document document, System.Func<DependentContent, Task<ContentsDto>> GetDependency)
{
var dependentContent = new DependentContent()
{
Id = "fa197b43-5b2a-4c53-bddd-3dad534f0284",
};
var content = GetDependency(dependentContent).Result;
if (content.Found)
{
DocumentBuilder builder = new DocumentBuilder(document);
Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
pageSetup.HeaderDistance = 20;
pageSetup.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//Initialize a Header Instance
using (var inputStream = new MemoryStream(content.Data))
{
var inputImageFromStream = Image.FromStream(inputStream);
builder.InsertImage(inputImageFromStream, RelativeHorizontalPosition.Page, 10, RelativeVerticalPosition.Page, 10, 50, 50, WrapType.Through);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
}
}
}
private static void AddFooter(Document document)
{
DocumentBuilder builder = new DocumentBuilder(document);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Page ");
builder.InsertField("PAGE", "");
builder.Write(" of ");
builder.InsertField("NUMPAGES", "");
}
private static void ApplyImageFormatting(Document document, HtmlDocument htmlDocument)
{
HtmlNodeCollection images = htmlDocument.DocumentNode.SelectNodes("//img");
Node node = document;
NodeCollection shapes = document.GetChildNodes(NodeType.Shape, true);
DocumentBuilder builder = new DocumentBuilder(document);
var img = 0;
if (images != null)
{
var imgCount = 0;
foreach (Shape shape in shapes)
{
var image = images.ElementAt(imgCount);
if (image.HasClass("fr-fil"))
{
shape.WrapType = WrapType.Square;
}
else
{
shape.HorizontalAlignment = HorizontalAlignment.Center;
}
shape.AllowOverlap = false;
imgCount++;
}
}
}