First off, thanks for taking the time to look at this issue for me. The code I am using during testing is below. If the code is run as is, the foreach section of the PageType.Custom3 case in AppendDocument will add a floating box as a header and footer. If you look at the output you will see that all of the pages are of equal size and orientation. If you comment out the two lines within the foreach of the PageType.Custom3 case and regenerate the document you will see that the output pages are correct (document 3 is A3 landscape).
As I am sure you will be able to note, I have also made the two header/footer floating boxes have very obvious background colours during testing. The actual colour, however, doesn’t change the outcome.
public void AppendPdf()
{
var pageInfoList = new List<PageInfo>();
<span style="color:green;">// Instantiate License class and call its SetLicense method to use the license</span>
<span style="color:blue;">var</span> license = <span style="color:blue;">new</span> <span style="color:darkblue;">License</span>();
license.<span style="color:darkcyan;">SetLicense</span>(<span style="color:#a31515;">"Aspose.Pdf.lic"</span>);
<span style="color:blue;">var</span> pageInfos = <span style="color:blue;">new</span> <span style="color:darkblue;">Dictionary</span><<span style="color:darkblue;">PageType</span>, <span style="color:darkblue;">PageInfo</span>>();
<span style="color:blue;">var</span> outputDocument = <span style="color:blue;">new</span> <span style="color:darkblue;">Document</span>();
<span style="color:blue;">var</span> inputDocument1 = <span style="color:blue;">new</span> <span style="color:darkblue;">Document</span>(<span style="color:#a31515;">@"D:\append-2.pdf"</span>);
<span style="color:blue;">this</span>.<span style="color:darkcyan;">AppendDocument</span>(inputDocument1, <span style="color:darkblue;">PageType</span>.<span style="font-weight:bold;color:purple;">Custom1</span>, outputDocument, pageInfos);
<span style="color:blue;">var</span> inputDocument2 = <span style="color:blue;">new</span> <span style="color:darkblue;">Document</span>(<span style="color:#a31515;">@"D:\append-3.pdf"</span>);
<span style="color:blue;">this</span>.<span style="color:darkcyan;">AppendDocument</span>(inputDocument2, <span style="color:darkblue;">PageType</span>.<span style="font-weight:bold;color:purple;">Custom2</span>, outputDocument, pageInfos);
<span style="color:blue;">var</span> inputDocument3 = <span style="color:blue;">new</span> <span style="color:darkblue;">Document</span>(<span style="color:#a31515;">@"D:\append-4.pdf"</span>);
<span style="color:blue;">this</span>.<span style="color:darkcyan;">AppendDocument</span>(inputDocument3, <span style="color:darkblue;">PageType</span>.<span style="font-weight:bold;color:purple;">Custom3</span>, outputDocument, pageInfos);
<span style="color:darkcyan;">AddHeaderAndFooter</span>(outputDocument);
outputDocument.<span style="color:darkcyan;">Save</span>(<span style="color:#a31515;">@"D:\output.pdf"</span>);
}
<span style="color:blue;">void</span> <span style="color:darkcyan;">AppendDocument</span>(<span style="color:darkblue;">Document</span> inputDocument, <span style="color:darkblue;">PageType</span> pageType, <span style="color:darkblue;">Document</span> outputDocument, <span style="color:darkblue;">IDictionary</span><<span style="color:darkblue;">PageType</span>, <span style="color:darkblue;">PageInfo</span>> pageInfos)
{
<span style="color:blue;">var</span> startPage = outputDocument.<span style="color:purple;">Pages</span>.<span style="color:purple;">Count</span> + 1;
outputDocument.<span style="color:purple;">Pages</span>.<span style="color:darkcyan;">Add</span>(inputDocument.<span style="color:purple;">Pages</span>);
<span style="color:blue;">var</span> endPage = outputDocument.<span style="color:purple;">Pages</span>.<span style="color:purple;">Count</span>;
<span style="color:blue;">var</span> pageRange = <span style="color:darkblue;">Enumerable</span>.<span style="color:darkcyan;">Range</span>(startPage, endPage - startPage + 1).<span style="color:darkcyan;">ToList</span>();
<span style="color:darkblue;">PageInfo</span> pageInfo;
<span style="color:blue;">if</span> (!pageInfos.<span style="color:darkcyan;">TryGetValue</span>(pageType, <span style="color:blue;">out</span> pageInfo))
{
pageInfo = <span style="color:blue;">new</span> <span style="color:darkblue;">PageInfo</span> { <span style="color:purple;">Type</span> = pageType };
pageInfos[pageType] = pageInfo;
<span style="color:blue;">var</span> pageRect = outputDocument.<span style="color:purple;">Pages</span>[startPage].<span style="color:darkcyan;">GetPageRect</span>(<span style="color:blue;">true</span>);
<span style="color:blue;">var</span> pageMargin = outputDocument.<span style="color:purple;">Pages</span>[startPage].<span style="color:purple;">PageInfo</span>.<span style="color:purple;">Margin</span>;
<span style="color:blue;">switch</span> (pageType)
{
<span style="color:blue;">case</span> <span style="color:darkblue;">PageType</span>.<span style="font-weight:bold;color:purple;">Custom1</span>:
{
<span style="color:green;">// Add blanking text floats</span>
<span style="color:blue;">var</span> headerBlank = <span style="color:blue;">new</span> <span style="color:darkblue;">Aspose</span>.<span style="color:darkblue;">Pdf</span>.<span style="color:darkblue;">FloatingBox</span>((<span style="color:blue;">float</span>)pageRect.<span style="color:purple;">Width</span>, 50)
{
<span style="color:purple;">Top</span> = -pageMargin.<span style="color:purple;">Top</span>,
<span style="color:purple;">Left</span> = -pageMargin.<span style="color:purple;">Left</span>,
<span style="color:purple;">BackgroundColor</span> = outputDocument.<span style="color:purple;">Pages</span>[startPage].<span style="color:purple;">Background</span>
};
<span style="color:blue;">var</span> footerBlank = <span style="color:blue;">new</span> <span style="color:darkblue;">Aspose</span>.<span style="color:darkblue;">Pdf</span>.<span style="color:darkblue;">FloatingBox</span>((<span style="color:blue;">float</span>)pageRect.<span style="color:purple;">Width</span>, 50)
{
<span style="color:purple;">Top</span> = pageRect.<span style="color:purple;">Height</span> - 80 - pageMargin.<span style="color:purple;">Top</span>,
<span style="color:purple;">Left</span> = -pageMargin.<span style="color:purple;">Left</span>,
<span style="color:purple;">BackgroundColor</span> = outputDocument.<span style="color:purple;">Pages</span>[startPage].<span style="color:purple;">Background</span>
};
<span style="color:blue;">foreach</span> (<span style="color:blue;">var</span> <span style="font-weight:bold;">pageNum</span> <span style="color:blue;">in</span> pageRange)
{
outputDocument.<span style="color:purple;">Pages</span>[<span style="font-weight:bold;">pageNum</span>].<span style="color:purple;">Paragraphs</span>.<span style="color:darkcyan;">Add</span>(headerBlank);
outputDocument.<span style="color:purple;">Pages</span>[<span style="font-weight:bold;">pageNum</span>].<span style="color:purple;">Paragraphs</span>.<span style="color:darkcyan;">Add</span>(footerBlank);
}
<span style="color:blue;">break</span>;
}
<span style="color:blue;">case</span> <span style="color:darkblue;">PageType</span>.<span style="font-weight:bold;color:purple;">Custom2</span>:
{
<span style="color:green;">// This document type doesn't have a header or footer</span>
<span style="color:blue;">break</span>;
}
<span style="color:blue;">case</span> <span style="color:darkblue;">PageType</span>.<span style="font-weight:bold;color:purple;">Custom3</span>:
{
<span style="color:green;">// Add blanking text floats</span>
<span style="color:blue;">var</span> headerBlank = <span style="color:blue;">new</span> <span style="color:darkblue;">Aspose</span>.<span style="color:darkblue;">Pdf</span>.<span style="color:darkblue;">FloatingBox</span>((<span style="color:blue;">float</span>)pageRect.<span style="color:purple;">Width</span>, 50)
{
<span style="color:purple;">Top</span> = -pageMargin.<span style="color:purple;">Top</span>,
<span style="color:purple;">Left</span> = -pageMargin.<span style="color:purple;">Left</span>,
<span style="color:purple;">BackgroundColor</span> = <span style="color:darkblue;">Aspose</span>.<span style="color:darkblue;">Pdf</span>.<span style="color:darkblue;">Color</span>.<span style="color:purple;">DarkGreen</span>
// BackgroundColor = outputDocument.Pages[startPage].Background
};
<span style="color:blue;">var</span> footerBlank = <span style="color:blue;">new</span> <span style="color:darkblue;">Aspose</span>.<span style="color:darkblue;">Pdf</span>.<span style="color:darkblue;">FloatingBox</span>((<span style="color:blue;">float</span>)pageRect.<span style="color:purple;">Width</span>, 50)
{
<span style="color:purple;">Top</span> = pageRect.<span style="color:purple;">Height</span> - 80 - pageMargin.<span style="color:purple;">Top</span>,
<span style="color:purple;">Left</span> = -pageMargin.<span style="color:purple;">Left</span>,
// BackgroundColor = outputDocument.Pages[startPage].Background
BackgroundColor = Aspose.Pdf.Color.DarkBlue
};
<span style="color:blue;">foreach</span> (<span style="color:blue;">var</span> <span style="font-weight:bold;">pageNum</span> <span style="color:blue;">in</span> pageRange)
{
outputDocument.<span style="color:purple;">Pages</span>[<span style="font-weight:bold;">pageNum</span>].<span style="color:purple;">Paragraphs</span>.<span style="color:darkcyan;">Add</span>(headerBlank);
outputDocument.<span style="color:purple;">Pages</span>[<span style="font-weight:bold;">pageNum</span>].<span style="color:purple;">Paragraphs</span>.<span style="color:darkcyan;">Add</span>(footerBlank);
}
<span style="color:blue;">break</span>;
}
<span style="color:blue;">case</span> <span style="color:darkblue;">PageType</span>.<span style="font-weight:bold;color:purple;">Custom4</span>:
{
<span style="color:blue;">break</span>;
}
}
}
pageInfo.<span style="color:purple;">Pages</span>.<span style="color:darkcyan;">AddRange</span>(pageRange);
}
<span style="color:blue;">void</span> <span style="color:darkcyan;">AddHeaderAndFooter</span>(<span style="color:darkblue;">Document</span> document)
{
<span style="color:blue;">var</span> pageCount = document.<span style="color:purple;">Pages</span>.<span style="color:purple;">Count</span>;
<span style="color:green;">// Add page number stamp</span>
<span style="color:blue;">var</span> pageNumberStamp = <span style="color:blue;">new</span> <span style="color:darkblue;">PageNumberStamp</span>
{
<span style="color:purple;">Background</span> = <span style="color:blue;">false</span>,
<span style="color:purple;">Format</span> = <span style="color:#a31515;">"Page # of "</span> + pageCount,
<span style="color:purple;">BottomMargin</span> = 10,
<span style="color:purple;">RightMargin</span> = 10,
<span style="color:purple;">HorizontalAlignment</span> = <span style="color:darkblue;">HorizontalAlignment</span>.<span style="font-weight:bold;color:purple;">Right</span>,
<span style="color:purple;">StartingNumber</span> = 1
};
pageNumberStamp.<span style="color:purple;">TextState</span>.<span style="color:purple;">Font</span> = <span style="color:darkblue;">FontRepository</span>.<span style="color:darkcyan;">FindFont</span>(<span style="color:#a31515;">"Arial"</span>);
pageNumberStamp.<span style="color:purple;">TextState</span>.<span style="color:purple;">FontSize</span> = 10.0F;
pageNumberStamp.<span style="color:purple;">TextState</span>.<span style="color:purple;">FontStyle</span> = <span style="color:darkblue;">FontStyles</span>.<span style="font-weight:bold;color:purple;">Bold</span>;
pageNumberStamp.<span style="color:purple;">TextState</span>.<span style="color:purple;">FontStyle</span> = <span style="color:darkblue;">FontStyles</span>.<span style="font-weight:bold;color:purple;">Italic</span>;
pageNumberStamp.<span style="color:purple;">TextState</span>.<span style="color:purple;">ForegroundColor</span> = <span style="color:darkblue;">Aspose</span>.<span style="color:darkblue;">Pdf</span>.<span style="color:darkblue;">Color</span>.<span style="color:purple;">Black</span>;
<span style="color:blue;">foreach</span> (<span style="color:blue;">var</span> <span style="font-weight:bold;">rawPage</span> <span style="color:blue;">in</span> document.<span style="color:purple;">Pages</span>)
{
<span style="color:blue;">var</span> page = <span style="font-weight:bold;">rawPage</span> <span style="color:blue;">as</span> <span style="color:darkblue;">Page</span>;
<span style="color:blue;">if</span> (page != <span style="color:blue;">null</span>)
{
<span style="color:blue;">var</span> pageRect = page.<span style="color:darkcyan;">GetPageRect</span>(<span style="color:blue;">true</span>);
page.<span style="color:darkcyan;">AddStamp</span>(pageNumberStamp);
}
}
}</pre></div><div><br></div><div>Along with the three input PDFs, I have also attached the two outputs I am getting at the moment - one with the floating box additions to Custom3 commented out and one with the floating boxes added.</div><div><br></div><div>Lastly, the three input files were generated from Microsoft Word documents as test documents. Testing on the live documents has the same effect.</div><div><br></div><div>Thanks again for your help.</div><div><br></div><div>regards,</div><div>John.</div>