IacDocument.Merge Method

The Merge method merges the contents of the current document into a specified PDF file.

 

Syntax

Visual Basic .NET:

public Function Merge(document As AmyuniPDFCreator.IacDocument, options As Integer) As Boolean

C#:

public bool Merge(AmyuniPDFCreator.IacDocument document, int options)

C++/CX:

public: bool Merge(AmyuniPDFCreator.IacDocument^ document, int options)

JavaScript:

Boolean Merge(AmyuniPDFCreator.IacDocument document, Number options)

 

Parameters

document

Reference to the second document that should be merged into to the current one.

options

The options parameter is a combination of the following flags:

 

Option

Name

Description

0

No Repeat

The source file merges without repetition.

1

Repeat

Option 1

The contents of the current document are repeated on all pages.

2

Above

The contents of the current document are above the document specified.

4

Layered

Place each merged document on a separate layer. The layer name will be the same as the document title.

64

Repeat Option 2

The first N-1 pages of an N page document will be merged over the right-side document's pages, the Nth (last page of left-side document) will repeat over all remaining pages of right-side document.

 

 

Return Value

True if the Merge succeeds, False otherwise.

 

Remarks

The document to be merged has to opened by the caller before it can be merged. If the document is password protected, the owner password will be needed to merge.

 

If the documents do not have the same number of pages, say doc1 has N1 pages and doc2 has N2 pages where N1 < N2, then the developer can choose to:

 

E.g.: if doc1 contains the company’s letterhead in PDF format as one page, doc2 is a two page invoice in PDF format generated with the accounting package, one can call:

doc1.Merge(doc2, 1)  // repeat the company’s 
 letterhead on all the invoice pages

or

doc1.Merge(doc2, 0)  // to insert the company’s 
 letterhead on the first page only.

 

The two Repeat Options are mutually exclusive. If both are specified, the Repeat Option 1 only will be used.

 

Member of AmyuniPDFCreator.IacDocument.

 

Example

Merging the page contents of two PDF files, stamping a page of a PDF file on another PDF file, or putting a PDF watermark on a PDF file can be done with Amyuni PDF Creator for Windows Store Apps using the method IacDocument.Merge as follows:

 

' Merge options

'  1  Repeat: The contents of the current document are repeated on all pages.

'  2  Above: The contents of the current document are above the document specified.

'  4  Layered: Place each merged document on a separate layer. The layer name will be the same as the document title. 

<Flags()> _

Private Enum MergeOption As Integer

    Repeat = 1

    Above = 2

    Layered = 4

End Enum

 

Private Async Sub MergeSample()

    Dim doc1 As AmyuniPDFCreator.IacDocument = New AmyuniPDFCreator.IacDocument()

    ' Set the license key

    doc1.SetLicenseKey("Amyuni PDF Creator WinRT-UWP Evaluation", "07EFCDAB01000100F280477A62560570D2805290D715A3744214C2755E1A174DABC5DF79946E4D3E88696CBF0724548B26CC8DE76773")

 

    ' Open a test file asyncronously, from one of the folders where 

    ' a Windows Store Application has read access by default

    Dim file As Windows.Storage.StorageFile = Await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Watermark.pdf")

    Dim result As Boolean = Await fPDFCreator.Document.OpenAsync(file, "")

    If result Then

        ' Open the second test file asyncronously

        Dim doc2 As AmyuniPDFCreator.IacDocument = New AmyuniPDFCreator.IacDocument()

        file = Await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("TestFile1.pdf")

        result = Await doc2.OpenAsync(file, "")

        If (result) Then

 

            ' Merging/Stamping/Watermarking the first document over the second one

            result = doc1.Merge(doc2, MergeOption.Above Or MergeOption.Repeat)

            If (result) Then

 

                ' Saving the resulting file asyncronously, into one of the folders where 

                ' a Windows Store Application has write access by default

                Dim tempPath As String = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path

                result = Await doc1.SaveAsync(System.IO.Path.Combine(tempPath, "merge_result.pdf"))

                If (result) Then

                    Await (New Windows.UI.Popups.MessageDialog( _

                        "Merge Succedded! " + Environment.NewLine + System.IO.Path.Combine(tempPath, "merge_result.pdf"), _

                        "Merge Sample")).ShowAsync()

                Else

                    Await (New Windows.UI.Popups.MessageDialog("Merge Failed", "Merge Sample")).ShowAsync()

                End If

            End If

        End If

    End If

