当前位置: 首页>>代码示例>>Java>>正文


Java PdfGraphics2D类代码示例

本文整理汇总了Java中com.itextpdf.awt.PdfGraphics2D的典型用法代码示例。如果您正苦于以下问题:Java PdfGraphics2D类的具体用法?Java PdfGraphics2D怎么用?Java PdfGraphics2D使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


PdfGraphics2D类属于com.itextpdf.awt包,在下文中一共展示了PdfGraphics2D类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: print

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
public void print(String plot_pdf) {
	try {
		float width = jframe.getSize().width,
				height = jframe.getSize().height;
		Document document = new Document(new Rectangle(width, height));
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(plot_pdf));
		document.open();
		PdfContentByte canvas = writer.getDirectContent();
		PdfTemplate template = canvas.createTemplate(width, height);
		Graphics2D g2d = new PdfGraphics2D(template, width, height);
		jframe.paint(g2d);
		g2d.dispose();
		canvas.addTemplate(template, 0, 0);
		document.close();
	} catch (FileNotFoundException | DocumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
开发者ID:c-zhou,项目名称:polyGembler,代码行数:20,代码来源:JfreeChart.java

示例2: exportPdf

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
private static void exportPdf(OutputStream ostream, Collection<GridElement> entities, FontHandler diagramFont) throws IOException {
	try {
		FontMapper mapper = new PdfFontMapper();

		Rectangle bounds = DrawPanel.getContentBounds(Config.getInstance().getPrintPadding(), entities);
		com.itextpdf.text.Document document = new com.itextpdf.text.Document(new com.itextpdf.text.Rectangle(bounds.getWidth(), bounds.getHeight()));
		PdfWriter writer = PdfWriter.getInstance(document, ostream);
		document.open();

		Graphics2D graphics2d = new PdfGraphics2D(writer.getDirectContent(), bounds.getWidth(), bounds.getHeight(), mapper);

		// We shift the diagram to the upper left corner, so we shift it by (minX,minY) of the contextBounds
		Dimension trans = new Dimension(bounds.getX(), bounds.getY());
		graphics2d.translate(-trans.getWidth(), -trans.getHeight());

		paintEntitiesIntoGraphics2D(graphics2d, entities, diagramFont);
		graphics2d.dispose();
		document.close();
	} catch (com.itextpdf.text.DocumentException e) {
		throw new IOException(e.getMessage());
	}
}
 
开发者ID:umlet,项目名称:umlet,代码行数:23,代码来源:OutputHandler.java

示例3: createMultipagePDF

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
/**
 * Create the multipage PDF report from the internal list of JPanels.
 * @throws DocumentException 
 */
public void createMultipagePDF() throws DocumentException {
	// Document defaults to A4, so specify the current dimensions
	Document doc= new Document(new Rectangle(pageWidth, pageHeight));
	PdfWriter writer= PdfWriter.getInstance(doc, os);
	doc.open();
	PdfContentByte cb= writer.getDirectContent();
	
	// Iterate over tabs
	for (int i= 0; i != tabs.getTabCount(); ++i) {
		JPanel jp= (JPanel) tabs.getComponent(i);
		// Iterate over pages
		for (int currentPage= 0; currentPage < getNumPages(jp); ++currentPage) {
			doc.newPage(); // not needed for page 1, needed for >1

			PdfTemplate template= cb.createTemplate(pageWidth, pageHeight);
			Graphics2D g2d= new PdfGraphics2D(template, pageWidth, pageHeight * (currentPage + 1));
			jp.printAll(g2d);
			g2d.dispose();

			cb.addTemplate(template, 0, 0);
		}
	}
	
	doc.close();
}
 
开发者ID:BaderLab,项目名称:pharmacogenomics,代码行数:30,代码来源:PGXPDFExporter.java

示例4: renderGral

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
private List<Element> renderGral(String raw) throws BadElementException, IOException {
    ChartDescriptor descriptor = new ChartDescriptorParser().parse(raw);

    PdfContentByte cb = writer.get().getDirectContent();
    float width = (float) descriptor.getWidth();
    float height = (float) descriptor.getHeight();

    PdfTemplate template = cb.createTemplate(width, height);
    Graphics2D g2 = new PdfGraphics2D(template, width, height);

    GralRenderer renderer = new GralRenderer();
    renderer.render(g2, descriptor);

    ArrayList<Element> elements = new ArrayList<Element>(1);
    elements.add(new ImgTemplate(template));
    return elements;
}
 
开发者ID:Arnauld,项目名称:cucumber-contrib,代码行数:18,代码来源:GralProcessor.java

示例5: renderGraph

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
@Override
public void renderGraph(JGraph<?> graph, File file) throws PortException {
    // Get graph bounds. If not available, do nothing (probably empty graph)
    Rectangle2D bounds = graph.getGraphBounds();
    if (bounds == null) {
        return;
    }
    Rectangle bound = new Rectangle((float) bounds.getWidth(), (float) bounds.getHeight());

    try (FileOutputStream fos = new FileOutputStream(file)) {
        Document document = new Document(bound);
        // Open file, create PDF document
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        // Set some metadata
        document.addCreator(Version.getAbout());

        // Open document, get graphics
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        boolean onlyShapes = true;
        //The embedded fonts most likely do not contain all necessary glyphs, so using outlines instead
        // onlyShapes makes PDF considerably bigger, but no alternative at the moment
        PdfGraphics2D pdf2d =
            new PdfGraphics2D(cb, (float) bounds.getWidth(), (float) bounds.getHeight(),
                new DefaultFontMapper(), onlyShapes, false, (float) 100.0);

        // Render
        toGraphics(graph, pdf2d);

        // Cleanup
        pdf2d.dispose();
        document.close();
    } catch (DocumentException | IOException e) {
        throw new PortException(e);
    }
}
 
开发者ID:meteoorkip,项目名称:JavaGraph,代码行数:37,代码来源:GraphToPDF.java

示例6: pageStart

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
public void pageStart() {
	this.document.newPage();
	this.template = this.cb.createTemplate(this.bounds.getWidth(), this.bounds.getHeight());
	
	this.graphics = new PdfGraphics2D(this.template, this.bounds.getWidth(), this.bounds.getHeight());
	this.painter.init(this.graphics);
}
 
开发者ID:theokyr,项目名称:TuxGuitar-1.3.1-fork,代码行数:8,代码来源:PDFDocument.java

示例7: save

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
public static final void save(File file, Component c, int width, int height) {
	if (file == null) {
		logger.log(Level.WARNING, "no file selected");
		return;
	}
	if (c == null) {
		logger.log(Level.WARNING, "no component provided");
		return;
	}
	try {
		Document document = new Document(new Rectangle(width, height));
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file.getAbsolutePath()));
		document.addAuthor("UJMP v" + UJMP.UJMPVERSION);
		document.open();
		PdfContentByte cb = writer.getDirectContent();
		PdfTemplate tp = cb.createTemplate(width, height);
		Graphics2D g2 = new PdfGraphics2D(cb, width, height, new DefaultFontMapper());
		if (c instanceof CanRenderGraph) {
			((CanRenderGraph) c).renderGraph(g2);
		} else {
			c.paint(g2);
		}
		g2.dispose();
		cb.addTemplate(tp, 0, 0);
		document.close();
		writer.close();
	} catch (Exception e) {
		logger.log(Level.WARNING, "could not save PDF file", e);
	}
}
 
