本文整理汇总了Java中com.itextpdf.text.pdf.PdfContentByte.setGState方法的典型用法代码示例。如果您正苦于以下问题:Java PdfContentByte.setGState方法的具体用法?Java PdfContentByte.setGState怎么用?Java PdfContentByte.setGState使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.itextpdf.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.setGState方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSimple
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
@Test
public void testSimple() throws FileNotFoundException, DocumentException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "transparency.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();
content.saveState();
PdfGState state = new PdfGState();
state.setFillOpacity(0.5f);
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.fillStroke();
content.fill();
content.restoreState();
document.close();
}
示例2: createPDFFile
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
private void createPDFFile(String filename) throws FileNotFoundException, DocumentException {
PNEditorComponent editor = getEditor();
PNGraph pnGraph = editor.getGraphComponent().getGraph();
JFrame frm = new JFrame();
PNGraphComponent forPrint = new PNGraphComponent(pnGraph) {
};
mxRectangle size = forPrint.getGraph().getGraphBounds();
double space = 4;
float x = (float) (size.getRectangle().getWidth() + size.getRectangle().getX() + space);
float y = (float) (size.getRectangle().getHeight() + size.getRectangle().getY() + space);
Document document = new Document(new Rectangle(x, y));
PdfWriter writer = null;
writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// set crop of pdf doc = ll=lowerleft; ur=upper right
float llx = (float) size.getX();
float lly = 0;
float urx = x;
float ury = (float) ((float) size.getRectangle().getHeight() + space*4);
com.itextpdf.text.Rectangle crop = new com.itextpdf.text.Rectangle(llx, lly, urx, ury);
writer.setCropBoxSize(crop);
document.open();
PdfContentByte canvas = writer.getDirectContent();
// make pdf-background transparent
PdfGState gState = new PdfGState();
gState.setFillOpacity(0.0f);
canvas.setGState(gState);
forPrint.setGridVisible(false);
PdfGraphics2D g2 = new PdfGraphics2D(canvas, x, y);
frm.getContentPane().add(forPrint);
frm.pack();
forPrint.paint(g2);
g2.dispose();
document.close();
}
示例3: doFancyStuff
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
@Override
protected void doFancyStuff(ActionEvent e) throws Exception {
setUpGui();
fch.addChoosableFileFilter(new FileFilter() {
public String getDescription() {
return "Portable Network Graphics (*.png)";
}
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} else {
return f.getName().toLowerCase().endsWith(".png");
}
}
});
fch.setDialogTitle("Save PNG");
int returnVal = fch.showDialog(getEditor().getGraphComponent(), "save PNG");
if (returnVal == JFileChooser.APPROVE_OPTION) {
String filename = fch.getSelectedFile().getAbsolutePath();
if (!filename.toLowerCase().endsWith(".png"))
filename += ".png";
PNEditorComponent editor = getEditor();
PNGraph pnGraph = editor.getGraphComponent().getGraph();
JFrame frm = new JFrame();
PNGraphComponent forPrint = new PNGraphComponent(pnGraph) {
};
mxRectangle size = forPrint.getGraph().getGraphBounds();
double space = 4;
float x = (float) (size.getRectangle().getWidth() + size.getRectangle().getX() + space);
float y = (float) (size.getRectangle().getHeight() + size.getRectangle().getY() + space);
Document document = new Document(new Rectangle(x, y));
PdfWriter writer = null;
writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// set crop of pdf doc = ll=lowerleft; ur=upper right
float llx = (float) size.getX();
float lly = 0;
float urx = x;
float ury = (float) ((float) size.getRectangle().getHeight() + space);
com.itextpdf.text.Rectangle crop = new com.itextpdf.text.Rectangle(llx, lly, urx, ury);
writer.setCropBoxSize(crop);
document.open();
PdfContentByte canvas = writer.getDirectContent();
// make pdf-background transparent
PdfGState gState = new PdfGState();
gState.setFillOpacity(0.0f);
canvas.setGState(gState);
forPrint.setGridVisible(false);
// PdfGraphics2D g2 = new PdfGraphics2D(canvas, x, y);
BufferedImage b = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB); /* change sizes of course */
Graphics2D g2 = b.createGraphics();
// component.print(g);
frm.getContentPane().add(forPrint);
frm.pack();
forPrint.paint(g2);
ImageIO.write(b,"png",new File(filename));
g2.dispose();
document.close();
}
}
示例4: applyWatermarkOnAllPages
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
public static void applyWatermarkOnAllPages(
PdfReader reader, PdfStamper stamper, String text, BaseColor color, float opacity
) throws DocumentException, IOException {
int numberOfPages = reader.getNumberOfPages();
PdfGState gs = new PdfGState();
gs.setFillOpacity(opacity);
for (int i = 1; i <= numberOfPages; i++) {
Rectangle pageSize = reader.getPageSize(i);
int pageRotation = reader.getPageRotation(i);
boolean isLandscape = false;
if(pageRotation > 0){
isLandscape = true;
}
int pageDiagonalSize = getDiagonalSize(pageSize);
int watermarkAngle = getDiagonalAngle(pageSize, pageDiagonalSize, isLandscape);
LinkedHashSet<Integer> fontSizeSet = new LinkedHashSet<Integer>();
float fontSize = determineFontSize(pageDiagonalSize, a4WatermarkFontSize, fontSizeSet);
fontSize = fontSize - 1;
int cornerPlacement = getCornerPlacementDiagonal(fontSize);
PdfContentByte underContent = stamper.getUnderContent(i);
underContent.setGState(gs);
underContent.setFontAndSize(getWaterkmarkFont(), fontSize);
underContent.setColorFill(color);
underContent.beginText();
underContent.showTextAligned(
Element.ALIGN_BASELINE, text,
cornerPlacement/2,
cornerPlacement/2,
watermarkAngle
);
underContent.endText();
}
}
示例5: testComplex
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
@Test
public void testComplex() throws FileNotFoundException, DocumentException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "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.fillStroke();
content.fill();
content.restoreState();
document.close();
}
示例6: onEndPage
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
// write the headertable
table.setTotalWidth(document.right() - document.left());
table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 50, cb);
// compose the footer
String text = "Page " + writer.getPageNumber() + " of ";
float textSize = helv.getWidthPoint(text, 12);
float textBase = document.bottom() - 20;
cb.beginText();
cb.setFontAndSize(helv, 12);
// for odd pagenumbers, show the footer at the left
if ((writer.getPageNumber() & 1) == 1) {
cb.setTextMatrix(document.left(), textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.left() + textSize, textBase);
}
// for even numbers, show the footer at the right
else {
float adjust = helv.getWidthPoint("0", 12);
cb.setTextMatrix(document.right() - textSize - adjust, textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.right() - adjust, textBase);
}
cb.saveState();
// draw a Rectangle around the page
cb.setColorStroke(BaseColor.ORANGE);
cb.setLineWidth(2);
cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
cb.stroke();
cb.restoreState();
// starting on page 3, a watermark with an Image that is made transparent
if (writer.getPageNumber() >= 3) {
cb.setGState(gstate);
cb.setColorFill(BaseColor.RED);
cb.beginText();
cb.setFontAndSize(helv, 48);
cb.showTextAligned(Element.ALIGN_CENTER, "Watermark Opacity " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
cb.endText();
try {
cb.addImage(headerImage, headerImage.getWidth(), 0, 0, headerImage.getHeight(), 440, 80);
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
cb.restoreState();
}
cb.sanityCheck();
}