巨大的清单<t>处理和Pdf生成

时间:2018-01-16 21:18:07

标签: c# asp.net pdf task-parallel-library

我有一个pdf文件作为Byte [],我使用iTextSharp来修改文件并在其中嵌入特定的细节。 在列表中我有分钟。 25K对象,我需要生成25K pdf文件。 我使用的是Parallel.ForEach,但总计需要16.40分钟。 我像这样使用ToLookUp方法:

var valuesToLookupWith = Recipients.ToLookup(item => item.ID);

List<int> Ids = Recipients.Select(item => item.ID).ToList();

Partitioner<int> partitioner = Partitioner.Create(Ids, EnumerablePartitionerOptions.NoBuffering);

Parallel.ForEach(partitioner, new ParallelOptions { MaxDegreeOfParallelism = 6 } ,(id) =>
{
    var item = valuesToLookupWith[id].ToList().FirstOrDefault(); 
    item.Attachment = AttachmentEngine.GeneratePdfFromPdfFile((fileAsByteArray,id, "www.xyz.ca"));
    ...
});

我使用了ForEach,也需要约。 &GT; 25分钟。

foreach (int id in Ids)
{
    var item = valuesToLookupWith[id].ToList().FirstOrDefault();
    item.Attachment = AttachmentEngine.GeneratePdfFromPdfFile(fileAsByteArray,id, "www.xyz.ca");
    ...
}

任何建议的方法来加快这个过程吗?

仅供参考我没有在磁盘上写任何内容,所有内容都在内存中以Byte []的形式完成,然后我将值写回Db。 并且所花费的所有时间 - 问题中提到的只是Parallel.ForEach / ForEach语句花费的时间。 Db通话对我来说根本不是问题,我只对Db进行两次调用,一次是从我加载收件人列表,另一次是在将值写回Db时调用

public static byte[] GeneratePdfFromPdfFile(byte[] file, int id, string landingPage)
{
    try
    {
        using (var ms = new MemoryStream())
        {
            //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
            var doc = new iTextSharp.text.Document();

            //Create a writer that's bound to our PDF abstraction and our stream
            var writer = PdfWriter.GetInstance(doc, ms);

            //Open the document for writing
            doc.Open();
            PdfContentByte cb = writer.DirectContent;
            //  doc.NewPage();

            //var srHtml = new StringReader(source);

            ////parse html code to xml 
            //iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
            PdfReader reader = new PdfReader(file);
            for (int pageNumber = 1; pageNumber < reader.NumberOfPages + 1; pageNumber++)
            {
                doc.SetPageSize(reader.GetPageSizeWithRotation(1));
                doc.NewPage();

                //Insert to Destination on the first page
                PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
                int rotation = reader.GetPageRotation(pageNumber);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pageNumber).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }

            // Add a new page to the pdf file
            doc.NewPage();

            // set pdf open action to open the link embedded in the file.
            string _embeddedURL = "http://" + landingPage + "/Default.aspx?code=" + GetCampaignRecipientCode(id) + "&m=" + eventCode18;
            PdfAction act = new PdfAction(_embeddedURL);

            writer.SetOpenAction(act);

            doc.Close();

            return ms.ToArray();
        }
    }
    catch { return null; }
}

