Finding Node (no paragraph) page index

I have installed the template font, what I saw is that the first shape is “repeated” on other pages: if I select it → it’s selected on all pages

Using the suggested code generates an exception

:frowning:

@Jackomino Try adding the following condition at the beginning of the loop:

// Skip shapes in header/footer and process only top level shapes.
if (!s.IsTopLevel || s.GetAncestor(NodeType.HeaderFooter) != null)
    continue;

Ok thanks.

Our idea is to let the user place the shape wherever he wants in the template, then we create a floating one in each page. But it seems that if it’s placed in footer area → it’s linked to the footer, but we want it to be floating, like a watermark I mean

Hard job !!!

Thanks again

@Jackomino Yes, this is a tricky task since, MS Word are flow by their nature. Content is reflowed into pages on the fly and layout depend on many factors such as fonts, MS Word version etc. That is why when you add watermark using MS Word, it is added to header so it is repeated on each page.

Can I do the same so ?!?!

At the moment I loop on paragraph to collect the start page of each of them and shape position is related to page… I should anchor it to header so ?

This is the method to read shape from template and the found shape is cloned on each page

public static Shape GetShapeByTextContent(Document document, string textContext, ShapeType? shapeType = null)
{
    Shape foundShape = null;

    // Get all shapes in the document.
    foreach (Node node in document.Sections)
    {
        IEnumerable<Shape> shapes = ((Section)node).Body.GetChildNodes(NodeType.Shape, isDeep: true).Cast<Shape>();
        foreach (Shape shape in shapes)
        {
            if (shapeType != null)
            {
                if (shape.ShapeType != shapeType)
                {
                    continue;
                }
            }

            string shapeTextContent = shape.GetText().Replace("\r", string.Empty);

            if (shapeTextContent != textContext)
            {
                continue;
            }

            foundShape = (Shape)shape.Clone(isCloneChildren: false);

            foundShape.AppendChild(new Paragraph(document));
            foundShape.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            foundShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
            foundShape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
            foundShape.WrapType = WrapType.None;
            foundShape.ZOrder = 100;
            foundShape.BehindText = true;
            //foundShape.AnchorLocked = true;

            //foundShape.IsLayoutInCell = false;

            shape.Remove();
        }
    }

    //if (foundShape != null)
    //{
    //    document.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010);
    //}

    return foundShape;
}

you mean the same as here Document.ExtractPages | Aspose.Words for .NET

Remarks

The resulting document should look like the one in MS Word, as if we had performed ‘Print specific pages’ – the numbering, headers/footers and cross tables layout will be preserved. But due to a large number of nuances, appearing while reducing the number of pages, full match of the layout is a quiet complicated task requiring a lot of effort. Depending on the document complexity there might be slight differences in the resulting document contents layout comparing to the source document. Any feedback would be greatly appreciated.

I found this…

https://stackoverflow.com/questions/77347085/aspose-word-custom-footer-for-each-page

// Clone source document.
Document result = (Document)doc.deepClone(false);

// Split and rejoin document by pages.
for (int i = 0; i < doc.getPageCount(); i++)
{
    Document page = doc.extractPages(i, 1);
    // Remove old Header/Footer
    for (Section s : page.getSections())
    {
        s.getHeadersFooters().clear();
        s.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
        s.getPageSetup().setDifferentFirstPageHeaderFooter(false);
    }
    // Add new header to the first section of page.
    DocumentBuilder pageBuilder = new DocumentBuilder(page);
    pageBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
    pageBuilder.write("This is specific header for " + i + " page");

    result.appendDocument(page, ImportFormatMode.USE_DESTINATION_STYLES);
}

@Jackomino Yes, this can be the solution. But splitting the document into pages and rejoining it might affect the original document layout. Also, the code is in Java, you are using .NET, but the code in C# is pretty much the same. There should not be problems with translation.

I think I found the problem on duplicated shapes: the problem is when the paragraph is a table as we have set to repeat table header on each page so the first shape is duplicated on each page.

I’m trying to anchor the first shape to the first row that have “Repeat as header row at the top of each page” = false

I guess getHeadingFormat() = false

Keep pushing

1 Like

@Jackomino It is perfect that you managed to find the reason of the problem.

Hi, I need more help:
the paragraph in the picture below is on page 16, but when I ask

int paraPage = layoutCollector.GetStartPageIndex(para);

paraPage is equal to 17

Could you explain me why ?

Reading Layout collector returning wrong page number in specific conditions - #10 by awais.hafeez

Word file is not ok, while the same file save into PDF format is ok for paragraph… but not OK for column width ???

Thanks a lot in advance
Jack

@Jackomino Could you please attach your input document and simple code that will allow us to reproduce the problem on our side?

Good morning,
here you can find the imput document, regarding the DataSet used to fill the final document, how can I share data with you ?
I mean an Excel file can be ok ? as the problem is strictly linked to template layout + data

In order to create the triangular grid, our flow is the following:

  • in the template we set a placeholder with specific text
  • once text is found, there are C# methods that write rows and cols in a table, managing borders to have the “triangle”

We also paid for support, just to inform you, if there are better ways to share data to find out the problem
Anonim_template.docx (73.0 KB)

@Jackomino You can attach a .zip file here containing files and code that will help us reproduce the issue. If you have paid support, you can also create tickets in paid support helpdesk, which is in a different location than the free support forum.

Thanks,
here you can find template and resulting document

37435.zip (141.7 KB)

Anonim_template.docx (73.0 KB)

We look for a table with specific text

then we create a table dynamically with data coming from database
Table.docx (14.7 KB)

Please let me know if you need more info, I’m trying to understand how send you also data

Jack

@Jackomino The problem seems to be two shapes on the table page (in MS Word 2019). If you remove one shape, the table displays correctly in the PDF file. Are these two shapes the expected behavior?

Hi,
you have 2 shapes on the same page due to error in getting paragraph:
when we ask Aspose to list the paragraphs in the document, the library returns one paragraph on page 16 and one on page 17 (the row starting with grayed cell with 1) but paragraph on page 17 is created on page 16 → PDF has paragraph in right position

I can also make cell border of the “paragraph” returned by Aspose in red, if you prefer

Thanks for your help
Jack

@Jackomino Ok, thank you for the information.

Good morning,
do you have any news regarding this matter ?

Thanks in advance
Have a nice day
Jack

@Jackomino This issue is related to Doc to PDF error on table column size - #12 by Jackomino and will be fixed as soon as the issue is fixed in the appropriate thread.