我有一个pdf模板,我想添加一个"选项操作"对于那个模板,这应该对我拥有的所有收件人进行,他们已经4000了。
这意味着我应该为每个收件人提供4000个pdf文件。 但它需要很长时间,大约500个文件/ 12分钟。
注意:模板大小为340 KB
我构建了一个Windows服务来完成这项工作,这是完成这项工作的代码:
private static byte[] GeneratePdfFromPdfFile(byte[] file, string landingPage, string code)
{
try
{
using (var ms = new MemoryStream())
{
var reader = new PdfReader(file);
var stamper = new PdfStamper(reader, ms);
string _embeddedURL = "http://" + landingPage + "/Default.aspx?code=" + code + "&m=" + eventCode18;
PdfAction act = new PdfAction(_embeddedURL);
stamper.Writer.SetOpenAction(act);
stamper.Close();
reader.Close();
return ms.ToArray();
}
}
catch (Exception ex)
{
return null;
}
}
public static byte[] GenerateAttachment(AttachmentExtenstion type, string Contents, string FileName, string code, string landingPage, bool zipped, byte[] File = null)
{
byte[] finalVal = null;
try
{
switch (type)
{
case AttachmentExtenstion.PDF:
finalVal = GeneratePdfFromPdfFile(File, landingPage, code);
break;
case AttachmentExtenstion.WordX:
case AttachmentExtenstion.Word:
finalVal = GenerateWordFromDocFile(File, code, landingPage);
break;
case AttachmentExtenstion.HTML:
finalVal = GenerateHtmlFile(Contents, code, landingPage);
break;
}
return zipped ? _getZippedFile(finalVal, FileName) : finalVal;
}
catch (Exception ex)
{
return null;
}
finally
{
GC.Collect();
}
}
正在为每个收件人调用GenerateAttachment方法,因为该操作基于收件人的ID,并且我使用此方法迭代列表以加快流程
Parallel.ForEach(listRecipients.AsEnumerable(),
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 2 },
(item) =>
{
File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + @"Attachments\" + group.Code + @"\" + item.Scenario.CampaignCode + @"\" + item.CMPRCode + "." + extension,
GenerateAttachment(_type, value, item.AttachmentName, item.CMPRCode, item.Scenario.LandingDomain, item.Scenario.AttachmentZip.Value));
});