Thursday, April 9, 2020

Microsoft Dynamics AX 2012 Combine Multiple PDF Files into Single PDF File

public static client Filename makeDocumentCombined(Set _files)  // set of Filename strings
{
    #File
    Filename                            ret             = WinAPI::getTempPath() + "singlefilename" + #pdf;
    SetEnumerator                       se              = _files.getEnumerator();
    System.IO.FileStream                stream          = new System.IO.FileStream(ret, System.IO.FileMode::Create);
    iTextSharp.text.Document            document        = new iTextSharp.text.Document();
    iTextSharp.text.pdf.PdfCopy         copy            = new iTextSharp.text.pdf.PdfCopy(document, stream);
    iTextSharp.text.pdf.PdfReader       reader;
    iTextSharp.text.pdf.PdfImportedPage page;
    int                                 numberOfPages;
    int                                 numberOfPage;
    Filename                            file;

    document.Open();

    while (se.moveNext())
    {
        file = se.current();  // Filename

        reader = new iTextSharp.text.pdf.PdfReader(file);

        numberOfPages = reader.get_NumberOfPages();
        for (numberOfPage = 1; numberOfPage <= numberOfPages; numberOfPage++)
        {
            page = copy.GetImportedPage(reader, numberOfPage);
            copy.AddPage(page);
        }

        copy.FreeReader(reader);

        reader.Close();
        reader = null;
    }

    copy.Flush();
    copy.Close();
    copy.Dispose();
    copy = null;  // dereference

    document.Close();
    document.Dispose();
    document = null;  // dereference

    stream.Close();
    stream.Dispose();
    stream = null;  // dereference

    return ret;  // return Filename string of single PDF file
}

Source: https://community.dynamics.com/ax/b/dynamicsaxexperience/posts/microsoft-dynamics-ax-2012-combine-set-of-pdfs-into-single-pdf

No comments:

Post a Comment