我添加了背景颜色(添加了带填充颜色的矩形)和透明度,但添加矩形后,pdf中的现有文本不可见。
这是代码
public static void drawRectangle(PdfContentByte content, float width, float height)
{
content.SaveState();
PdfGState state = new PdfGState();
state.FillOpacity = 0.5F;
content.SetGState(state);
content.Rectangle(0, 0, width, height);
content.SetColorFill(new BaseColor(255, 255, 0));
content.Rectangle(0, 0, width, height);
content.Fill();
content.RestoreState();
}
以下是我创建内容字节的方法
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(tBox_pdf.Text, FileMode.Create));
doc.Open();
PdfContentByte cb = writer.DirectContent;
drawRectangle(cb, doc.PageSize.Width, doc.PageSize.Height);
doc.Close();
Process.Start(tBox_pdf.Text);