Aspose PDF Data Merge

Hello,

We have a requirement to merge the content of one pdf into a specific page and location in another pdf. Attached two sample pdfs. In this case if you scroll down to the second page on MainPDF.pdf, the area where the text highlighted in red color will be empty. We have to copy the text from DataToBeCopied.pdf to this location. Could you please suggest what is the best way to achieve this.

Thanks,
BDataToBeCopied.pdf (38.2 KB)
MainPDF.pdf (63.2 KB)

@judiciary,

This is the code sample will get you started:

private void Logic()
{
    Document doc = new Document($"{PartialPath}_input.pdf");

    Document docSource = new Document($"{PartialPath}_source.pdf");

    // Create TextAbsorber object to extract text
    TextAbsorber textAbsorber = new TextAbsorber();

    // Accept the absorber for all the pages
    docSource.Pages.Accept(textAbsorber);

    // Get the extracted text
    string extractedText = textAbsorber.Text;

    // Create TextBuilder in an specific page
    TextBuilder builder = new TextBuilder(doc.Pages[2]);
    
    // Create text paragraph
    TextParagraph paragraph = new TextParagraph();

    // Set subsequent lines indent
    paragraph.SubsequentLinesIndent = 0;

    // Specify the location to add TextParagraph
    paragraph.Rectangle = new Aspose.Pdf.Rectangle(322, 180, 545, 380);

    // Specify word wraping mode
    paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;

    // Create text fragment
    TextFragment fragment = new TextFragment(extractedText);

    fragment.TextState.Font = FontRepository.FindFont("Times New Roman");

    fragment.TextState.FontSize = 12;

    // Add fragment to paragraph
    paragraph.AppendLine(fragment);

    // Add paragraph
    builder.AppendParagraph(paragraph);

    doc.Save($"{PartialPath}_output.pdf");
}

This are the input and output files:
ReplaceContentFromOnePdfIntoAnother_input.pdf (63.2 KB)
ReplaceContentFromOnePdfIntoAnother_output.pdf (142.7 KB)
ReplaceContentFromOnePdfIntoAnother_source.pdf (38.2 KB)

The code in Java:

public void Logic() throws Exception
{
    Document doc = new Document(PartialPath + "_input.pdf");

    Document docSource = new Document(PartialPath + "_source.pdf");

    // Create TextAbsorber object to extract text
    TextAbsorber textAbsorber = new TextAbsorber();

    // Accept the absorber for all the pages
    docSource.getPages().accept(textAbsorber);

    // Get the extracted text
    String extractedText = textAbsorber.getText();

    // Create TextBuilder in an specific page
    TextBuilder builder = new TextBuilder(doc.getPages().get_Item(2));

    // Create text paragraph
    TextParagraph paragraph = new TextParagraph();

    // Set subsequent lines indent
    paragraph.setSubsequentLinesIndent(0);

    // Specify the location to add TextParagraph
    paragraph.setRectangle(new com.aspose.pdf.Rectangle(322, 180, 545, 380));

    // Specify word wraping mode
    paragraph.getFormattingOptions().setWrapMode(TextFormattingOptions.WordWrapMode.ByWords);

    // Create text fragment
    TextFragment fragment = new TextFragment(extractedText);

    fragment.getTextState().setFont(FontRepository.findFont("Times New Roman"));

    fragment.getTextState().setFontSize(12);

    // Add fragment to paragraph
    paragraph.appendLine(fragment);

    // Add paragraph
    builder.appendParagraph(paragraph);

    doc.save(PartialPath + "_output.pdf");
}