0

itextpdf でシェーディングを使用しようとしています。四角形 (ここでは位置という名前) をグレーから透明に変更します。これは私が書いたコードですが、動作しません: new BaseColor(255,255,255,0) は BaseColor.WHITE として動作するようです。何か案が ?

//Create a shading object with cell-specific coords
        PdfShading shading = PdfShading.simpleAxial(w,
                position.getLeft(),
                position.getBottom(),
                position.getRight(),
                position.getTop(),
                BaseColor.GRAY,
                new BaseColor(255,255,255,0));
4

1 に答える 1

0

PDF シェーディング自体は、不透明な色のシェーディングを作成します。

透明なシェーディングが必要な場合は、マスクを作成できます。マスク テンプレートでは、たとえば白から黒へのシェーディングを使用します。マスクとして使用すると、不透明から透明へのシェーディング効果が作成されます。マスクをアクティブにして、灰色の長方形を描きます。この効果は、不透明なグレーから透明なグレーへのシェーディングです。


iText を使用して、この回答で同様のことを示しました。そこでのイメージ

不透明な赤からやや透明な赤までの放射状のシェーディングで、次のコードで作成されています。

@Test
public void testComplex() throws FileNotFoundException, DocumentException
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("target/test-outputs/transparencyComplex.pdf"));
    writer.setCompressionLevel(0);
    document.open();
    PdfContentByte content = writer.getDirectContent();

    content.setRGBColorStroke(0, 255, 0);
    for (int y = 0; y <= 400; y+= 10)
    {
        content.moveTo(0, y);
        content.lineTo(500, y);
    }
    for (int x = 0; x <= 500; x+= 10)
    {
        content.moveTo(x, 0);
        content.lineTo(x, 400);
    }
    content.stroke();

    PdfTemplate template = content.createTemplate(500, 400);
    PdfTransparencyGroup group = new PdfTransparencyGroup();
    group.put(PdfName.CS, PdfName.DEVICEGRAY);
    group.setIsolated(false);
    group.setKnockout(false);
    template.setGroup(group);
    PdfShading radial = PdfShading.simpleRadial(writer, 262, 186, 10, 262, 186, 190, BaseColor.WHITE, BaseColor.BLACK, true, true);
    template.paintShading(radial);

    PdfDictionary mask = new PdfDictionary();
    mask.put(PdfName.TYPE, PdfName.MASK);
    mask.put(PdfName.S, new PdfName("Luminosity"));
    mask.put(new PdfName("G"), template.getIndirectReference());

    content.saveState();
    PdfGState state = new PdfGState();
    state.put(PdfName.SMASK, mask);
    content.setGState(state);
    content.setRGBColorFill(255, 0, 0);
    content.moveTo(162, 86);
    content.lineTo(162, 286);
    content.lineTo(362, 286);
    content.lineTo(362, 86);
    content.closePath();
    content.fill();

    content.restoreState();

    document.close();
}

( TestTransparency.java )

シェーディングも同様の方法で実装する必要があります。

于 2016-02-10T13:51:59.503 に答える