如何在iTextSharp中绘制垂直渐变? [英] How to draw vertical gradient in iTextSharp?

查看:291
本文介绍了如何在iTextSharp中绘制垂直渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iTextSharp pdf文档的底部绘制一个垂直渐变:

  PdfShading着色= PdfShading.SimpleAxial (pdfWriter,0,document.PageSize.Height,document.PageSize.Width,0,BaseColor.WHITE,BaseColor.GREEN); 
PdfShadingPattern pattern = new PdfShadingPattern(shading);
pdfContentByte.SetShadingFill(pattern);
pdfContentByte.Rectangle(0,0,document.PageSize.Width,70);
pdfContentByte.Fill();

这会在我想要创建的确切位置创建一个渐变,但渐变是水平的左(白色)到右(绿色)。



我希望渐变从顶部(白色)到底部(绿色)垂直。



像这里的某些人一样修改坐标(


I am trying to draw a vertical gradient at the bottom of an iTextSharp pdf document:

        PdfShading shading = PdfShading.SimpleAxial(pdfWriter, 0, document.PageSize.Height, document.PageSize.Width, 0, BaseColor.WHITE, BaseColor.GREEN);
        PdfShadingPattern pattern = new PdfShadingPattern(shading);
        pdfContentByte.SetShadingFill(pattern);
        pdfContentByte.Rectangle(0, 0, document.PageSize.Width, 70);
        pdfContentByte.Fill();

This creates a gradient at the exact position I want it to be created, but the gradient is horizontal from left (white) to right (green).

I want the gradient to be vertical from top (white) to bottom (green).

Modifying the coordinates like some one did here (Does iTextsharp support multi color diagonal gradients?) did not solve the problem. I also tried to rotate the document but that didn't work as well.

解决方案

You are using the wrong coordinates. In Java, you'd need something like this:

public void createPdf(String dest) throws IOException, DocumentException {
    Rectangle pageSize = new Rectangle(150, 300);
    Document document = new Document(pageSize);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfShading shading = PdfShading.simpleAxial(writer,
            0, pageSize.getHeight(),
            0, 0,
            BaseColor.WHITE, BaseColor.GREEN);
    PdfShadingPattern pattern = new PdfShadingPattern(shading);
    PdfContentByte canvas = writer.getDirectContent();
    canvas.setShadingFill(pattern);
    canvas.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
    canvas.fill();
    document.close();
}

See GradientTopToBottom for the full sample code.

Do you see the difference?

  • You go from the left-top corner (0, document.PageSize.Height) to the right-bottom corner (document.PageSize.Width, 0). That's a diagonal.
  • You want to go from the top (0, document.PageSize.Height) to the bottom (0, 0) which leads to the following result: gradient_top_to_bottom.pdf

这篇关于如何在iTextSharp中绘制垂直渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