ItextSharp光滑的背景阴影

时间:2017-04-20 12:51:44

标签: itext pdf-generation

我正在使用iTextSharp制作pdf,产品所有者决定在背景上投下一个“贴纸”类似的文本框。我在互联网上搜索,无法找到如何实现这种效果的提示。提供的设计如下: enter image description here

有没有更有经验的iText或iTextsharp可以通过建议如何达到这样的效果来帮助我吗? 亲切的问候 卡罗尔

2 个答案:

答案 0 :(得分:2)

使用Joris答案中的提示和链接,可以轻松实现这样的辅助方法来绘制阴影圆角矩形:

void DrawRoundedShadedRectangle(PdfWriter writer, float x, float y, float w, float h, float r, float shade, BaseColor innerColor, BaseColor shadeColor, BaseColor outerColor)
{
    PdfContentByte canvas = writer.DirectContent;

    canvas.SaveState();
    canvas.Rectangle(x - shade, y + r, w + 2 * shade, h - 2*r);
    canvas.Clip();
    canvas.NewPath();
    PdfShading shadingRight = PdfShading.SimpleAxial(writer, x + w, y, x + w + shade, y, shadeColor, outerColor, false, false);
    canvas.PaintShading(shadingRight);
    PdfShading shadingLeft = PdfShading.SimpleAxial(writer, x, y, x - shade, y, shadeColor, outerColor, false, false);
    canvas.PaintShading(shadingLeft);
    canvas.RestoreState();

    canvas.SaveState();
    canvas.Rectangle(x + r, y - shade, w - 2 * r, h + 2 * shade);
    canvas.Clip();
    canvas.NewPath();
    PdfShading shadingTop = PdfShading.SimpleAxial(writer, x, y + h, x, y + h + shade, shadeColor, outerColor, false, false);
    canvas.PaintShading(shadingTop);
    PdfShading shadingBottom = PdfShading.SimpleAxial(writer, x, y, x, y - shade, shadeColor, outerColor, false, false);
    canvas.PaintShading(shadingBottom);
    canvas.RestoreState();

    canvas.SaveState();
    canvas.Rectangle(x + w - r, y + h - r, r + shade, r + shade);
    canvas.Clip();
    canvas.NewPath();
    PdfShading shadingTopRight = PdfShading.SimpleRadial(writer, x + w - r, y + h - r, r, x + w - r, y + h - r, r + shade, shadeColor, outerColor);
    canvas.PaintShading(shadingTopRight);
    canvas.RestoreState();

    canvas.SaveState();
    canvas.Rectangle(x - shade, y + h - r, r + shade, r + shade);
    canvas.Clip();
    canvas.NewPath();
    PdfShading shadingTopLeft = PdfShading.SimpleRadial(writer, x + r, y + h - r, r, x + r, y + h - r, r + shade, shadeColor, outerColor);
    canvas.PaintShading(shadingTopLeft);
    canvas.RestoreState();

    canvas.SaveState();
    canvas.Rectangle(x - shade, y - shade, r + shade, r + shade);
    canvas.Clip();
    canvas.NewPath();
    PdfShading shadingBottomLeft = PdfShading.SimpleRadial(writer, x + r, y + r, r, x + r, y + r, r + shade, shadeColor, outerColor);
    canvas.PaintShading(shadingBottomLeft);
    canvas.RestoreState();

    canvas.SaveState();
    canvas.Rectangle(x + w - r, y - shade, r + shade, r + shade);
    canvas.Clip();
    canvas.NewPath();
    PdfShading shadingBottomRight = PdfShading.SimpleRadial(writer, x + w - r, y + r, r, x + w - r, y + r, r + shade, shadeColor, outerColor);
    canvas.PaintShading(shadingBottomRight);
    canvas.RestoreState();

    canvas.SaveState();
    canvas.SetColorFill(innerColor);
    canvas.SetColorStroke(innerColor);
    canvas.RoundRectangle(x, y, w, h, r);
    canvas.FillStroke();
    canvas.RestoreState();
}

例如,

DrawRoundedShadedRectangle(writer, 100, 500, 200, 100, 10, 20, BaseColor.GREEN, BaseColor.RED, BaseColor.WHITE);

得出这个:

Green-Red-White

这段代码

DrawRoundedShadedRectangle(writer, writer.PageSize.GetLeft(100), 300, writer.PageSize.Width - 200, 100, 10, 10, BaseColor.ORANGE, BaseColor.YELLOW, BaseColor.WHITE);
PdfContentByte canvas = writer.DirectContent;
canvas.SaveState();
canvas.Rectangle(writer.PageSize.Left, writer.PageSize.Bottom, writer.PageSize.Right, 300);
canvas.Clip();
canvas.NewPath();
DrawRoundedShadedRectangle(writer, writer.PageSize.GetLeft(100), 100, writer.PageSize.Width - 200, 200, 10, 10, BaseColor.WHITE, BaseColor.YELLOW, BaseColor.WHITE);
canvas.RestoreState();

绘制这个

Orange/White - Yellow - White

通过调整 r adius,阴影以及所有颜色参数,这应该与提供的设计非常相似。

答案 1 :(得分:1)

这可以通过iText完成。 这是在画布上使用绘图操作来实现效果的问题。 在你的情况下   - 圆角矩形,橙色   - 在其下方的圆角矩形,充满白色