Search in sources :

Example 1 with RectangleReadOnly

use of com.itextpdf.text.RectangleReadOnly 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)

Aggregations

Document (com.itextpdf.text.Document)1 Image (com.itextpdf.text.Image)1 RectangleReadOnly (com.itextpdf.text.RectangleReadOnly)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1