开发者ID:ujmp,项目名称:universal-java-matrix-package,代码行数:31,代码来源:ExportPDF.java

示例8: createPDFFile

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的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();
}
 
开发者ID:iig-uni-freiburg,项目名称:WOLFGANG,代码行数:44,代码来源:ExportPDFAction.java

示例9: emit

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
@Override
public void emit(SourceCode sourceCode, ITextContext context) {
    String lang = sourceCode.lang();
    String code = sourceCode.content();

    try {
        String trimmed = Strings.unindentBlock(code);

        log.debug("Initializing text grid");
        TeXFormula formula = new TeXFormula(trimmed);
        TeXIcon teXIcon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 14f);
        teXIcon.setInsets(new Insets(1, 1, 1, 1));
        teXIcon.setForeground(foreground);

        PdfWriter pdfWriter = context.getPdfWriter();
        PdfContentByte cb = pdfWriter.getDirectContent();
        float width = (float) teXIcon.getIconWidth();
        float height = (float) teXIcon.getIconHeight();

        PdfTemplate template = cb.createTemplate(width, height);
        Graphics2D g2 = new PdfGraphics2D(template, width, height, new JLaTeXmathFontMapper());

        log.debug("Rendering formula");
        teXIcon.paintIcon(null, g2, 0, 0);
        g2.dispose();

        log.debug("Rendering diagram done");
        ImgTemplate imgTemplate = new ImgTemplate(template);
        scaleToFit(imgTemplate, context.getDocumentArtBox());

        context.append(imgTemplate);

    } catch (Exception e) {
        throw new WrappedRuntimeException(e);
    }

}
 
