Hi, We are currently running into some issues while generating word document from aspose. We need the document to have left side bar to be of different color. But only the first page of single document is containing different color and all other subsequent pages are of different color. See the code we are using below.See the red lines where we are calling the function and the function definition.Document is generating the right color. But if a single document containns 2 or more pages. only the first page is showing the color. Other pages are coming without any leftsidebar as well as no color. We need to solve this issue urgently. Can we have a call to discuss this in detail. The client is wilmerhale.They have bought the aspose license.Any help on this would be really appreciated.
protected void PracticeDocGeneration(List pracContent, List contids) // JGS modified to accept two params
{
// Document Title
string DocumentTitle = "Practice_" + System.Guid.NewGuid().ToString().Substring(0, 4) + "_" + (Request.Form["TitleField"] == "" ? "Title" : Request.Form["TitleField"]) + ".docx";
// Get Asset template location.
string assetpath = GetTemplateLocation("Practice");
// Declaring main document ASPOSE object.
Document MainDoc = new Document();
Service objService = new Service();
PgSquareBanner.Sqaurebanner objSquareBanner = new PgSquareBanner.Sqaurebanner();
Office objOffice = new Office();
AlternateContent objaltcontent = new AlternateContent();
CurrentDevelopment objCurrentDevlopment = new CurrentDevelopment();
string Title = string.Empty;
string Img = string.Empty;
string BodyDescription = string.Empty;
int itemCount = 0;
if (pracContent.Count > 0) // JGS modified to check content data list not empty
{
foreach (ContentData contentData in pracContent) // JGS modified to loop content data
{
// ContentData contentData = contentManager.GetItem(uid); // JGS removed unnecessary code
if (contentData != null)
{
long PracticeSmartformId = contentData.XmlConfiguration.Id;
XDocument personal = XDocument.Parse(contentData.Html.ToString());
if (personal != null)
{
switch (PracticeSmartformId)
{
// Practice Or Industries(48924) - Service Smartform
case 7:
objService = GetObject(contentData.Html, false);
ContentData cd = new ContentData();
ContentManager cm = new ContentManager();
cd = cm.GetItem(contentData.Id, true);
string foldername = cd.FolderName;
if (!string.IsNullOrEmpty(foldername))
{
Title = foldername + " " + "-" + " ";
}
Title += objService.TxtName;
if (objService.ServiceImage != null)
{
if (objService.ServiceImage.img != null)
{
Img = objService.ServiceImage.img.src;
}
}
if (objService.Description.Any != null)
{
BodyDescription = EktronHelper.GetRichTextOuterXML(objService.Description);
if (BodyDescription.Contains(""))
{
BodyDescription = BodyDescription.Replace("", "");
}
}
break;
// Firm Information Or About - 8536 - CommonBannerwithHeading Smartform
case 28:
objSquareBanner = GetObject<PgSquareBanner.Sqaurebanner>(contentData.Html, false);
Title = objSquareBanner.Heading;
if (objSquareBanner.Image != null)
{
if (objSquareBanner.Image.img != null)
{
Img = objSquareBanner.Image.img.src;
}
}
if (objSquareBanner.Description.Any != null)
{
BodyDescription = GetRichTextOuterXML(objSquareBanner.Description);
if (BodyDescription.Contains(""))
{
BodyDescription = BodyDescription.Replace("", "");
}
}
break;
// Office - Office Smartform
case 10:
objOffice = GetObject(contentData.Html, false);
Title = objOffice.Name;
if (objOffice.OfficePhoto != null)
{
if (objOffice.OfficePhoto.img != null)
{
Img = objOffice.OfficePhoto.img.src;
}
}
if (objOffice.Description.Any != null)
{
BodyDescription = EktronHelper.GetRichTextOuterXML(objOffice.Description);
if (BodyDescription.Contains(""))
{
BodyDescription = BodyDescription.Replace("", "");
}
}
break;
case 24:
objCurrentDevlopment = GetObject(contentData.Html, false);
if (objCurrentDevlopment.TxtName.Any != null)
{
Title = EktronHelper.GetRichTextOuterXML(objCurrentDevlopment.TxtName);
}
if (objCurrentDevlopment.ServiceImage != null)
{
if (objCurrentDevlopment.ServiceImage.img != null)
{
Img = objCurrentDevlopment.ServiceImage.img.src;
}
}
if (objCurrentDevlopment.Description.Any != null)
{
BodyDescription = EktronHelper.GetRichTextOuterXML(objCurrentDevlopment.Description);
if (BodyDescription.Contains(""))
{
BodyDescription = BodyDescription.Replace("", "");
}
}
break;
case 37:
objaltcontent = GetObject(contentData.Html, false);
if (!string.IsNullOrEmpty(objaltcontent.Title))
{
Title = objaltcontent.Title;
}
if (objaltcontent.Description.Any != null)
{
BodyDescription = EktronHelper.GetRichTextOuterXML(objaltcontent.Description);
if (BodyDescription.Contains(""))
{
BodyDescription = BodyDescription.Replace("", "");
}
}
Img = null;
break;
}
try
{
Document DynamictDoc = new Document(assetpath);
// Creating two biography documents and merging into one final document.
// Open the template document with merge fields.
DocumentBuilder documentBuilder = new DocumentBuilder(DynamictDoc);
#region Service Image
string Practiceimg = string.Empty;
string strtitle = Request.Form["TitleField"].ToString();
if (!string.IsNullOrEmpty(strtitle) && strtitle.ToLower() != "Title".ToLower())
{
// documentBuilder.MoveToMergeField(Request.Form["TitleField"].ToString());
documentBuilder.MoveToMergeField("PracticeTitleHeading");
documentBuilder.InsertHtml(string.Format("{0}", strtitle));
}
// Added by Edynamics-Gaurav
if (!string.IsNullOrEmpty(Img))
{
if (Img.Contains("?"))
{
Practiceimg = Server.MapPath(Img.Split(’?’)[0]);
}
else
{
Practiceimg = Server.MapPath(Img);
}
if (Practiceimg != null)
{
string imageExtension = Path.GetExtension(Practiceimg);
Bitmap bmp = new Bitmap(Practiceimg);
bmp = MakeGrayscale(bmp);
// Bitmap b = new Bitmap(HttpContext.Current.Server.MapPath("~" + Practiceimg));
string imageName = Guid.NewGuid().ToString() + imageExtension;
bmp.Save(HttpContext.Current.Server.MapPath("~/PGImages/" + imageName));
Practiceimg = Server.MapPath("~/PGImages/" + imageName);
}
}
else
{
Practiceimg = Server.MapPath(Img);
}
if (!string.IsNullOrEmpty(Practiceimg))
{
// check if image exist before inserting
if (File.Exists(Practiceimg))
{
documentBuilder.InsertImage(Practiceimg, 85, 85);
System.IO.File.Delete(Practiceimg);
}
}
#endregion
if (long.Parse(hdnTemplateID.Value) == PRAC_TEMPLATE_ID_US_HEADER || long.Parse(hdnTemplateID.Value) == PRAC_TEMPLATE_ID_US_A4_HEADER)
{
SetSideBar(ref DynamictDoc, ref documentBuilder, contentData.FolderId);
}
documentBuilder.MoveToMergeField("SerName");
if (!string.IsNullOrEmpty(Practiceimg))
{
documentBuilder.InsertHtml(string.Format("{0}", Title));
}
else
{
documentBuilder.InsertHtml(string.Format("{0}", Title));
}
if (!string.IsNullOrEmpty(Title))
{
string footertext = string.Empty;
footertext = Title;
int strlength = Title.Length;
if (strlength > 60)
{
footertext = EktronHelper.Chop(Title, 60);
footertext += "…";
}
else
{
footertext = Title;
}
documentBuilder.MoveToMergeField("footerTitle");
documentBuilder.InsertHtml(string.Format("{0}", footertext.ToUpper()));
}
documentBuilder.MoveToMergeField("SerDescription");
StringBuilder str = new StringBuilder();
XmlDocument doc = new XmlDocument();
XmlElement rootNode = doc.CreateElement("root");
doc.AppendChild(rootNode);
XmlElement root = doc.DocumentElement;
if (root != null)
{
root.InnerXml = BodyDescription;
XmlNodeList pNodes = root.SelectNodes("//p");
if (pNodes != null && pNodes.Count > 0)
{
foreach (XmlNode pNode in pNodes)
{
if (!string.IsNullOrEmpty(pNode.InnerText.Trim()))
{
str.Append(pNode.OuterXml);
}
}
}
else
{
str.Append(BodyDescription);
}
}
documentBuilder.InsertHtml(str.ToString());
// Don’t need merge fields in the document anymore.
DynamictDoc.MailMerge.DeleteFields();
itemCount++;
if (itemCount < pracContent.Count)
{
documentBuilder.InsertBreak(BreakType.PageBreak);
}
if (string.IsNullOrEmpty(MainDoc.ToTxt().Trim()))
{
MainDoc = DynamictDoc;
}
else
{
// Merge two biography contents into one document
foreach (Section srcSection in DynamictDoc)
{
// Importing a node creates a copy of the original node
Node dstSection = MainDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
// Now the new section node can be appended to the destination document.
MainDoc.AppendChild(dstSection);
}
}
}
catch (Exception ex)
{
}
}
}
}
#region Saving DMS document and updating Proposal content
// Saving the document using bytes stream.
MemoryStream stream = new MemoryStream();
MainDoc.Save(stream, SaveFormat.Docx);
// Getting data in to bytes
long fileLength = stream.Length;
byte[] fileData = new byte[fileLength];
fileData = stream.ToArray();
// Adding DMS document and updating dms link in proposal content.
AddDMSAsset(fileData, DocumentTitle, 221);
#endregion
}
}
here is the function definition
private void SetSideBar(ref Document DynamictDoc, ref DocumentBuilder documentBuilder, long FolderId)
{
// GENERAL - RGB(152, 1, 46) - RED COLOR
System.Drawing.Color color = System.Drawing.Color.FromArgb(152, 1, 46);
string ImageFile = String.Empty;
System.Drawing.Image img;
double HeaderDistance = documentBuilder.PageSetup.HeaderDistance;
double PageWidth = documentBuilder.PageSetup.PageWidth;
double RightMargin = documentBuilder.PageSetup.RightMargin;
double RightBorder = documentBuilder.PageSetup.Borders.Right.LineWidth;
double PageHeight = documentBuilder.PageSetup.PageHeight;
double TopMargin = documentBuilder.PageSetup.TopMargin;
double BottomMargin = documentBuilder.PageSetup.BottomMargin;
double TopBorder = documentBuilder.PageSetup.Borders.Top.DistanceFromText;
double Top = -(TopMargin + HeaderDistance + TopBorder + 20);
double Height = PageHeight + TopMargin + BottomMargin;
double Width = 40;
if (FolderId == 181)
{
// Intellectual Property - RGB(229,130,0) - ORANGE
color = System.Drawing.Color.FromArgb(229, 130, 0);
ImageFile = Server.MapPath("…/…/images/WHProposalGenerator/IntellectualProperty.png");
}
if (FolderId == 182 || FolderId == 640)
{
// Litigation/Controversy - RGB(85,37,77) - PURPLE
color = System.Drawing.Color.FromArgb(85, 37, 77);
ImageFile = Server.MapPath("…/…/images/WHProposalGenerator/LitigationControversy.png");
}
// else if (FolderId == 183 || (FolderId >= 313 && FolderId <= 322))
else if (FolderId == 183)
{
// Regulatory and government affairs - RGB(90,127,146) - LIGHT BLUE
color = System.Drawing.Color.FromArgb(90, 127, 146);
ImageFile = Server.MapPath("…/…/images/WHProposalGenerator/RegulatoryAndGovernmentAffairs.png");
}
// else if (FolderId == 185 || (FolderId >= 294 && FolderId <= 301))
else if (FolderId == 185)
{
// Transactional - RGB(170,179,0) - GREEN
color = System.Drawing.Color.FromArgb(170, 179, 0);
ImageFile = Server.MapPath("…/…/images/WHProposalGenerator/Transactional.png");
}
else if (FolderId == 184)
{
// Securities - RGB(0,87,114) - DARK BLUE
color = System.Drawing.Color.FromArgb(0, 87, 114);
ImageFile = Server.MapPath("…/…/images/WHProposalGenerator/Securities.png");
}
// Create textbox shape and insert it into the document.
Aspose.Words.Drawing.Shape shape = new Aspose.Words.Drawing.Shape(DynamictDoc, Aspose.Words.Drawing.ShapeType.Rectangle);
double Left = PageWidth - RightMargin - shape.Width + 9;
shape.Height = Height;
shape.Width = Width;
shape.FillColor = color;
shape.Top = Top;
shape.Left = PageWidth - RightMargin - shape.Width + 9; ;
shape.BehindText = true;
shape.Name = "WHColorSideBar";
shape.DistanceTop = 0;
shape.StrokeWeight = 0;
documentBuilder.InsertNode(shape);
if (ImageFile != String.Empty)
{
img = new Bitmap(ImageFile);
// Create textbox shape and insert it into the document.
Aspose.Words.Drawing.Shape shapeImg = new Aspose.Words.Drawing.Shape(DynamictDoc, Aspose.Words.Drawing.ShapeType.Image);
shapeImg.Height = img.Height;
shapeImg.Width = img.Width;//
shapeImg.FillColor = color;
shapeImg.Top = -40;
shapeImg.Left = PageWidth - RightMargin - shape.Width + img.Width + 10;
shapeImg.BehindText = false;
shapeImg.ImageData.SetImage(ImageFile);
shapeImg.DistanceTop = 0;
documentBuilder.InsertNode(shapeImg);
}
}