Java/PDF RTF/Transparency

Материал из Java эксперт
Перейти к: навигация, поиск

Transparency by PdfContentByte

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfTransparencyGroup;
import com.lowagie.text.pdf.PdfWriter;
public class TransparencyPDF {
    public static void main(String[] args) {
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TransparencyPDF.pdf"));
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            
            PdfGState gs1 = new PdfGState();
            gs1.setFillOpacity(0.5f);
            
            cb.setGState(gs1);
            cb.setColorStroke(Color.black);
            cb.setColorFill(Color.gray);
            cb.rectangle(0, 0, 100, 200);
            cb.fill();
            cb.setLineWidth(2);
            cb.rectangle(0, 0, 200, 200);
            cb.stroke();
            
            PdfGState gs2 = new PdfGState();
            gs2.setFillOpacity(0.1f);
            
            cb.setGState(gs2);
            
            cb.setColorStroke(Color.black);
            cb.setColorFill(Color.gray);
            cb.rectangle(50, 50, 100, 200);
            cb.fill();
            cb.setLineWidth(2);
            cb.rectangle(50, 50, 200, 200);
            cb.stroke();
            
        }
        catch (Exception de) {
            de.printStackTrace();
        }
        document.close();
    }
}