开发者ID:Arnauld,项目名称:gutenberg,代码行数:38,代码来源:SourceCodeLaTeXExtension.java

示例10: print

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
/**
 * Prints the given {@link Layout} into the given PDF output stream.
 */
public static void print(Layout layout, OutputStream out) {

	Document document = new Document();
	PdfWriter writer = null;
	try {
		writer = PdfWriter.getInstance(document, out);
	} catch (Exception e) {
		handle(warning(Voc.ErrorWhilePrinting));
	}

	document.open();
	PdfContentByte cb = writer.getDirectContent();

	It<Page> pages = it(layout.getPages());
	for (Page page : pages) {
		//create PDF page
		Size2f pageSize = page.getFormat().getSize();
		float width = Units.mmToPx(pageSize.width, 1);
		float height = Units.mmToPx(pageSize.height, 1);
		document.newPage();
		PdfTemplate tp = cb.createTemplate(width, height);
		//fill PDF page
		Graphics2D g2d = new PdfGraphics2D(cb, width, height);
		log(remark("Printing page " + pages.getIndex() + "..."));
		LayoutRenderer.paintToCanvas(layout, pages.getIndex(), new AwtCanvas(g2d, pageSize,
			CanvasFormat.Vector, CanvasDecoration.None, CanvasIntegrity.Perfect));
		//finish page
		g2d.dispose();
		cb.addTemplate(tp, 0, 0);
	}

	document.close();
}
 
开发者ID:Xenoage,项目名称:Zong,代码行数:37,代码来源:PdfPrinter.java

示例11: writeChartToPDF

import com.itextpdf.awt.PdfGraphics2D; //导入依赖的package包/类
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName)
{
    PdfWriter writer = null;
    Document document = new Document();

    try
    {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte pdfContentByte = writer.getDirectContent();
        PdfTemplate pdfTemplateChartHolder = pdfContentByte.createTemplate(50, 50);
        Graphics2D graphics2d = new PdfGraphics2D(pdfTemplateChartHolder, 50, 50);
        Rectangle2D chartRegion = new Rectangle2D.Double(0, 0, 50, 50);
        chart.draw(graphics2d, chartRegion);
        graphics2d.dispose();

        Image chartImage = Image.getInstance(pdfTemplateChartHolder);
        document.add(chartImage);

        PdfPTable table = new PdfPTable(5);
        // the cell object
        // we add a cell with colspan 3

        PdfPCell cellX = new PdfPCell(new Phrase("A"));
        cellX.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
        cellX.setRowspan(6);
        table.addCell(cellX);

        PdfPCell cellA = new PdfPCell(new Phrase("A"));
        cellA.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
        cellA.setColspan(4);
        table.addCell(cellA);

        PdfPCell cellB = new PdfPCell(new Phrase("B"));
        table.addCell(cellB);
        PdfPCell cellC = new PdfPCell(new Phrase("C"));
        table.addCell(cellC);
        PdfPCell cellD = new PdfPCell(new Phrase("D"));
        table.addCell(cellD);
        PdfPCell cellE = new PdfPCell(new Phrase("E"));
        table.addCell(cellE);
        PdfPCell cellF = new PdfPCell(new Phrase("F"));
        table.addCell(cellF);
        PdfPCell cellG = new PdfPCell(new Phrase("G"));
        table.addCell(cellG);
        PdfPCell cellH = new PdfPCell(new Phrase("H"));
        table.addCell(cellH);
        PdfPCell cellI = new PdfPCell(new Phrase("I"));
        table.addCell(cellI);

        PdfPCell cellJ = new PdfPCell(new Phrase("J"));
        cellJ.setColspan(2);
        cellJ.setRowspan(3);
        //instead of
        //  cellJ.setImage(chartImage);
        //the OP now uses
        Chunk chunk = new Chunk(chartImage, 20, -50);
        cellJ.addElement(chunk);
        //presumably with different contents of the other cells at hand
        table.addCell(cellJ);

        PdfPCell cellK = new PdfPCell(new Phrase("K"));
        cellK.setColspan(2);
        table.addCell(cellK);
        PdfPCell cellL = new PdfPCell(new Phrase("L"));
        cellL.setColspan(2);
        table.addCell(cellL);
        PdfPCell cellM = new PdfPCell(new Phrase("M"));
        cellM.setColspan(2);
        table.addCell(cellM);

        document.add(table);

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    document.close();
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:81,代码来源:JFreeChartTest.java


注:本文中的com.itextpdf.awt.PdfGraphics2D类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。