Search in sources :

Example 1 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project gephi by gephi.

the class EdgeLabelRenderer method renderPDF.

public void renderPDF(PDFTarget target, String label, float x, float y, Color color, float outlineSize, Color outlineColor) {
    PdfContentByte cb = target.getContentByte();
    cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
    BaseFont bf = target.getBaseFont(font);
    float textHeight = getTextHeight(bf, font.getSize(), label);
    if (outlineSize > 0) {
        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
        cb.setRGBColorStroke(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue());
        cb.setLineWidth(outlineSize);
        cb.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
        cb.setLineCap(PdfContentByte.LINE_CAP_ROUND);
        if (outlineColor.getAlpha() < 255) {
            cb.saveState();
            float alpha = outlineColor.getAlpha() / 255f;
            PdfGState gState = new PdfGState();
            gState.setStrokeOpacity(alpha);
            cb.setGState(gState);
        }
        cb.beginText();
        cb.setFontAndSize(bf, font.getSize());
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
        cb.endText();
        if (outlineColor.getAlpha() < 255) {
            cb.restoreState();
        }
    }
    cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
    cb.beginText();
    cb.setFontAndSize(bf, font.getSize());
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
    cb.endText();
}
Also used : BaseFont(com.itextpdf.text.pdf.BaseFont) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) PdfGState(com.itextpdf.text.pdf.PdfGState)

Example 2 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project gephi by gephi.

the class NodeRenderer method renderPDF.

public void renderPDF(Item item, PDFTarget target, PreviewProperties properties) {
    Float x = item.getData(NodeItem.X);
    Float y = item.getData(NodeItem.Y);
    Float size = item.getData(NodeItem.SIZE);
    size /= 2f;
    Color color = item.getData(NodeItem.COLOR);
    Color borderColor = ((DependantColor) properties.getValue(PreviewProperty.NODE_BORDER_COLOR)).getColor(color);
    float borderSize = properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH);
    float alpha = properties.getBooleanValue(PreviewProperty.NODE_PER_NODE_OPACITY) ? color.getAlpha() / 255f : properties.getFloatValue(PreviewProperty.NODE_OPACITY) / 100f;
    PdfContentByte cb = target.getContentByte();
    cb.setRGBColorStroke(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue());
    cb.setLineWidth(borderSize);
    cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
    if (alpha < 1f) {
        cb.saveState();
        PdfGState gState = new PdfGState();
        gState.setFillOpacity(alpha);
        gState.setStrokeOpacity(alpha);
        cb.setGState(gState);
    }
    cb.circle(x, -y, size);
    if (borderSize > 0) {
        cb.fillStroke();
    } else {
        cb.fill();
    }
    if (alpha < 1f) {
        cb.restoreState();
    }
}
Also used : DependantColor(org.gephi.preview.types.DependantColor) Color(java.awt.Color) DependantColor(org.gephi.preview.types.DependantColor) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) PdfGState(com.itextpdf.text.pdf.PdfGState)

Example 3 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project gephi by gephi.

the class PDFExporter method execute.

