Search in sources :

Example 16 with Document

use of com.itextpdf.text.Document in project digilib by robcast.

the class PDFStreamWorker method renderPDF.

/**
 * @throws DocumentException
 * @throws InterruptedException
 * @throws ExecutionException
 * @throws IOException
 * @throws ImageOpException
 */
protected OutputStream renderPDF() throws DocumentException, InterruptedException, ExecutionException, IOException, ImageOpException {
    // create document object
    doc = new Document(PageSize.A4, 0, 0, 0, 0);
    PdfWriter docwriter = null;
    long start_time = System.currentTimeMillis();
    docwriter = PdfWriter.getInstance(doc, outstream);
    setPDFProperties(doc);
    doc.open();
    addTitlePage(doc);
    logger.debug("PDF: " + outstream + " doc.open()ed (" + (System.currentTimeMillis() - start_time) + "ms)");
    NumRange pgs = job_info.getPages();
    for (int p : pgs) {
        logger.debug("PDF: adding Image " + p + " to " + outstream);
        // set page number
        job_info.setValue("pn", p);
        // create ImageJobInformation
        ImageJobDescription iji = ImageJobDescription.getInstance(job_info, job_info.getDlConfig());
        addImage(doc, iji);
        logger.debug("PDF: done adding Image " + p + " to " + outstream);
    }
    logger.debug("PDF: done adding all Images to " + outstream);
    doc.close();
    logger.debug("PDF: " + outstream + " doc.close() (" + (System.currentTimeMillis() - start_time) + "ms)");
    docwriter.flush();
    docwriter.close();
    return outstream;
}
Also used : NumRange(digilib.util.NumRange) PdfWriter(com.itextpdf.text.pdf.PdfWriter) ImageJobDescription(digilib.image.ImageJobDescription) Document(com.itextpdf.text.Document)

Example 17 with Document

use of com.itextpdf.text.Document in project saga by timurstrekalov.

the class PdfReporter method writeReportInternal.

