Appending PDF with table in DOCX creating issues

Hello Team,

We are trying to append one PDF document which has one table in it into our DOCX document. In this process, the final word document is loosing table width as well as the cell height and because of that the text is getting cut in table.

Please help us on what should be set in table to have proper table in our final document.

Thanks
Hardik

@HardikS Could you please attach your input PDD document and the problematic output? We will check the issue and provide you more information.

Here is the attached details.

  1. Source Document - SampleDoc.docx
  2. PDF with table - TablePDF2.pdf
  3. Final Outcome - FinalTableOutput.docx

SampleDoc.docx (16.6 KB)
FinalTableOutput.docx (28.1 KB)
TablePDF2.pdf (650.6 KB)

@HardikS I cannot preproduce the problem on my side. I used the following simple code for testing:

Document dst = new Document(@"C:\Temp\SampleDoc.docx");
Document src = new Document(@"C:\Temp\TablePDF2.pdf");
dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
dst.Save(@"C:\Temp\out.docx");

Please see the output document produced on my side: out.docx (23.2 KB)

That is because you maintained KeepSourceFormatting but with that the width is not common for all pages. We need all pages with A4 size as standard width and margin hence we have maintained the code like that.

Additionally I tried maintaining table width using below code.

foreach (Table tbl in srcDoc.GetChildNodes(NodeType.Table, true))
{
    //AutoFit Tables To Window
    tbl.AllowAutoFit = true;
    tbl.AutoFit(AutoFitBehavior.AutoFitToWindow);
}

Document first_Page = srcDoc.ExtractPages(0, 1);

foreach (Section section in srcDoc.Sections)
{
    PageSetup pageSetup = section.PageSetup;
    pageSetup.LeftMargin = first_Page.FirstSection.PageSetup.LeftMargin;
    pageSetup.RightMargin = first_Page.FirstSection.PageSetup.RightMargin;
    pageSetup.TopMargin = first_Page.FirstSection.PageSetup.TopMargin;
    pageSetup.BottomMargin = first_Page.FirstSection.PageSetup.BottomMargin;
    section.PageSetup.PaperSize = Aspose.Words.PaperSize.A4;
}

Table firsttbl = (Table)srcDoc.GetChildNodes(NodeType.Table, true)[0];
double standardtableWidth = firsttbl.PreferredWidth.Value;

LayoutCollector collector = new LayoutCollector(srcDoc);
LayoutEnumerator enumerator = new LayoutEnumerator(srcDoc);


foreach (Table tbl in srcDoc.GetChildNodes(NodeType.Table, true))
{
    double percWidth = 0;
    if (tbl.PreferredWidth.Type == PreferredWidthType.Percent)
    {
        Row row = tbl.FirstRow;
        Cell firstCell = row.FirstCell;
        Cell lastCell = row.LastCell;

        enumerator.Current = collector.GetEntity(firstCell.FirstParagraph);
        enumerator.MoveParent(LayoutEntityType.Cell);

        double firststart = enumerator.Rectangle.X;
        double width = enumerator.Rectangle.Width;

        enumerator.Current = collector.GetEntity(lastCell.LastParagraph);
        enumerator.MoveParent(LayoutEntityType.Cell);

        double laststart = enumerator.Rectangle.X;
        double lastwidth = enumerator.Rectangle.Width;

        percWidth = (laststart - firststart) + lastwidth;
    }
    //AutoFit Tables To Window
    if (tbl.PreferredWidth.Type == PreferredWidthType.Points && tbl.PreferredWidth.Value > standardtableWidth)
        tbl.PreferredWidth = PreferredWidth.FromPoints(standardtableWidth);
    if (tbl.PreferredWidth.Type == PreferredWidthType.Percent && percWidth > standardtableWidth)
    {
        tbl.PreferredWidth = PreferredWidth.FromPoints(standardtableWidth);
        tbl.TextWrapping = TextWrapping.Around;
        tbl.AutoFit(AutoFitBehavior.AutoFitToWindow);

    }
    //if (tbl.PreferredWidth.Type == PreferredWidthType.Percent && percWidth > standardtableWidth)
    //{
    //    tbl.AllowAutoFit = true;
    //    tbl.TextWrapping = TextWrapping.Around;
    //    tbl.AutoFit(AutoFitBehavior.AutoFitToContents);
    //}
}
srcDoc.UpdatePageLayout();

The output document is attached here.FinalTableOutput_1.docx (27.9 KB)

As you can see, the table has text but it is getting cut.

Thanks
Hardik

@HardikS Please try using the following code:

Document dst = new Document(@"C:\Temp\SampleDoc.docx");
Document src = new Document(@"C:\Temp\TablePDF2.pdf");

// Set page size and orientation.
foreach (Section s in src.Sections)
{
    s.PageSetup.PaperSize = Aspose.Words.PaperSize.A4;
    s.PageSetup.Orientation = Orientation.Portrait;
}

NodeCollection tables = src.GetChildNodes(NodeType.Table, true);
foreach (Table t in tables)
{
    // Auto fit the table to window.
    t.AutoFit(AutoFitBehavior.AutoFitToWindow);

    foreach (Row r in t.Rows)
    {
        // Specify row height rule to make it grow vertically if required.
        r.RowFormat.HeightRule = HeightRule.Auto;

        // Reset paddings of cells.
        foreach (Cell c in r.Cells)
            c.CellFormat.SetPaddings(0, 0, 0, 0);
    }
}

dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
dst.Save(@"C:\Temp\out.docx");

out.docx (22.8 KB)

Hi @alexey.noskov,

That helped, however if you see the last page of the document, the text is not proper as original one.

Can you please see if we can find some solution there?

Thanks
Hardik

@HardikS The reason of the problem is a big value of right indent of paragraphs. The value is ok for landscape orientation, but when the orientation is changed and indent value is keep the same, it becomes too big. You can fix this by resetting right indent of paragraphs:

// Reset right indent of paragraphs.
NodeCollection paragraphs = src.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph p in paragraphs)
    p.ParagraphFormat.RightIndent = 0;