string filename = DateTime.Now.ToString("yyyyMMddHHmmss").ToString() + ".pdf";
float w = PageSize.A4.Width;
float h = PageSize.A4.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
Document document = new Document(rect);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(filename), FileMode.Create));
document.Open();
PdfContentByte content = writer.DirectContent;
PdfTemplate template = content.CreateTemplate(rect.Width, rect.Height);
PdfGState state = null;
template.SaveState();
template.SetColorFill(Color.RED);
template.SetColorStroke(Color.GREEN);
template.SetLineWidth(3);
state = new PdfGState();
state.FillOpacity = 0.5f;
state.AlphaIsShape = false;
template.SetGState(state);
template.Rectangle(100, 100, 100, 100);
template.FillStroke();
template.RestoreState();
template.SaveState();
template.SetColorFill(Color.RED);
template.SetColorStroke(Color.BLUE);
template.SetLineWidth(3);
state = new PdfGState();
state.FillOpacity = 0.5f;
state.AlphaIsShape = false;
template.SetGState(state);
template.Rectangle(150, 150, 100, 100);
template.FillStroke();
template.RestoreState();
template.SaveState();
//处理同一组内的元素,设置组的透明度为0.5,这样组内元素重叠的时候是后画的图形覆盖先画的图形,不会应为设置了透明度,让元素重叠的部分颜色加深或者相互影响
state = new PdfGState();
state.FillOpacity = 0.5f;//设置透明度为0.5
template.SetGState(state);
PdfTemplate _template = template.CreateTemplate(rect.Width, rect.Height);
//设置透明为为一个组
PdfTransparencyGroup group = new PdfTransparencyGroup();
group.Isolated = false;//标示是否独立
_template.Group = group;
_template.SaveState();
_template.SetColorFill(Color.RED);
_template.SetColorStroke(Color.GREEN);
_template.SetLineWidth(3);
_template.Rectangle(300, 100, 100, 100);
_template.FillStroke();
_template.RestoreState();
_template.SaveState();
_template.SetColorFill(Color.RED);
_template.SetColorStroke(Color.BLUE);
_template.SetLineWidth(3);
_template.Rectangle(350, 150, 100, 100);
_template.FillStroke();
_template.RestoreState();
template.AddTemplate(_template, 0, 0);
template.RestoreState();
content.AddTemplate(template, 1, 0, 0, -1, 0, rect.Height);
document.Close();
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。