import { Callout } from "zudoku/ui/Callout";

# Pdf - Concatenate

Codeunit `5523674` `bdev.Pdf - Concatenate` merges PDF documents into a single one. Two of its overloads work on `Temp Blob` codeunits, the other two on a list of Base64 encoded documents - which is what you want when more than two documents are involved, because the order of the list is the order of the result.

All four overloads can produce several copies of the merged document in one go.

```pascal
    pdfConcatenate: Codeunit "bdev.Pdf - Concatenate";
```

## ConcatenateDocument(Codeunit, Codeunit)

Appends a second document to a document.

```pascal
    procedure ConcatenateDocument(var document: Codeunit "Temp Blob"; document2: Codeunit "Temp Blob")
```

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `document` | Codeunit "Temp Blob" | The base document. It is replaced by the merged result. |
| `document2` | Codeunit "Temp Blob" | The document to append. |

<Callout type="caution" title="Identical documents are merged only once">
Both overloads that take two `Temp Blob` codeunits compare the two documents and drop the second one when it is byte-identical to the first. Appending a document to itself - to print a copy, for example - therefore does nothing. Use the `copies` parameter for that.
</Callout>

### Example

```pascal
    procedure AppendTermsAndConditions(var document: Codeunit "Temp Blob")
    var
        mySetup: Record "My Setup";
        pdfConcatenate: Codeunit "bdev.Pdf - Concatenate";
        termsAndConditions: Codeunit "Temp Blob";
    begin
        mySetup.Get();

        termsAndConditions.FromRecord(mySetup, mySetup.FieldNo("Terms and Conditions"));

        pdfConcatenate.ConcatenateDocument(document, termsAndConditions);
    end;
```

## ConcatenateDocument(Codeunit, Codeunit, Integer, Boolean)

Appends a second document to a document and produces the given number of copies.

```pascal
    procedure ConcatenateDocument(var document: Codeunit "Temp Blob"; document2: Codeunit "Temp Blob"; copies: Integer; grouped: Boolean)
```

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `document` | Codeunit "Temp Blob" | The base document. It is replaced by the merged result. |
| `document2` | Codeunit "Temp Blob" | The document to append. |
| `copies` | Integer | The number of copies of the merged document. `0` means no copy handling at all, the documents are merged once. |
| `grouped` | Boolean | The copy order, see [Copies and copy order](#copies-and-copy-order). |

## ConcatenateDocument(List of [Text])

Merges a list of Base64 encoded PDF documents into one, in the order of the list.

```pascal
    procedure ConcatenateDocument(documents: List of [Text]): Codeunit "Temp Blob"
```

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `documents` | List of [Text] | The Base64 encoded PDF documents. |

**Returns** the merged document as a `Temp Blob` codeunit.

Unlike the overloads above, the list is merged as it is - identical entries are kept and appear in the result as often as they are listed.

### Example

```pascal
    procedure MergeDocuments(var document: Codeunit "Temp Blob"; var attachment: Record "My Attachment"): Codeunit "Temp Blob"
    var
        convertBase64: Codeunit "Base64 Convert";
        pdfConcatenate: Codeunit "bdev.Pdf - Concatenate";
        documents: List of [Text];
        stream: InStream;
    begin
        documents.Add(convertBase64.ToBase64(document.CreateInStream()));

        if (attachment.FindSet(false)) then
            repeat
                attachment.CalcFields(Content);
                attachment.Content.CreateInStream(stream);
                documents.Add(convertBase64.ToBase64(stream));
            until attachment.Next() = 0;

        exit(pdfConcatenate.ConcatenateDocument(documents));
    end;
```

## ConcatenateDocument(List of [Text], Integer, Boolean)

Merges a list of Base64 encoded PDF documents into one and produces the given number of copies.

```pascal
    procedure ConcatenateDocument(documents: List of [Text]; copies: Integer; grouped: Boolean): Codeunit "Temp Blob"
```

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `documents` | List of [Text] | The Base64 encoded PDF documents. |
| `copies` | Integer | The number of copies of the merged document. `0` means no copy handling at all. |
| `grouped` | Boolean | The copy order, see [Copies and copy order](#copies-and-copy-order). |

**Returns** the merged document as a `Temp Blob` codeunit.

## Copies and copy order

`copies` is the number of copies of the **merged** document, produced by the PDF service rather than by the printer. `grouped` decides in which order they end up in the result. For a merge of the documents `A` and `B` with three copies:

| `grouped` | Copy order | Result |
| --- | --- | --- |
| `true` | Grouped | `A A A B B B` |
| `false` | Collated | `A B A B A B` |

Collated is what a user expects when the result is stapled as a set - each copy is a complete document. Grouped is what you want when the copies are separated by document afterwards.

Collated is the default of the PDF service: the copy order is only sent when `grouped` is `true`. A `copies` value of `0` suppresses the copy handling altogether and leaves the merge as it is.

## See also

- [365 business PDF - Overview](readme.mdx)
- [Pdf - Stationery](pdf-stationery.mdx)
- [Extensibility Events](extensibility-events.mdx)
- [Documentation - Concatenate](../../en-us/365-business-pdf/concatenate.mdx)
