I am building a document that must be printed in Word and PDF format. I have a stripped-down code example attached to replicate the problem. The footer uses the Word IF condition to determine if it is the last page or not. A different footer is shown on the last page than on the other pages. Footers are contained in table cells. For this example, there is a one-row table with two cells. The first cell is left justified and the second cell is right justified.
The problem is that all but the first cell of the table are removed if printing to PDF. In Word, it works perfectly.
Here is the code (also attached as a text file)
public void AsposeTest(string someText, bool asPDF)
{
Document doc = null;
DocumentBuilder builder = null;
byte[] byteArray = Encoding.ASCII.GetBytes(someText);
MemoryStream stream = new MemoryStream(byteArray);
doc = new Document(stream, new LoadOptions(LoadFormat.Html, string.Empty, string.Empty));
builder = new DocumentBuilder(doc);
Aspose.Words.Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
// conditional footer, last pg or only single page doc versus all other pages
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
// insert a table with 2 columns to hold footer
builder.StartTable();
// Calculate table width as total page width with left and right marins subtracted.
double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;
///////////////////////////////////////////////////////////////////// left side footer
builder.InsertCell();
builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.CellFormat.Width = tableWidth * .50;
Field field = builder.InsertField("IF \"", null);
FieldStart fieldStart = field.Start;
// Move DocumentBuilder cursor to the next run after field start.
builder.MoveTo(fieldStart.NextSibling.NextSibling);
builder.InsertField("PAGE", null);
builder.Write("\" = \"");
builder.InsertField("NUMPAGES", null);
builder.Write("\" \""); //start open quote of true condition
// Insert Conditional Part Of Footer Cell Into Builder (true part)
builder.InsertHtml("Left Justified Footer for LAST pg");
builder.Write("\" \""); //close quote for true condition and open quote of false condition
// Insert Conditional Part Of Footer Cell Into Builder (false part)
builder.InsertHtml("Left Justified Footer for OTHER pgs");
builder.Write("\""); //close quote for false condition
///////////////////////////////////////////////////////////////////// right side footer
builder.InsertCell();
builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.CellFormat.Width = tableWidth * .50;
field = builder.InsertField("IF \"", null);
fieldStart = field.Start;
// Move DocumentBuilder cursor to the next run after field start.
builder.MoveTo(fieldStart.NextSibling.NextSibling);
builder.InsertField("PAGE", null);
builder.Write("\" = \"");
builder.InsertField("NUMPAGES", null);
builder.Write("\" \""); //start open quote of true condition
// Insert Conditional Part Of Footer Cell Into Builder (true part)
builder.InsertHtml("right Justified Footer for LAST pg");
builder.Write("\" \""); //close quote for true condition and open quote of false condition
// Insert Conditional Part Of Footer Cell Into Builder (false part)
builder.InsertHtml("right Justified Footer for OTHER pgs");
builder.Write("\""); //close quote for false condition
builder.EndRow();
builder.EndTable();
doc.UpdateTableLayout();
doc.UpdateFields();
// print out word or PDF format
if (asPDF)
doc.Save(Response, "filename.pdf", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Pdf));
else
{
doc.Save(Response, "filename.docx", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Docx));
Response.End();
}
}
This message was posted using Aspose.Live 2 Forum (private + attachment)