We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy Tables from One Pdf To Another Pdf

I am working on an update to our app that merges customer data and a Word document to produce a .pdf file. The update I’m working on nvolves then overlaying another pdf file with our existing pdf file.

The overlay process we have right now is working (except for another issue which I have created another thread for) but no tables exist in the overlayed final pdf file.

The code we did have was code to copy Text and Images to the new document. I found that we need to add a section to copy Tables to the new document (I’m assuming). I started doing that but I am not sure where to go after using the TableAbsorber to Visit() the overlay page. You can see my code below, and my comment “don’t know what to do here” for copying the tables on basePage/Pdf to overlayPage/Pdf.

Any help would be greatly appreciated!

Document basePDFdoc = new Document(basePDF.FullName);
Document overlayPDFdoc = new Document(overlayPDF.FullName);

foreach(OverlayPageMapping mapping in overlayPageMappings)
{
    Page basePage = basePDFdoc.Pages[mapping.basePageNumber];
    Page overlayPage = overlayPDFdoc.Pages[mapping.overlayPageNumber];

    // Copy text to new document
    Aspose.Pdf.Text.TextFragmentAbsorber tfAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber();
    tfAbsorber.Visit(overlayPage);
            
    foreach (Aspose.Pdf.Text.TextFragment textFragment in tfAbsorber.TextFragments)
    {
        AsposeAddText(basePage, textFragment);
    }

    tfAbsorber = null;

    // Copy images to new document
    ImagePlacementAbsorber imageAbsorber = new ImagePlacementAbsorber();

    imageAbsorber.Visit(overlayPage);

    foreach (ImagePlacement image in imageAbsorber.ImagePlacements)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms);
            basePage.AddImage(ms, image.Rectangle);
        }
    }

    // Copy tables to new document
    Aspose.Pdf.Text.TableAbsorber tableAbsorber = new Aspose.Pdf.Text.TableAbsorber();
    tableAbsorber.Visit(overlayPage);

    foreach (Aspose.Pdf.Text.AbsorbedTable absorbedTable in tableAbsorber.TableList)
    {
        // don't know what to do here
    }

    imageAbsorber = null;
}

basePDFdoc.Save(outputFile.FullName);

@Robert343

TableAbsorber Class allows you to replace, remove and manipulate existing tables in PDF document. You may further check Manipulate tables in existing PDF articles in our API documentation where it is explained how to use mentioned class. In case you still face any issue or need further assistance, please feel free to let us know.

Thank you for your prompt response!

I did see that “Manipulate tables in existing PDF” documentation before I wrote my thread but it did not help me in transferring the tables from my “overlayPage” to the “basePage” in my code.

Referencing my code above, in the “absorbedTable” objects in the “tableAbsorber.TableList” collection, there’s only the following properties: PageNum, Rectange, and RowList.

I don’t see how these properties are enough to fully describe a Table, including its background color, in order for the Table to be copied to the “basePage” object in my code.

We are currently already copying the Text and Images from “overlayPage” to “basePage” but I’m just not sure how we copy the Tables.

All relevant code is included above, although I’ve included the following “AsposeAddText()” method for copying Text, just for reference, but it’s not directly related to copying of Tables (it’s just called in the above code).

private static void AsposeAddText(Page page, Aspose.Pdf.Text.TextFragment textFragment)
{
    Aspose.Pdf.Text.TextFragment local = new Aspose.Pdf.Text.TextFragment();
    local.Position = textFragment.Position;

    // Recalcualte a new position since page size differs the originl PDF
    local.Position.XIndent = textFragment.Position.XIndent;
    local.Position.YIndent = textFragment.Position.YIndent;

    // Copy the text style
    local.Text = textFragment.Text;
    local.TextState.Font = textFragment.TextState.Font;
    local.TextState.FontSize = textFragment.TextState.FontSize;

    local.TextState.FormattingOptions = textFragment.TextState.FormattingOptions;
    local.TextState.ForegroundColor = textFragment.TextState.ForegroundColor;

    // Add the customized text fragment
    Aspose.Pdf.Text.TextBuilder textBuilder = new Aspose.Pdf.Text.TextBuilder(page);
    textBuilder.AppendText(local);
}

Thanks very much for your help!

@Robert343

Thanks for elaborating further.

Would you kindly share your sample source PDF document with us. We will test the scenario in our environment and address it accordingly.

I have attached three files, which should describe our issue.

The first is “NoTables-BasePdf” and is a simple base .pdf file, with basically an address to a person.

The second is “NoTables-OverlayPdf” and is our “overlay” .pdf file. This file is combined with the “BasePdf.”

The third is “NoTables-CombinedPdfs” and is the result of copying the contents of “OverlayPdf” and putting them into “BasePdf”, using the code I have provided in my two replies above.

As you can see, the “CombinedPdfs” does not have the table info from the “OverlayPdf” file. What I am trying to do is finish the code I originally provided, specifically the section where I create a TableAbsorber and have the comment “don’t know what to do here.” I only assume that a TableAbsorber is required; perhaps the solution lies elsewhere.

Thanks so much for your prompt attention in this; it’s something our client is waiting for.

NoTables-BasePdf.pdf (49.5 KB)
NoTables-CombinedPdfs.pdf (151.2 KB)
NoTables-OverlayPdf.PDF (50.9 KB)

@Robert343

We are looking into the scenario and will get back to you shortly.

Hello,

Just checking back if there has been any update on this.

Thank you!

Adam

@Robert343

Thanks for your patience.

We have tested the scenario in our environment using Aspose.PDF for .NET 19.8 and used your files to copy table data from one PDF to another with all formatting but we could not get much success. The requirement needs further enhancements in the API and we will be able to comment more about its feasibility after detailed investigation.

For the purpose, we have logged this scenario under the ticket ID PDFNET-46796 in our issue tracking system. We will further investigate the feasibility of your requirement and share our feedback as soon as ticket is resolved. Please be patient and spare us little time.

We are sorry for the inconvenience.

Thanks for the update. I appreciate you guys looking into it!