End Sub

// Merge options

// 1  Repeat: The contents of the current document are repeated on all pages.

// 2  Above: The contents of the current document are above the document specified.

// 4  Layered: Place each merged document on a separate layer. The layer name will be the same as the document title. 

[Flags]

private enum MergeOption : int 

{ Repeat = 1, Above = 2, Layered = 4 } 

 

private async void MergeSample()

{

    AmyuniPDFCreator.IacDocument doc1 = new AmyuniPDFCreator.IacDocument();

    // Set the license key

    doc1.SetLicenseKey("Amyuni PDF Creator WinRT-UWP Evaluation", "07EFCDAB01000100F280477A62560570D2805290D715A3744214C2755E1A174DABC5DF79946E4D3E88696CBF0724548B26CC8DE76773");

 

    // Open a test file asyncronously, from one of the folders where 

    // a Windows Store Application has read access by default

    string tempPath = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

    bool result = await doc1.OpenAsync(Path.Combine(tempPath, "Watermark.pdf"), "");

    if (result)

    {

        AmyuniPDFCreator.IacDocument doc2 = new AmyuniPDFCreator.IacDocument();

        result = await doc2.OpenAsync(Path.Combine(tempPath, "TestFile1.pdf"), "");

        if (result)

        {

            // Merging/Stamping/Watermarking the first PDF document over the second one

            result = doc1.Merge(doc2, (int)(MergeOption.Repeat | MergeOption.Above));

            if (result)

            {

                // Saving the resulting file asyncronously, into one of the folders where 

                // a Windows Store Application has write access by default

                tempPath = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path;

                result = await doc1.SaveAsync(System.IO.Path.Combine(tempPath, "merge_result.pdf"));

                if (result)

                    await(new Windows.UI.Popups.MessageDialog(

                        "Merge Succedded!" + Environment.NewLine + System.IO.Path.Combine(tempPath, "merge_result.pdf"),

                        "Merge Sample")).ShowAsync();

                else

                    await(new Windows.UI.Popups.MessageDialog("Merge Failed", "Merge Sample")).ShowAsync();

            }

        }

    }

}

// Merge options

// 1  Repeat: The contents of the current document are repeated on all pages.

// 2  Above: The contents of the current document are above the document specified.

// 4  Layered: Place each merged document on a separate layer. The layer name will be the same as the document title. 

#define MergeOptionRepeat  1

#define MergeOptionAbove   2

#define MergeOptionLayered 4

 

AmyuniPDFCreator::IacDocument^ doc1 = ref new AmyuniPDFCreator::IacDocument();

// Set the license key

// doc1->SetLicenseKey( "Amyuni PDF Creator WinRT-UWP Evaluation", "07EFCDAB01000100F280477A62560570D2805290D715A3744214C2755E1A174DABC5DF79946E4D3E88696CBF0724548B26CC8DE76773");

doc1->SetLicenseKey ( strLicenseTo, strActivationCode );

 

// Open a test file asyncronously, from one of the folders where 

// a Windows Store Application has read access by default

String^ tempPath = Windows::ApplicationModel::Package::Current->InstalledLocation->Path;

concurrency::create_task(doc1->OpenAsync(tempPath + "\\Watermark.pdf", "")).

