我想在iTextSharp pdf文档的底部绘制一个垂直渐变:
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();
这会在我想要创建的确切位置创建渐变,但渐变从左(白色)到右(绿色)是水平的.
我希望渐变从顶部(白色)到底部(绿色)垂直.
像这里所做的那样修改坐标(Does iTextsharp support multi color diagonal gradients?)并没有解决问题.我也尝试旋转文档,但这不起作用.
最佳答案 您使用了错误的坐标.在Java中,你需要这样的东西:
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();
}
有关完整的示例代码,请参见GradientTopToBottom.
你看得到差别吗?
>您从左上角(0,document.PageSize.Height)到右下角(document.PageSize.Width,0).这是一个对角线.
>你想从顶部(0,document.PageSize.Height)到底部(0,0),这导致以下结果:gradient_top_to_bottom.pdf