@Override
public boolean execute() {
    Progress.start(progress);
    PreviewController controller = Lookup.getDefault().lookup(PreviewController.class);
    controller.getModel(workspace).getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 1.0);
    controller.refreshPreview(workspace);
    PreviewProperties props = controller.getModel(workspace).getProperties();
    Rectangle size = new Rectangle(pageSize);
    if (landscape) {
        size = new Rectangle(pageSize.rotate());
    }
    Color col = props.getColorValue(PreviewProperty.BACKGROUND_COLOR);
    size.setBackgroundColor(new BaseColor(col.getRed(), col.getGreen(), col.getBlue()));
    Document document = new Document(size);
    PdfWriter pdfWriter = null;
    try {
        pdfWriter = PdfWriter.getInstance(document, stream);
        pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_5);
        pdfWriter.setFullCompression();
    } catch (DocumentException ex) {
        Exceptions.printStackTrace(ex);
    }
    document.open();
    PdfContentByte cb = pdfWriter.getDirectContent();
    cb.saveState();
    props.putValue(PDFTarget.LANDSCAPE, landscape);
    props.putValue(PDFTarget.PAGESIZE, size);
    props.putValue(PDFTarget.MARGIN_TOP, new Float((float) marginTop));
    props.putValue(PDFTarget.MARGIN_LEFT, new Float((float) marginLeft));
    props.putValue(PDFTarget.MARGIN_BOTTOM, new Float((float) marginBottom));
    props.putValue(PDFTarget.MARGIN_RIGHT, new Float((float) marginRight));
    props.putValue(PDFTarget.PDF_CONTENT_BYTE, cb);
    target = (PDFTarget) controller.getRenderTarget(RenderTarget.PDF_TARGET, workspace);
    if (target instanceof LongTask) {
        ((LongTask) target).setProgressTicket(progress);
    }
    try {
        controller.render(target, workspace);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    cb.restoreState();
    document.close();
    Progress.finish(progress);
    props.putValue(PDFTarget.PDF_CONTENT_BYTE, null);
    props.putValue(PDFTarget.PAGESIZE, null);
    return !cancel;
}
Also used : PreviewProperties(org.gephi.preview.api.PreviewProperties) PdfWriter(com.itextpdf.text.pdf.PdfWriter) Color(java.awt.Color) BaseColor(com.itextpdf.text.BaseColor) Rectangle(com.itextpdf.text.Rectangle) Document(com.itextpdf.text.Document) PreviewController(org.gephi.preview.api.PreviewController) DocumentException(com.itextpdf.text.DocumentException) BaseColor(com.itextpdf.text.BaseColor) LongTask(org.gephi.utils.longtask.spi.LongTask) DocumentException(com.itextpdf.text.DocumentException) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte)

Example 4 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project summer-bean by cn-cerc.

the class BarcodeDemo2 method createPdf.

