itextsharp处理一个组与非一个组内的元素
stringfilename=DateTime.Now.ToString("yyyyMMddHHmmss").ToString()+".pdf";
floatw=PageSize.A4.Width;
floath=PageSize.A4.Height;
Rectanglerect=newRectangle(0,0,w,h);
Documentdocument=newDocument(rect);
PdfWriterwriter=PdfWriter.GetInstance(document,newFileStream(Server.MapPath(filename),FileMode.Create));
document.Open();
PdfContentBytecontent=writer.DirectContent;
PdfTemplatetemplate=content.CreateTemplate(rect.Width,rect.Height);
PdfGStatestate=null;
template.SaveState();
template.SetColorFill(Color.RED);
template.SetColorStroke(Color.GREEN);
template.SetLineWidth(3);
state=newPdfGState();
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=newPdfGState();
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=newPdfGState();
state.FillOpacity=0.5f;//设置透明度为0.5
template.SetGState(state);
PdfTemplate_template=template.CreateTemplate(rect.Width,rect.Height);
//设置透明为为一个组
PdfTransparencyGroupgroup=newPdfTransparencyGroup();
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();
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。