Table Inserted on earlier page breaks following page's layout

Hi

See the code below, if I insert a page before the existing pages it’s fine but if I add a table it throws out the layout, see the code example below and the right and wrong output attachments, wrong is when the table is included.

Would appreciate if you can work out what’s going wrong here.

Thanks

Simon

right.pdf (36.2 KB)
wrong.pdf (36.5 KB)
landscape.pdf (33.4 KB)

using Aspose.Pdf;
using Aspose.Pdf.Drawing;
using Aspose.Pdf.Forms;
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))
                {
                    AddPages(newPdf);

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

                    // Uncomment these lines and it throws out the page already added.

                    //Table tableTOC = new Table();
                    //tableTOC.ColumnWidths = "90% 10%";
                    //pageTOC.Paragraphs.Add(tableTOC);

                    newPdf.ProcessParagraphs();

                    newPdf.Optimize();
                    newPdf.Save();
                }
            }
        }

        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;

                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.Save();
            }
        }
    }
}

@simon.fairey

Thanks for your inquiry.

We have tested the scenario and observed the issue which you have mentioned. However, it has been found during initial investigation that the issue can be prevented by removing ProcessParagraphs() call. i.e.

newPdf.ProcessParagraphs();

Output PDF file with table seemed fine after removing above line of code. output.pdf (36.2 KB)

Concerning to the issue with ProcessParagraphs() method, we have logged an investigation ticket as PDFNET-45297 in our issue tracking system for further investigation. We will further check this in details and keep you informed with the status of its correction.

Please try removing the suggested line of code at your side and add table with complete data as per your requirements. In case you face any further issue, please feel free to let us know.

We are sorry for the inconvenience.

Hi,

Unfortunately in the “real” code we found we have to use ProcessParagraphs before adding the page because when we mix page sizes on a PDF if we don’t run it before adding the page to bake in any annotations and just run ProcessParagraphs at the end it goes into what appears to be an infinite loop and never recovers. It only does this when we have a scenario where we might be adding pages from multiple source PDFs that vary in size and orientation.

Thanks

Simon

@simon.fairey

Thanks for your response.

We have recorded your concerns along with the logged ticket and will definitely consider them while investigating the issue. Please spare us little time.

Hi,

Can you think of any way I can maybe force it to “close” the table to not affect subsequent pages, I tried also adding a new table to the new pages where it had a single 100% width column but that didn’t appear to work.

Would appreciate if you can think of any workarounds.

Simon

For info trying to generate a ToC using TocInfo also has the same issue

@simon.fairey

Would you please share sample code snippet, which you are using to add TOC in PDF which you have already shared with us. We will test the scenario in our environment and share our feedback with you.

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