/// <summary> /// Returns an Image that has been constructed taking in account /// the value of some attributes. /// </summary> /// <param name="attributes">Some attributes</param> /// <returns>an Image</returns> public static Image getInstance(Properties attributes) { string value = attributes.Remove(ElementTags.URL); if (value == null) { throw new Exception("The URL of the image is missing."); } Image image = Image.getInstance(value); int align = Element.ALIGN_LEFT; if ((value = attributes.Remove(ElementTags.ALIGN)) != null) { if (ElementTags.ALIGN_LEFT.ToLower().Equals(value)) { align |= Image.LEFT; } else if (ElementTags.ALIGN_RIGHT.ToLower().Equals(value)) { align |= Image.RIGHT; } else if (ElementTags.ALIGN_MIDDLE.ToLower().Equals(value)) { align |= Image.MIDDLE; } } if ((value = attributes.Remove(ElementTags.UNDERLYING)) != null) { if (bool.Parse(value)) { align |= Image.UNDERLYING; } } if ((value = attributes.Remove(ElementTags.TEXTWRAP)) != null) { if (bool.Parse(value)) { align |= Image.TEXTWRAP; } } image.alignment = align; if ((value = attributes.Remove(ElementTags.ALT)) != null) { image.Alt = value; } string x; string y; if (((x = attributes.Remove(ElementTags.ABSOLUTEX)) != null) && ((y = attributes.Remove(ElementTags.ABSOLUTEY)) != null)) { image.setAbsolutePosition(float.Parse(x), float.Parse(y)); } if ((value = attributes.Remove(ElementTags.PLAINWIDTH)) != null) { image.scaleAbsoluteWidth(float.Parse(value)); } if ((value = attributes.Remove(ElementTags.PLAINHEIGHT)) != null) { image.scaleAbsoluteHeight(float.Parse(value)); } if ((value = attributes.Remove(ElementTags.ROTATION)) != null) { image.setRotation(float.Parse(value)); } if (attributes.Count > 0) { image.MarkupAttributes = attributes; } return(image); }
/// <summary> /// 生成PDF文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { string localFilePath = String.Empty; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "pdf files(*.pdf)|*.pdf"; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { localFilePath = saveFileDialog1.FileName.ToString(); Document document = new Document(PageSize.A4); PdfWriter.getInstance(document, new System.IO.FileStream(localFilePath, System.IO.FileMode.Create)); document.Open(); BaseFont bfHei = BaseFont.createFont(Application.StartupPath + "\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); iTextSharp.text.Font font1 = new iTextSharp.text.Font(bfHei, 16); Paragraph p2 = new Paragraph(new Phrase("多电机测试报表", font1)); p2.Alignment = Element.ALIGN_CENTER; document.Add(p2); iTextSharp.text.Font font2 = new iTextSharp.text.Font(bfHei, 10); Paragraph ptx4 = new Paragraph(new Phrase("1. 电流结果图", font2)); ptx4.Alignment = Element.ALIGN_LEFT; document.Add(ptx4); Paragraph ptx5 = new Paragraph(new Phrase(" ", font2)); document.Add(ptx5); Paragraph ptx6 = new Paragraph(new Phrase("以下图形显示电机电流测试结果,每个图形显示100个数据点,用折线连接,按时间顺序排列。", font2)); ptx6.Alignment = Element.ALIGN_LEFT; document.Add(ptx6); int k2 = 1; for (int i = 1; i <= chart2.Series[0].Points.Count; i = i + 2 + (int)chart2.ChartAreas[0].AxisX.ScaleView.Size) { chart2.ChartAreas[0].AxisX.ScaleView.Position = i; chart2.SaveImage(Application.StartupPath + "\\Chart2TempFile" + i.ToString() + ".jpg", ChartImageFormat.Jpeg); iTextSharp.text.Image image2 = iTextSharp.text.Image.getInstance(Application.StartupPath + "\\Chart2TempFile" + i.ToString() + ".jpg"); image2.scaleAbsoluteHeight(PageSize.A4.Height / 10); image2.scaleAbsoluteWidth(PageSize.A4.Width - 80); document.Add(image2); string name = string.Format("图2.{0} 电流区间图", k2); Paragraph pt = new Paragraph(new Phrase(name, font2)); pt.Alignment = Element.ALIGN_CENTER; document.Add(pt); k2 = k2 + 1; } chart2.ChartAreas[0].AxisX.ScaleView.Position = 1; document.newPage(); Paragraph ptx7 = new Paragraph(new Phrase("1. 转速结果图", font2)); ptx7.Alignment = Element.ALIGN_LEFT; document.Add(ptx7); Paragraph ptx8 = new Paragraph(new Phrase(" ", font2)); document.Add(ptx8); Paragraph ptx9 = new Paragraph(new Phrase("以下图形显示电机转速测试结果,每个图形显示50个数据点,用折线连接,按时间顺序排列。", font2)); ptx9.Alignment = Element.ALIGN_LEFT; document.Add(ptx9); int k3 = 1; for (int i = 1; i <= chart3.Series[0].Points.Count; i = i + 2 + (int)chart3.ChartAreas[0].AxisX.ScaleView.Size) { chart3.ChartAreas[0].AxisX.ScaleView.Position = i; chart3.SaveImage(Application.StartupPath + "\\Chart3TempFile" + i.ToString() + ".jpg", ChartImageFormat.Jpeg); iTextSharp.text.Image image3 = iTextSharp.text.Image.getInstance(Application.StartupPath + "\\Chart3TempFile" + i.ToString() + ".jpg"); image3.scaleAbsoluteHeight(PageSize.A4.Height / 10); image3.scaleAbsoluteWidth(PageSize.A4.Width - 80); document.Add(image3); string name = string.Format("图3.{0} 转速区间图", k3); Paragraph pt = new Paragraph(new Phrase(name, font2)); pt.Alignment = Element.ALIGN_CENTER; document.Add(pt); k3 = k3 + 1; } chart3.ChartAreas[0].AxisX.ScaleView.Position = 1; document.Close(); //删除临时文件 for (int i = 1; i <= chart2.Series[0].Points.Count; i = i + 2 + (int)chart2.ChartAreas[0].AxisX.ScaleView.Size) { System.IO.File.Delete(Application.StartupPath + "\\Chart2TempFile" + i.ToString() + ".jpg"); } for (int i = 1; i <= chart3.Series[0].Points.Count; i = i + 2 + (int)chart3.ChartAreas[0].AxisX.ScaleView.Size) { System.IO.File.Delete(Application.StartupPath + "\\Chart3TempFile" + i.ToString() + ".jpg"); } } }