Table Inserted on earlier page breaks following page's layout

I copied the ToCInfo code from another post and just inserted it into the existing program, can’t find much documentation on TocInfo though! Uses the same file as before for the final page

using Aspose.Pdf;
using Aspose.Pdf.Drawing;
using Aspose.Pdf.Forms;
using Aspose.Pdf.Text;
using System.IO;

namespace SomeAsposePoC
{
class Program
{
static void Main(string[] args)
{
License pdfLicense = new License();
pdfLicense.SetLicense(“Aspose.Total.lic”);
var pageDoc = new Document();
pageDoc.PageInfo.Margin.Bottom = 20;
pageDoc.PageInfo.Margin.Top = 0;
pageDoc.PageInfo.Margin.Left = 0;
pageDoc.PageInfo.Margin.Right = 0;
pageDoc.Save(@“C:\temp\OUTPUT.pdf”, SaveFormat.Pdf);
using (var filestream = new FileStream(@“C:\temp\OUTPUT.pdf”, FileMode.Open, FileAccess.ReadWrite))
{
using (var newPdf = new Document(filestream))
{
Page toc = newPdf.Pages.Add();
toc.PageInfo = new PageInfo
{
Width = PageSize.A4.Width,
Height = PageSize.A4.Height,
IsLandscape = false,
Margin = new MarginInfo(64, 28, 42, 85)
};
TocInfo tocInfo = new TocInfo();
TextFragment title = new TextFragment(“Table of Contents”);
title.TextState.FontSize = 20;
title.TextState.FontStyle = FontStyles.Bold;
title.TextState.HorizontalAlignment = HorizontalAlignment.Left;
tocInfo.Title = title;
toc.TocInfo = tocInfo;
Page page1 = newPdf.Pages.Add();
Page page2 = newPdf.Pages.Add();
var heading1 = new Heading(1);
heading1.Text = “Heading 1”;
heading1.IsAutoSequence = true;
heading1.IsInList = true;
heading1.Level = 1;
heading1.TocPage = toc;
page1.Paragraphs.Add(heading1);
string text1 = “Text1 Text1 Text1 …”;
page1.Paragraphs.Add(AddTextBlock(text1));
var heading2 = new Heading(2);
heading2.Text = “Heading 1.1”;
heading2.IsAutoSequence = true;
heading2.IsInList = true;
heading2.Level = 2;
heading2.TocPage = toc;
page1.Paragraphs.Add(heading2);
string text2 = “Text11 Text11 Text11 …”;
page1.Paragraphs.Add(AddTextBlock(text2));
var heading3 = new Heading(1);
heading3.Text = “Heading 2”;
heading3.IsAutoSequence = true;
heading3.IsInList = true;
heading3.Level = 1;
heading3.TocPage = toc;
page2.Paragraphs.Add(heading3);
string text3 = “Text2 Text2 Text2 …”;
page2.Paragraphs.Add(AddTextBlock(text3));
AddPages(newPdf);
newPdf.ProcessParagraphs();
newPdf.Optimize();
newPdf.Save();
}
}
}
static TextFragment AddTextBlock(string text)
{
var fragment = new TextFragment(text);
fragment.TextState.Font = FontRepository.FindFont(“Arial”);
fragment.TextState.FontSize = 10;
fragment.TextState.HorizontalAlignment = HorizontalAlignment.Left;
return fragment;
}
static void AddPages(Document pdf)
{
var colour = “#602D91”;
var topLeftXPos = 328;
var topLeftYPos = 273;
var bottomRightXPos = 944;
var bottomRightYPos = 426;
var pageWidth = 1280;
var pageHeight = 720;
var note = “Hoverover text”;
using (MemoryStream inMemoryCopy = new MemoryStream())
{
using (FileStream fs = File.OpenRead(@“C:\temp\landscape.pdf”))
{
fs.CopyTo(inMemoryCopy);
}
var pageDoc = new Document(inMemoryCopy);
pageDoc.PageInfo.Margin.Bottom = 20;
pageDoc.PageInfo.Margin.Top = 0;
pageDoc.PageInfo.Margin.Left = 0;
pageDoc.PageInfo.Margin.Right = 0;
pageDoc.PageInfo.Height = pageDoc.Pages[1].TrimBox.Height;
pageDoc.PageInfo.Width = pageDoc.Pages[1].TrimBox.Width;
Table tableTOC = new Table();
tableTOC.ColumnWidths = “100%”;
pageDoc.Pages[1].Paragraphs.Add(tableTOC);
var pdfPageHeight = pageDoc.PageInfo.Height;
var pdfPageWidth = pageDoc.PageInfo.Width;
var graph = new Graph((float)pdfPageWidth, (float)pdfPageHeight);
pageDoc.Pages[1].Paragraphs.Add(graph);
var xRatio = pdfPageWidth / pageWidth;
var yRatio = pdfPageHeight / pageHeight;
var actualRect = new Aspose.Pdf.Rectangle(
topLeftXPos * xRatio, // llx
pdfPageHeight - (bottomRightYPos * yRatio), // lly
bottomRightXPos * xRatio, // urx
pdfPageHeight - (topLeftYPos * yRatio)); // ury
var buttonField = new ButtonField(pageDoc.Pages[1], actualRect);
buttonField.AlternateName = $“Note :\n{note}”;
Aspose.Pdf.Drawing.Rectangle colourrect = new Aspose.Pdf.Drawing.Rectangle(
(float)actualRect.LLX,
(float)actualRect.LLY,
(float)actualRect.Width,
(float)actualRect.Height);
colourrect.GraphInfo.LineWidth = 0.001f;
colourrect.GraphInfo.FillColor = Color.FromArgb(40, int.Parse(colour.Substring(1, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(colour.Substring(3, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(colour.Substring(5, 2), System.Globalization.NumberStyles.HexNumber));
graph.Shapes.Add(colourrect);
pageDoc.Form.Add(buttonField);
pageDoc.ProcessParagraphs();
pdf.Pages.Add(pageDoc.Pages);
if (pageDoc.PageInfo.Height < pageDoc.PageInfo.Width)
{
pdf.Pages[pdf.Pages.Count].PageInfo.IsLandscape = true;
}
pdf.Pages[pdf.Pages.Count].Paragraphs.Add(tableTOC);
pdf.Save();
}
}
}
}

@simon.fairey

We have tried to run your code snippet and faced an exception saying “input stream was not in correct format.”. Would you please confirm if same code is running fine at your end with same PDF document which you have shared with us before.

Works fine for me using the landscape.pdf file I attached to the original file, all I added to the new code was the ToC bit

@simon.fairey

We have managed to execute the code without any exception and also observed the issue of white space at the top of last page inside PDF. I regret to share that we cannot offer any workaround for now, as issue is still pending for investigation. We have logged all these details along with earlier logged issue. As soon as we have some definite updates regarding its investigation progress, we will surely inform you. Please spare us little time.

We are sorry for the inconvenience.

Hi,

Do you have any idea on a timescale for the fix as it’s quite a serious issue for us currently

Thanks

Simon

@simon.fairey

Thanks for your inquiry.

Since your issue has been logged under free support model, it has low priority and will be investigated and resolved on first come first serve basis. Please note that there is a long queue of pending issue which were logged previously than yours and we regret that we cannot share any reliable ETA at the moment.

In case your issue is urgent and you need it to be resolved on priority basis, you may please consider our paid support option where issues are resolved with high priority. As soon as we have some definite updates regarding issue resolution, we will let you know. Please spare us little time.

We are sorry for the inconvenience.

Hi

I enquired regarding paid support and got a quote but they had no idea what sort of priority my bug would get once I had paid support. I don’t want to pay and then it’s not fixed for 12 months. They instructed me to ask support as they said you’d have a better idea so I’m trying to find out if I paid for support how much sooner would this issue be fixed?

Thanks

Simon

@simon.fairey

Thanks for your sharing your concerns.

We have recorded your concerns along with the logged ticket and will definitely consider them during initial investigations. We will soon share with you about our findings on how soon issue can be resolved if it is escalated to paid support.

Hi

Is there any update on this as it’s quite a serious issue for us

Thanks

Simon

@simon.fairey

Thanks for contacting support.

I am afraid that issue could not be investigated due to other high priority issues in the queue. However, we will definitely keep your concerns in view while looking into the issue and share our feedback with you as soon as some share-worthy updates are available. We greatly appreciate your patience and cooperation in this matter. Please spare us little time.

We are sorry for the inconvenience.

@simon.fairey

Thanks for your patience.

We have further investigated the scenario and found that you were using following lines of code in your program which caused page to rotate. Hence, page size was changing in final output.

if (pageDoc.PageInfo.Height < pageDoc.PageInfo.Width)
{
 pdf.Pages[pdf.Pages.Count].PageInfo.IsLandscape = true;
}

Please remove above lines of code which will resolve your issue. In case of any further assistance, please feel free to let us know.

Hi

That’s fine but how do we deal with when there is a landscape page if the table info is bleeding over into the next pages and breaks any page that needs to be rendered in landscape mode?

Si

Is there a way to “end” the table, I’ve tried creating a new table after the initial one set to 100% and single column but that doesn’t seem to help

@simon.fairey

Thanks for writing back.

Would you kindly share an expected PDF document with us. We will try to generate alike PDF in our environment and share our findings with you.

Hi

The very first post in this thread shows a right and a wrong document. Right is generated by not including a table and wrong when you add the table on the page before.

Simon

@simon.fairey

We have tested the scenario again using your earlier shared documents. In your shared code snippet and documents, you were inserting new page at the start of input PDF and adding table to newly inserted page causing extra space at the top of second page. By investigating your code snippet, we found the code which was causing page to rotate and raising the issue.

Nevertheless, by removing the suggested lines of code, the output PDF document was as per expectations. However, shared PDF document and code snippet are quite simplified and not sufficient to understand your complete requirements and scenario which you are implementing at your end.

Just in case you want to insert a page with landscape orientation, you can specify its height and width as following:

Page pageTOC = newPdf.Pages.Insert(1);
pageTOC.PageInfo.Margin = new MarginInfo(20, 20, 20, 20);
pageTOC.PageInfo.Height = PageSize.A4.Width;
pageTOC.PageInfo.Width = PageSize.A4.Height;

Furthermore, if our understandings are not correct about your actual requirements or scenario, we request you to please share some more details. We will again test the scenario in our environment and address it accordingly.

Been very busy recently but this issue has caused another problem when trying to put a page stamp right aligned in the footer and it goes off the edge of the page if we include a table of content. I will update this with a suitable example in the next couple of days

@simon.fairey

Please take your time for sharing the code snippet able to reproduce the issue you are experiencing. We will test the scenario in our environment and address it accordingly.

Hi

Having done more testing it appears this is indeed the issue and that as long as I set the height and width the page is correctly oriented.

However for other peoples reference I believe there was also a change in the libs that helped this as previously with an older version of the libs it didn’t work without the landscape setting but with the latest it does so that’s great.

Thanks

@simon.fairey

Thanks for your kind feedback.

It is good to know that your issue has been resolved. Please keep using our API and in case of any further query, please feel free to create a new topic.