then([=](bool result)

{

    if (result)

    {

        // Open the second test file asyncronously

        AmyuniPDFCreator::IacDocument^ doc2 = ref new AmyuniPDFCreator::IacDocument();

        concurrency::create_task(doc2->OpenAsync(tempPath + "\\TestFile1.pdf", "")).

        then([=](bool result)

        {

            if (result)

            {

                // Merging/Stamping/Watermarking the first document over the second one

                result = doc1->Merge( doc2, MergeOptionRepeat | MergeOptionAbove );

                if (result)

                {

                    // Saving the resulting file asyncronously, into one of the folders where 

                    // a Windows Store Application has write access by default

                    Platform::String^ tempPath = Windows::Storage::ApplicationData::Current->TemporaryFolder->Path;

                    create_task(doc1->SaveAsync(tempPath + "\\merge_result.pdf" ));

                    if (result)

                    {

                        String^ message = ref new String(L"Merge Succedded! \n");

                        message += tempPath;

                        message += "\\merge_result.pdf";

                        Windows::UI::Popups::MessageDialog^ dialog = ref new Windows::UI::Popups::MessageDialog( message, "Merge Sample");

                        create_task(dialog ->ShowAsync());

                    }

                    else

                    {

                        Windows::UI::Popups::MessageDialog^ dialog = ref new Windows::UI::Popups::MessageDialog("Merge Failed", "Merge Sample");

                        create_task(dialog ->ShowAsync());

                    }

                }

            }

        });

    }

});

var doc1 = new AmyuniPDFCreator.IacDocument();

 

// Set the license key

doc1.SetLicenseKey("Amyuni PDF Creator WinRT-UWP Evaluation", "07EFCDAB01000100F280477A62560570D2805290D715A3744214C2755E1A174DABC5DF79946E4D3E88696CBF0724548B26CC8DE76773");

 

// Open a test file asyncronously, from one of the folders where 

// a Windows Store Application has read access by default

var tempPath = Windows.ApplicationModel.Package.current.installedLocation.path;

Windows.Storage.StorageFile.getFileFromPathAsync(tempPath + "\\Watermark.pdf").

then(function (file) {

 

    // Get a stream object for the input file

    file.openAsync(Windows.Storage.FileAccessMode.read).

    then(function (inStream) {

 

        // Open the PDF document from the file stream

        doc1.openAsync(inStream, "").

        then(function (result) {

            if (result) {

 

                // Open the second test file asyncronously

                Windows.Storage.StorageFile.getFileFromPathAsync(tempPath + "\\TestFile1.pdf").

                then(function (file) {

                    file.openAsync(Windows.Storage.FileAccessMode.read).

                    then(function (inStream) {

                        var doc2 = new AmyuniPDFCreator.IacDocument();

                        doc2.openAsync(inStream, "").

                        then(function (result) {

                            if (result) {

                                // Merge options

                                // 1  Repeat: The contents of the current document are repeated on all pages.

                                // 2  Above: The contents of the current document are above the document specified.

                                // 4  Layered: Place each merged document on a separate layer. The layer name will be the same as the document title. 

                                var MergeOptionRepeat = 1

                                var MergeOptionAbove = 2

                                var MergeOptionLayered = 4

 

                                // Merging/Stamping/Watermarking the first document over the second one

                                doc1.merge(doc2, MergeOptionRepeat | MergeOptionAbove);

 

                                // Saving the resulting file asyncronously, into one of the folders where 

                                // a Windows Store Application has write access by default

                                // Not all the overloads of the saveAsync method are available in Javascript

                                var temporaryFolder = Windows.Storage.ApplicationData.current.temporaryFolder;

                                temporaryFolder.getFileAsync("merge_output.pdf").

                                then(function (file) {

                                    file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (outStream) {

                                        doc1.saveAsync(outStream).then(function (result) {

                                            if (result)

                                                (new Windows.UI.Popups.MessageDialog("Merge Succedded! \n" + file.path, "Merge Sample")).showAsync();

                                            else 

                                                (new Windows.UI.Popups.MessageDialog("Merge Failed", "Merge Sample")).showAsync();

                                        });

                                    });

                                }, function (error) {

 

                                    // The output file does not exist, let' s create a new one

                                    temporaryFolder.createFileAsync("merge_output.pdf").

                                    then(function (file) {

                                        file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (outStream) {

                                            doc1.saveAsync(outStream).then(function (result) {

                                                if (result)

                                                    (new Windows.UI.Popups.MessageDialog("Merge Succedded! \n" + file.path, "Merge Sample")).showAsync();

                                                else 

                                                    (new Windows.UI.Popups.MessageDialog("Merge Failed", "Merge Sample")).showAsync();

                                            });

                                        });

                                    });

                                });

                            }

                        });

                    });

                });

            }

        });

    });

});