如何使用批处理实现向itextsharp pdf添加多个图像

时间:2019-05-23 09:36:41

标签: c# itext

我必须生成成千上万张图像,并使用批处理将其添加到itextsharp PDF中。我尝试使用任务并行库。请建议是否有更好的方法。

这是下面的代码:

int qty = 100000;

double[] array = new double[qty];

for (int i = 0; i < qty; i++) {
 array[i] = i;
}

string Number = string.Empty;
PdfPTable pdfTable = new PdfPTable(4);

Parallel.For(0, qty, i => {
  string Number = "ABC" + array[i];
  var url = "http://chart.apis.google.com/chart?cht=qr&chs=" + 100 + "x" + 100 + "&chl=" + Number;

  WebRequest request = WebRequest.Create(url);
  WebResponse response =
   default (WebResponse);
  response = request.GetResponse();

  using(BinaryReader reader = new BinaryReader(response.GetResponseStream())) {
   PdfPCell cell = new PdfPCell();
   bytes = reader.ReadBytes((int) response.ContentLength);
   imgBarCode = iTextSharp.text.Image.GetInstance(bytes); //, System.Drawing.Imaging.ImageFormat.Png);
   imgBarCode.ScaleAbsolute(60 f, 60 f);
   cell.AddElement(imgBarCode);
   Paragraph paragraph = new Paragraph(QRNumber);
   cell.AddElement(paragraph);
   cell.Padding = 1;
   pdfTable.AddCell(cell);
  }

 }):

 PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path + "/" + filename + "_" + ".pdf", FileMode.Create));
writer.PdfVersion = PdfWriter.VERSION_1_6;
writer.CompressionLevel = PdfStream.BEST_COMPRESSION;
writer.SetFullCompression();

doc.Open();
pdfTable.CompleteRow();
doc.Add(pdfTable ); //adding table to pdf     
doc.Close();

0 个答案:

没有答案