Hi Team, Greetings
We are facing a tricky situation in Aspose word page size set up. Following code which is written in C# with Aspose Word has two core logics in it which is setting up Page Size and another renumbering page number, but these two are independent features. Tricky part if this variable isPagingRequired is true and the code inside that loop gets executed Page Size is set correctly if this is false and if I try to set up Page size it is not reflecting. Kindly need your technical assistance from Aspose to guide us on this. Note: We are saving in PDF format
Code:
DocumentBuilder builder = new DocumentBuilder(docnew);
SetPageSize(builder, pageSize);
if (isPagingRequired)
{
InsertPageNumber(builder, HeaderFooterType.FooterPrimary, "Shape 1", pageNumber, pageAlignment);
if (builder.CurrentSection.PageSetup.DifferentFirstPageHeaderFooter)
InsertPageNumber(builder, HeaderFooterType.FooterFirst, "Shape 2", pageNumber, pageAlignment);
if (builder.CurrentSection.PageSetup.OddAndEvenPagesHeaderFooter)
InsertPageNumber(builder, HeaderFooterType.FooterEven, "Shape 3", pageNumber, pageAlignment);
}
docnew.UpdateFields();
MemoryStream _stream = new MemoryStream();
docnew.Save(_stream, Aspose.Words.SaveFormat.Pdf);
Relevant Methods:
private void SetPageSize(DocumentBuilder builder, string pageSize)
{
Console.WriteLine("Set Page Size method");
switch (pageSize)
{
case LetterGenerationConstants.PageSizeA3:
{
builder.PageSetup.PaperSize = PaperSize.A3;
break;
}
case LetterGenerationConstants.PageSizeA4:
{
builder.PageSetup.PaperSize = PaperSize.A4;
break;
}
case LetterGenerationConstants.PageSizeLetter:
{
builder.PageSetup.PaperSize = PaperSize.Letter;
break;
}
default:
{
builder.PageSetup.PaperSize = PaperSize.Letter;
break;
}
}
}
private void InsertPageNumber(DocumentBuilder builder, HeaderFooterType headerFooterType, string styleName, int pageNum, string pageAlignment)
{
builder.MoveToHeaderFooter(headerFooterType);
//Insert a Text Box for Page Number and Set the alignment
Shape shape = new Shape(builder.Document, ShapeType.TextBox);
shape.Stroked = false;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.BottomMargin;
shape.Height = 25;
shape.Width = builder.CurrentSection.PageSetup.PageWidth;
// Specify font formatting
Style style = builder.Document.Styles.Add(StyleType.Paragraph, styleName);
style.Font.Size = 10;
style.Font.Bold = true;
style.Font.Name = "Arial";
style.Font.Color = System.Drawing.Color.Black;
Paragraph paragraph = new Paragraph(builder.Document);
builder.ParagraphFormat.Style = style;
switch (pageAlignment)
{
case LetterGenerationConstants.BottomLeft:
{
shape.TextBox.InternalMarginLeft = 80;
paragraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
break;
}
case LetterGenerationConstants.BottomCentre:
{
paragraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
break;
}
case LetterGenerationConstants.BottomRight:
{
shape.TextBox.InternalMarginRight = 80;
paragraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;
break;
}
default:
{
break;
}
}
shape.AppendChild(paragraph);
builder.InsertNode(shape);
builder.MoveTo(paragraph);
// Add fields for current page number
builder.PageSetup.PageStartingNumber = pageNum;
builder.PageSetup.RestartPageNumbering = true;
// Add any custom text
builder.InsertField("PAGE", string.Empty);
builder.MoveToDocumentEnd();
}