• 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);
            }
        }
  • 相关阅读:
    别让暑假留下遗憾,让我们一起去黑龙潭耍水祈福吧
    黑龙潭亲子福利:参加亲子活动合影拿好礼
    黑龙潭,北京夏日养生旅游的首选之地
    黑龙潭,一个夏日亲子游的好地方
    黑龙潭,北京真龙的栖身之所?
    密云黑龙潭周末自驾游
    白天,你陪我黑龙潭戏水观瀑;夜晚,我陪你云蒙山数星看月
    北京黑龙潭旅游攻略
    亲爱的,让我们今生约定每年都去一次黑龙潭,好吗?
    成都飞客文化2014新春贺词:感恩有你,共创辉煌
  • 原文地址:https://www.cnblogs.com/dingkui/p/15934026.html
Copyright © 2020-2023  润新知