D365: 使用Pdfsharp合并多个pdf文件

2022/2/25 6:31:39

本文主要是介绍D365: 使用Pdfsharp合并多个pdf文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Pdfsharp获取路径

https://sourceforge.net/projects/pdfsharp/

D365FO中,可以利用Pdfsharp来合并多个pdf文件

使用场景

D365FO可能有需要将不同的报表、不同的附件,或者报表和附件进行合并,存储在一个pdf文件中,这时我们可以利用Pdfsharp来实现我们的需求,具体代码如下

//Report生成PDF转成Memory stream
public static System.IO.MemoryStream generatePDFStream(str _fileName)
    {
        SalesInvoiceController   controller = new SalesInvoiceController();
        SalesInvoiceContract           contract   = new SalesInvoiceContract();
        SRSPrintDestinationSettings         settings;
        Array                               arrayFiles;
        System.Byte[]                       reportBytes = new System.Byte[0]();
        SRSProxy                            srsProxy;
        Args                                args;
        System.IO.MemoryStream              stream;
        SRSReportRunService srsReportRunService = new SrsReportRunService();
        Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[] parameterValueArray;
        Map reportParametersMap;
        SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();
        ;

        args = new Args();
        args.record(CustInvoiceJournal);
        // Provide all the parameters to a contract
        contract.parmUserName("username");
        controller.parmArgs(args);
        controller.parmReportName(ssrsReportStr(SalesInvoice,Report));

    controller.parmShowDialog(false);
    controller.parmLoadFromSysLastValue(false);
    controller.parmReportContract().parmRdpContract(contract);
    // Provide printer settings
    settings = controller.parmReportContract().parmPrintSettings();
    settings.printMediumType(SRSPrintMediumType::File);
    settings.fileName(_fileName);
    settings.fileFormat(SRSReportFileFormat::PDF);

    // Below is a part of code responsible for rendering the report
    controller.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
    controller.parmReportContract().parmReportExecutionInfo(executionInfo);

    srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());
    srsReportRunService.preRunReport(controller.parmreportcontract());
    reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());
    parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

    srsProxy = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());
    // Actual rendering to byte array
    reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),
            parameterValueArray,
            settings.fileFormat(),
            settings.deviceinfo());

    if (reportBytes)
    {
        // Converting byte array to memory stream
        stream = new System.IO.MemoryStream(reportBytes);
    }

    return stream;
}
//附件转Memory stream
public static System.IO.MemoryStream attachmentToStream(DocuRef _attachRef)
    {
        System.IO.MemoryStream attachmentStream = new System.IO.MemoryStream();

        using (System.IO.Stream docuRefStream = DocumentManagement::getAttachmentStream(_attachRef))
        {
            docuRefStream.CopyTo(attachmentStream);
        }

        return attachmentStream;
    }
//利用Pdfsharp合并memory stream,注意一点,合并后需要先转成字节数据,然后将字节数据转成memory stream, 在将memory stream 转成文件
public static void mergeStream(System.IO.MemoryStream _reportStream, System.IO.MemoryStream _attachmentStream)
    {
        PdfSharp.Pdf.PdfDocument outPutPDFDocument = new PdfSharp.Pdf.PdfDocument();
        PdfSharp.Pdf.PdfDocument inputPDFDocument = new PdfSharp.Pdf.PdfDocument();
        System.IO.MemoryStream   mergedFileStream   = new System.IO.MemoryStream() ;
        PdfSharp.Pdf.PdfPages    pdfPages;
        System.Byte[]            mergeBytes;
        int i, j, pageCount;
        FileName pdfFile;
        InteropPermission permission;
        str errorMessage, fileName ="Test.pdf";
        ;

        try
        {
            permission = new InteropPermission(InteropKind::ClrInterop);
            permission.assert();
            System.IO.MemoryStream attachmentStream = new System.IO.MemoryStream();

            inputPDFDocument = PdfSharp.Pdf.IO.PdfReader::Open(_reportStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode::Import);
            outputPDFDocument.set_Version(inputPDFDocument.get_Version());
            pageCount = inputPDFDocument.get_PageCount();
            pdfPages = inputPDFDocument.get_Pages();

            if (pageCount > 0)
            {
                for (j = 0 ; j < pageCount; j++)
                {
                    outputPDFDocument.AddPage(pdfPages.get_Item(j));
                }
            }

            inputPDFDocument    = PdfSharp.Pdf.IO.PdfReader::Open(_attachmentStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode::Import);
            outputPDFDocument.set_Version(inputPDFDocument.get_Version());
            pageCount           = inputPDFDocument.get_PageCount();
            pdfPages            = inputPDFDocument.get_Pages();
            if (pageCount > 0)
            {
                for (j = 0 ; j < pageCount; j++)
                {
                    outputPDFDocument.AddPage(pdfPages.get_Item(j));
                }
            }
            outputPDFDocument.Save(mergedFileStream, false);
            mergeBytes    = mergedFileStream.ToArray();
            outputPDFDocument.Close();
            CodeAccessPermission::revertAssert();

            mergedFileStream = new System.IO.MemoryStream(mergeBytes);

            DocumentManagement::attachFileToCommon(Common, DocuType::typeFile(), mergedFileStream, fileName, System.Web.MimeMapping::GetMimeMapping(fileName), System.IO.Path::GetFileNameWithoutExtension(fileName));
        }
        catch(Exception::CLRError)
        {
            errorMessage = AifUtil::getClrErrorMessage();
            CodeAccessPermission::revertAssert();
            throw error(errorMessage);
        }
    }

 



这篇关于D365: 使用Pdfsharp合并多个pdf文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程