public void createPdf(String filename) throws IOException, DocumentException {
    BaseFont bf = null;
    Font fontChinese = null;
    try {
        // 使用iTextAsian.jar中的字体
        bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 使用Windows系统字体(TrueType)
        // BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF",
        // BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        // 使用资源字体(ClassPath)
        // BaseFont.createFont("/SIMYOU.TTF",
        // BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        fontChinese = new Font(bf, 12, Font.NORMAL);
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // step 1
    Document document = new Document(new Rectangle(340, 842));
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    PdfContentByte cb = writer.getDirectContent();
    document.add(new Paragraph("各类条码生成范例:", fontChinese));
    // EAN 13
    document.add(new Paragraph("Barcode EAN.UCC-13"));
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    document.add(new Paragraph("default:"));
    document.add(codeEAN.createImageWithBarcode(cb, null, null));
    codeEAN.setGuardBars(false);
    document.add(new Paragraph("without guard bars"));
    document.add(codeEAN.createImageWithBarcode(cb, null, null));
    codeEAN.setBaseline(-1f);
    codeEAN.setGuardBars(true);
    document.add(new Paragraph("text above:"));
    document.add(codeEAN.createImageWithBarcode(cb, null, null));
    codeEAN.setBaseline(codeEAN.getSize());
    // UPC A
    document.add(new Paragraph("Barcode UCC-12 (UPC-A)"));
    codeEAN.setCodeType(Barcode.UPCA);
    codeEAN.setCode("785342304749");
    document.add(codeEAN.createImageWithBarcode(cb, null, null));
    // EAN 8
    document.add(new Paragraph("Barcode EAN.UCC-8"));
    codeEAN.setCodeType(Barcode.EAN8);
    codeEAN.setBarHeight(codeEAN.getSize() * 1.5f);
    codeEAN.setCode("34569870");
    document.add(codeEAN.createImageWithBarcode(cb, null, null));
    // UPC E
    document.add(new Paragraph("Barcode UPC-E"));
    codeEAN.setCodeType(Barcode.UPCE);
    codeEAN.setCode("03456781");
    document.add(codeEAN.createImageWithBarcode(cb, null, null));
    codeEAN.setBarHeight(codeEAN.getSize() * 3f);
    // EANSUPP
    document.add(new Paragraph("Bookland"));
    document.add(new Paragraph("ISBN 0-321-30474-8"));
    codeEAN.setCodeType(Barcode.EAN13);
    codeEAN.setCode("9781935182610");
    BarcodeEAN codeSUPP = new BarcodeEAN();
    codeSUPP.setCodeType(Barcode.SUPP5);
    codeSUPP.setCode("55999");
    codeSUPP.setBaseline(-2);
    BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);
    document.add(eanSupp.createImageWithBarcode(cb, null, BaseColor.BLUE));
    // CODE 128
    document.add(new Paragraph("Barcode 128"));
    Barcode128 code128 = new Barcode128();
    code128.setCode("0123456789 hello");
    document.add(code128.createImageWithBarcode(cb, null, null));
    code128.setCode("0123456789\uffffMy Raw Barcode (0 - 9)");
    code128.setCodeType(Barcode.CODE128_RAW);
    document.add(code128.createImageWithBarcode(cb, null, null));
    // Data for the barcode :
    String code402 = "24132399420058289";
    String code90 = "3700000050";
    String code421 = "422356";
    StringBuffer data = new StringBuffer(code402);
    data.append(Barcode128.FNC1);
    data.append(code90);
    data.append(Barcode128.FNC1);
    data.append(code421);
    Barcode128 shipBarCode = new Barcode128();
    shipBarCode.setX(0.75f);
    shipBarCode.setN(1.5f);
    shipBarCode.setSize(10f);
    shipBarCode.setTextAlignment(Element.ALIGN_CENTER);
    shipBarCode.setBaseline(10f);
    shipBarCode.setBarHeight(50f);
    shipBarCode.setCode(data.toString());
    document.add(shipBarCode.createImageWithBarcode(cb, BaseColor.BLACK, BaseColor.BLUE));
    // it is composed of 3 blocks whith AI 01, 3101 and 10
    Barcode128 uccEan128 = new Barcode128();
    uccEan128.setCodeType(Barcode.CODE128_UCC);
    uccEan128.setCode("(01)00000090311314(10)ABC123(15)060916");
    document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE, BaseColor.BLACK));
    uccEan128.setCode("0191234567890121310100035510ABC123");
    document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE, BaseColor.RED));
    uccEan128.setCode("(01)28880123456788");
    document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE, BaseColor.BLACK));
    // INTER25
    document.add(new Paragraph("Barcode Interleaved 2 of 5"));
    BarcodeInter25 code25 = new BarcodeInter25();
    code25.setGenerateChecksum(true);
    code25.setCode("41-1200076041-001");
    document.add(code25.createImageWithBarcode(cb, null, null));
    code25.setCode("411200076041001");
    document.add(code25.createImageWithBarcode(cb, null, null));
    code25.setCode("0611012345678");
    code25.setChecksumText(true);
    document.add(code25.createImageWithBarcode(cb, null, null));
    // POSTNET
    document.add(new Paragraph("Barcode Postnet"));
    BarcodePostnet codePost = new BarcodePostnet();
    document.add(new Paragraph("ZIP"));
    codePost.setCode("01234");
    document.add(codePost.createImageWithBarcode(cb, null, null));
    document.add(new Paragraph("ZIP+4"));
    codePost.setCode("012345678");
    document.add(codePost.createImageWithBarcode(cb, null, null));
    document.add(new Paragraph("ZIP+4 and dp"));
    codePost.setCode("01234567890");
    document.add(codePost.createImageWithBarcode(cb, null, null));
    document.add(new Paragraph("Barcode Planet"));
    BarcodePostnet codePlanet = new BarcodePostnet();
    codePlanet.setCode("01234567890");
    codePlanet.setCodeType(Barcode.PLANET);
    document.add(codePlanet.createImageWithBarcode(cb, null, null));
    // CODE 39
    document.add(new Paragraph("Barcode 3 of 9"));
    Barcode39 code39 = new Barcode39();
    code39.setCode("ITEXT IN ACTION");
    document.add(code39.createImageWithBarcode(cb, null, null));
    document.add(new Paragraph("Barcode 3 of 9 extended"));
    Barcode39 code39ext = new Barcode39();
    code39ext.setCode("iText in Action");
    code39ext.setStartStopText(false);
    code39ext.setExtended(true);
    document.add(code39ext.createImageWithBarcode(cb, null, null));
    // CODABAR
    document.add(new Paragraph("Codabar"));
    BarcodeCodabar codabar = new BarcodeCodabar();
    codabar.setCode("A123A");
    codabar.setStartStopText(true);
    document.add(codabar.createImageWithBarcode(cb, null, null));
    // PDF417
    document.add(new Paragraph("Barcode PDF417"));
    BarcodePDF417 pdf417 = new BarcodePDF417();
    String text = "Call me Ishmael. Some years ago--never mind how long " + "precisely --having little or no money in my purse, and nothing " + "particular to interest me on shore, I thought I would sail about " + "a little and see the watery part of the world.";
    pdf417.setText(text);
    Image img = pdf417.getImage();
    img.scalePercent(50, 50 * pdf417.getYHeight());
    document.add(img);
    document.add(new Paragraph("Barcode Datamatrix"));
    BarcodeDatamatrix datamatrix = new BarcodeDatamatrix();
    datamatrix.generate(text);
    img = datamatrix.createImage();
    document.add(img);
    document.add(new Paragraph("Barcode QRCode"));
    BarcodeQRCode qrcode = new BarcodeQRCode("http://www.mimrc.com", 1, 1, null);
    img = qrcode.getImage();
    document.add(img);
    // step 5
    document.close();
}
Also used : BarcodeEANSUPP(com.itextpdf.text.pdf.BarcodeEANSUPP) PdfWriter(com.itextpdf.text.pdf.PdfWriter) BarcodeQRCode(com.itextpdf.text.pdf.BarcodeQRCode) BarcodeInter25(com.itextpdf.text.pdf.BarcodeInter25) BarcodePDF417(com.itextpdf.text.pdf.BarcodePDF417) Rectangle(com.itextpdf.text.Rectangle) IOException(java.io.IOException) Document(com.itextpdf.text.Document) BarcodePostnet(com.itextpdf.text.pdf.BarcodePostnet) Image(com.itextpdf.text.Image) Font(com.itextpdf.text.Font) BaseFont(com.itextpdf.text.pdf.BaseFont) BarcodeCodabar(com.itextpdf.text.pdf.BarcodeCodabar) Paragraph(com.itextpdf.text.Paragraph) Barcode39(com.itextpdf.text.pdf.Barcode39) Barcode128(com.itextpdf.text.pdf.Barcode128) BarcodeEAN(com.itextpdf.text.pdf.BarcodeEAN) DocumentException(com.itextpdf.text.DocumentException) FileOutputStream(java.io.FileOutputStream) BarcodeDatamatrix(com.itextpdf.text.pdf.BarcodeDatamatrix) BaseFont(com.itextpdf.text.pdf.BaseFont) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte)