收件人类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CampaignLauncherLibrary
{
    public class CampaignRecipientLib
    {
        private int _id;
        private int _crid;
        private string _crcode;
        private int _cmpId;
        private string _cmpStatus;
        private string _email;
        private string _firstName;
        private string _lastName;
        private string _language;
        private string _cmpDefaultlanguage;
        private bool _isdoubleBarrle;
        private DateTime? _scheduled;
        private string _offset;
        private string _emailTo;
        private string _emailFrom;
        private string _emailBody;
        private string _emailSubject;
        private byte[] _emailAttachment;
        private string _emailReplyTo;
        private string _attachmentName;
        private bool _readytobesent;
        private bool _pickupready;
        private TimeSpan _Toffset;
        private int? _cmprIDnextRecipient;
        private string _CampaignGroupCode;
        private bool _Reschedule;
        private List<int> _Campaigns;
        private List<int> _SentCampaigns;
        private bool _restrictToWorkHours;
        private TimeSpan? _whStart;
        private TimeSpan? _whEnd;
        private string _emailName;
        public CampaignRecipientLib()
        {

        }

        public CampaignRecipientLib(CampaignRecipientLib _recipient)
        {

            ID = _recipient.ID;
            CampaignId = _recipient.CampaignId;
            CampaignStatus = _recipient.CampaignStatus;
            CMPRID = _recipient.CMPRID;
            CMPRCode = Guid.NewGuid().ToString("N");
            Email = _recipient.Email;
            FirstName = _recipient.FirstName;
            LastName = _recipient.LastName;
            Language = _recipient.Language;
            DefaultLanguage = _recipient.DefaultLanguage;
            IsdoubleBarrle = _recipient.IsdoubleBarrle;
            Scheduled = _recipient.Scheduled;
            Offset = _recipient.Offset;
            EmailTo = _recipient.EmailTo;
            EmailFrom = _recipient.EmailFrom;
            EmailBody = _recipient.EmailBody;
            EmailSubject = _recipient.EmailSubject;
            EmailAttachment = _recipient.EmailAttachment;
            EmailReplyTo = _recipient.EmailReplyTo;
            AttachmentName = _recipient.AttachmentName;
            ReadyTobeSent = _recipient.ReadyTobeSent;
            PickupReady = _recipient.PickupReady;
            IDnextRecipient = _recipient.IDnextRecipient;
            CampaignGroupCode = _recipient.CampaignGroupCode;
            Reschedule = _recipient.Reschedule;
            Campaigns = _recipient.Campaigns;
            SentCampaigns = _recipient.SentCampaigns;
            EmailName = _recipient.EmailName;
            Toffset = _recipient.Toffset;
        }

        public void AssingRandomCampaign()
        {

            int result = 0;

            List<int> cmp = _Campaigns;
            List<int> sentcmp = _SentCampaigns;

            if (cmp.Where(x => !sentcmp.Distinct().Contains(x)).ToList().Count > 0)
            {
                cmp = cmp.Where(x => !sentcmp.Distinct().Contains(x)).ToList();
                result = cmp.Shuffle().Take(1).ToList()[0];
            }
            else
            {
                int N = 0;
                if (sentcmp.Count == 2) N = 1;
                else if (sentcmp.Count == 3) N = 2;
                else N = sentcmp.Count % 2 == 0 ? 2 : 3;
                List<int> lastN = sentcmp.Skip(Math.Max(0, sentcmp.Count) - N).ToList();
                cmp = cmp.Where(predicate: x => !lastN.Contains(x)).ToList();
                sentcmp = sentcmp.Where(predicate: x => cmp.Contains(x)).ToList();
                List<int> grpOccurrences = sentcmp.GroupBy(i => i).OrderByDescending(item => item.Count()).SelectMany(i => i).Distinct().ToList();
                result = grpOccurrences.Shuffle().PickRandom(1).ToList()[0];
            }

            if (result > 0)
            {
                _SentCampaigns.Add(result);
                CampaignId = result;

            }

        }


        public bool reAdjustScheduleDate()
        {
            try
            {
                Scheduled = Utilities.FixDate(Scheduled.Value, RestrictToWorkHours, Offset, WhStart, WhEnd);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;           
        }


        public TimeSpan Toffset
        {
            get { return _Toffset; }
            set { _Toffset = value; }
        }


        public string EmailName
        {
            get { return _emailName; }
            set { _emailName = value; }
        }
        public int? IDnextRecipient
        {
            get { return _cmprIDnextRecipient; }
            set { _cmprIDnextRecipient = value; }
        }

        public string CampaignGroupCode
        {
            get { return _CampaignGroupCode; }
            set { _CampaignGroupCode = value; }
        }

        public bool RestrictToWorkHours
        {
            get { return _restrictToWorkHours; }
            set { _restrictToWorkHours = value; }
        }

        public TimeSpan? WhStart
        {
            get { return _whStart; }
            set { _whStart = value; }
        }

        public TimeSpan? WhEnd
        {
            get { return _whEnd; }
            set { _whEnd = value; }
        }

        public bool Reschedule
        {
            get { return _Reschedule; }
            set { _Reschedule = value; }
        }

        public List<int> Campaigns
        {
            get { return _Campaigns; }
            set { _Campaigns = value; }
        }

        public List<int> SentCampaigns
        {
            get { return _SentCampaigns; }
            set { _SentCampaigns = value; }
        }

        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        public int CMPRID
        {
            get { return _crid; }
            set { _crid = value; }
        }

        public string CMPRCode
        {
            get { return _crcode; }
            set { _crcode = value; }
        }

        public int CampaignId
        {
            get { return _cmpId; }
            set { _cmpId = value; }
        }

        public string CampaignStatus
        {
            get { return _cmpStatus; }
            set { _cmpStatus = value; }
        }

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        public string Language
        {
            get { return _language; }
            set { _language = value; }
        }

        public string DefaultLanguage
        {
            get { return _cmpDefaultlanguage; }
            set { _cmpDefaultlanguage = value; }
        }

        public bool IsdoubleBarrle
        {
            get { return _isdoubleBarrle; }
            set { _isdoubleBarrle = value; }
        }

        public DateTime? Scheduled
        {
            get { return _scheduled; }
            set { _scheduled = value; }
        }

        public string EmailTo
        {
            get { return _emailTo; }
            set { _emailTo = value; }
        }

        public string Offset
        {
            get { return _offset; }
            set { _offset = value; }
        }

        public string EmailFrom
        {
            get { return _emailFrom; }
            set { _emailFrom = value; }
        }

        public string EmailBody
        {
            get { return _emailBody; }
            set { _emailBody = value; }
        }

        public string EmailSubject
        {
            get { return _emailSubject; }
            set { _emailSubject = value; }
        }

        public byte[] EmailAttachment
        {
            get { return _emailAttachment; }
            set { _emailAttachment = value; }
        }

        public string EmailReplyTo
        {
            get { return _emailReplyTo; }
            set { _emailReplyTo = value; }
        }

        public string AttachmentName
        {
            get { return _attachmentName; }
            set { _attachmentName = value; }
        }

        public bool ReadyTobeSent
        {
            get { return _readytobesent; }
            set { _readytobesent = value; }
        }

        public bool PickupReady
        {
            get { return _pickupready; }
            set { _pickupready = value; }
        }
    }


}

0 个答案:

没有答案
相关问题