@Override
public void writeReportInternal(final File outputFile, final TestRunCoverageStatistics runStats) throws IOException {
    try {
        document = new Document(PageSize.A4.rotate());
        writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
        document.open();
        addMetaData(runStats);
        addContent(runStats);
        document.close();
    } catch (final Exception e) {
        throw new IOException(e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Document(com.itextpdf.text.Document) IOException(java.io.IOException) DocumentException(com.itextpdf.text.DocumentException)

Example 18 with Document

use of com.itextpdf.text.Document in project carina by qaprosoft.

the class AbstractPage method savePageAsPdf.

public String savePageAsPdf(boolean scaled) throws IOException, DocumentException {
    String pdfName = "";
    // Define test screenshot root
    String test = TestNamingService.getTestName();
    File testRootDir = ReportContext.getTestDir();
    File artifactsFolder = ReportContext.getArtifactsFolder();
    String fileID = test.replaceAll("\\W+", "_") + "-" + System.currentTimeMillis();
    pdfName = fileID + ".pdf";
    String fullPdfPath = artifactsFolder.getAbsolutePath() + "/" + pdfName;
    // TODO: test this implementation and change back to capture if necessary
    Image image = Image.getInstance(testRootDir.getAbsolutePath() + "/" + Screenshot.capture(getDriver(), "", true));
    Document document = null;
    if (scaled) {
        document = new Document(PageSize.A4, 10, 10, 10, 10);
        if (image.getHeight() > (document.getPageSize().getHeight() - 20) || image.getScaledWidth() > (document.getPageSize().getWidth() - 20)) {
            image.scaleToFit(document.getPageSize().getWidth() - 20, document.getPageSize().getHeight() - 20);
        }
    } else {
        document = new Document(new RectangleReadOnly(image.getScaledWidth(), image.getScaledHeight()));
    }
    PdfWriter.getInstance(document, new FileOutputStream(fullPdfPath));
    document.open();
    document.add(image);
    document.close();
    return fullPdfPath;
}
Also used : FileOutputStream(java.io.FileOutputStream) RectangleReadOnly(com.itextpdf.text.RectangleReadOnly) Image(com.itextpdf.text.Image) Document(com.itextpdf.text.Document) File(java.io.File)

Example 19 with Document

use of com.itextpdf.text.Document in project mzmine2 by mzmine.

the class ChartExportUtil method writeChartToPDF.

/**
 * This method saves a chart as a PDF with given dimensions
 *
 * @param chart
 * @param width
 * @param height
 * @param fileName is a full path
 */
public static void writeChartToPDF(JFreeChart chart, int width, int height, File fileName) throws Exception {
    PdfWriter writer = null;
    Document document = new Document(new Rectangle(width, height));
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(graphics2d, rectangle2d);
        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        document.close();
    }
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) Rectangle(com.itextpdf.text.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) DefaultFontMapper(com.itextpdf.awt.DefaultFontMapper) Document(com.itextpdf.text.Document) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) SVGGraphics2DIOException(org.apache.batik.svggen.SVGGraphics2DIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EMFGraphics2D(org.freehep.graphicsio.emf.EMFGraphics2D) Graphics2D(java.awt.Graphics2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D)

Example 20 with Document

use of com.itextpdf.text.Document in project bamboobsc by billchen198318.

the class OrganizationReportPdfCommand method createPdf.

private String createPdf(Context context) throws Exception {
    BscReportPropertyUtils.loadData();
    String visionOid = (String) context.get("visionOid");
    VisionVO vision = null;
    BscStructTreeObj treeObj = (BscStructTreeObj) this.getResult(context);
    for (VisionVO visionObj : treeObj.getVisions()) {
        if (visionObj.getOid().equals(visionOid)) {
            vision = visionObj;
        }
    }
    FontFactory.register(BscConstants.PDF_ITEXT_FONT);
    String fileName = UUID.randomUUID().toString() + ".pdf";
    String fileFullPath = Constants.getWorkTmpDir() + "/" + fileName;
    OutputStream os = new FileOutputStream(fileFullPath);
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    document.left(100f);
    document.top(150f);
    PdfWriter writer = PdfWriter.getInstance(document, os);
    document.open();
    PdfPTable table = new PdfPTable(MAX_COLSPAN);
    table.setWidthPercentage(100f);
    PdfPTable signTable = new PdfPTable(1);
    signTable.setWidthPercentage(100f);
    this.createHead(table, vision, context);
    this.createBody(table, vision);
    this.putSignature(signTable, context);
    document.add(table);
    document.add(signTable);
    document.close();
    writer.close();
    os.flush();
    os.close();
    os = null;
    File file = new File(fileFullPath);
    String oid = UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, file, "department-report.pdf");
    file = null;
    return oid;
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) PdfPTable(com.itextpdf.text.pdf.PdfPTable) BscStructTreeObj(com.netsteadfast.greenstep.bsc.model.BscStructTreeObj) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Document(com.itextpdf.text.Document) VisionVO(com.netsteadfast.greenstep.vo.VisionVO) File(java.io.File)

Aggregations

Document (com.itextpdf.text.Document)32 FileOutputStream (java.io.FileOutputStream)25 PdfWriter (com.itextpdf.text.pdf.PdfWriter)22 IOException (java.io.IOException)12 DocumentException (com.itextpdf.text.DocumentException)10 Rectangle (com.itextpdf.text.Rectangle)10 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)10 File (java.io.File)10 Font (com.itextpdf.text.Font)8 Paragraph (com.itextpdf.text.Paragraph)8 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)6 FileNotFoundException (java.io.FileNotFoundException)6 Graphics2D (java.awt.Graphics2D)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)4 Chapter (com.itextpdf.text.Chapter)4 PdfPTable (com.itextpdf.text.pdf.PdfPTable)4 OutputStream (java.io.OutputStream)4 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4