Example 5 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project polyGembler by c-zhou.

the class Haplotyper method run.

@Override
public void run() {
    // TODO Auto-generated method stub
    myLogger.info("Random seed - " + Constants.seed);
    DataEntry[] de = start_pos == null ? DataCollection.readDataEntry(in_zip, scaff) : DataCollection.readDataEntry(in_zip, scaff, start_pos, end_pos);
    // DataEntry[] de = DataCollection.readDataEntry(in_zip, Constants._ploidy_H);
    final HiddenMarkovModel hmm = vbt ? new HiddenMarkovModelVBT(de, seperation, reverse, trainExp, field, founder_hap_coeff) : // new HiddenMarkovModelRST(de, seperation, reverse, trainExp, field, resampling):
    (hmm_file == null ? new HiddenMarkovModelBWT(de, seperation, reverse, trainExp, field, founder_hap_coeff) : new HiddenMarkovModelBWT(de, seperation, reverse, trainExp, field, founder_hap_coeff, hmm_file));
    if (Constants.plot()) {
        Runnable run = new Runnable() {

            public void run() {
                try {
                    hmmf = new HMMFrame();
                    hmmf.clearTabs();
                    if (Constants.showHMM)
                        hmmp = hmmf.addHMMTab(hmm, hmm.de(), new File(out_prefix));
                } catch (Exception e) {
                    Thread t = Thread.currentThread();
                    t.getUncaughtExceptionHandler().uncaughtException(t, e);
                    e.printStackTrace();
                }
            }
        };
        Thread th = new Thread(run);
        th.run();
    }
    if (Constants.plot()) {
        hmmf.pack();
        hmmf.setVisible(true);
    }
    double ll, ll0 = hmm.loglik();
    for (int i = 0; i < max_iter; i++) {
        hmm.train();
        if (Constants.plot())
            hmmp.update();
        ll = hmm.loglik();
        myLogger.info("----------loglik " + ll);
        if (ll < ll0) {
            // throw new RuntimeException("Fatal error!!!");
            myLogger.info("LOGLIK DECREASED!!!");
            // break;
            ll0 = ll;
            continue;
        }
        if (ll0 != Double.NEGATIVE_INFINITY && Math.abs((ll - ll0) / ll0) < Constants.minImprov)
            break;
        ll0 = ll;
    }
    if (Constants.printPlots()) {
        try {
            float width = hmmf.jframe.getSize().width, height = hmmf.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);
            hmmf.jframe.paint(g2d);
            g2d.dispose();
            canvas.addTemplate(template, 0, 0);
            document.close();
        } catch (FileNotFoundException | DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    String scaff_str = scaff[0] + (start_pos == null || start_pos[0] == Integer.MIN_VALUE ? "" : "_" + start_pos[0]) + (end_pos == null || end_pos[0] == Integer.MAX_VALUE ? "" : "_" + end_pos[0]);
    for (int i = 1; i < scaff.length; i++) {
        if (scaff_str.length() + scaff[i].length() + 32 <= Constants.MAX_FILE_ID_LENGTH)
            scaff_str += Constants.scaff_collapsed_str + scaff[i] + (start_pos == null || start_pos[i] == Integer.MIN_VALUE ? "" : "_" + start_pos[i]) + (end_pos == null || end_pos[i] == Integer.MAX_VALUE ? "" : "_" + end_pos[i]);
        else {
            scaff_str += Constants.scaff_collapsed_str + "etc" + scaff.length;
            break;
        }
    }
    hmm.write(out_prefix, expr_id, scaff_str, resampling);
}
Also used : Rectangle(com.itextpdf.text.Rectangle) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) FileNotFoundException(java.io.FileNotFoundException) Document(com.itextpdf.text.Document) DataEntry(cz1.hmm.data.DataEntry) DocumentException(com.itextpdf.text.DocumentException) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) HiddenMarkovModelBWT(cz1.hmm.model.HiddenMarkovModelBWT) PdfWriter(com.itextpdf.text.pdf.PdfWriter) HiddenMarkovModel(cz1.hmm.model.HiddenMarkovModel) HiddenMarkovModelVBT(cz1.hmm.model.HiddenMarkovModelVBT) HMMFrame(cz1.hmm.swing.HMMFrame) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DocumentException(com.itextpdf.text.DocumentException) PrinterException(java.awt.print.PrinterException) ParseException(org.apache.commons.cli.ParseException) Graphics2D(java.awt.Graphics2D) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)29 IOException (java.io.IOException)13 DocumentException (com.itextpdf.text.DocumentException)10 Document (com.itextpdf.text.Document)9 PdfWriter (com.itextpdf.text.pdf.PdfWriter)9 BaseFont (com.itextpdf.text.pdf.BaseFont)8 PdfReader (com.itextpdf.text.pdf.PdfReader)7 FileOutputStream (java.io.FileOutputStream)7 Font (com.itextpdf.text.Font)6 Rectangle (com.itextpdf.text.Rectangle)6 PdfImportedPage (com.itextpdf.text.pdf.PdfImportedPage)5 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)5 Graphics2D (java.awt.Graphics2D)5 Paragraph (com.itextpdf.text.Paragraph)4 PdfGState (com.itextpdf.text.pdf.PdfGState)4 PdfPTable (com.itextpdf.text.pdf.PdfPTable)4 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)3 Image (com.itextpdf.text.Image)3 Phrase (com.itextpdf.text.Phrase)3 Color (java.awt.Color)3