use of com.lowagie.text.Rectangle in project jaffa-framework by jaffa-projects.
the class FormPrintEngineIText method startPage.
/**
* Any work to start off printing a page of the document
* m_currentPage will contain the page being printed, and
* m_currentTemplatePage will contain the template page number to base this
* new page on.
* @throws FormPrintException Thrown if there is any form processing problems
*/
protected void startPage() throws FormPrintException {
log.debug("startPage: Page=" + getCurrentPage());
Rectangle r = m_templateReader.getPageSize(getCurrentTemplatePage());
log.debug("Page Size : t=" + r.getTop() + ",l=" + r.getLeft() + ",b=" + r.getBottom() + ",r=" + r.getRight() + ", rot=" + r.getRotation());
r = m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage());
log.debug("Page Size w/Rot: t=" + r.getTop() + ",l=" + r.getLeft() + ",b=" + r.getBottom() + ",r=" + r.getRight() + ", rot=" + r.getRotation());
// Get rotation quadrent 0..3
int q = (m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage()).getRotation() % 360) / 90;
float tX = (q == 2 ? r.getTop() : 0) + (q == 3 ? r.getRight() : 0);
float tY = (q == 1 ? r.getTop() : 0) + (q == 2 ? r.getRight() : 0);
float sX = 1f, sY = 1f;
double angle = -r.getRotation() * (Math.PI / 180f);
double transformA = sX * Math.cos(angle);
double transformB = sY * Math.sin(angle);
double transformC = -sX * Math.sin(angle);
double transformD = sY * Math.cos(angle);
double transformE = tX;
double transformF = tY;
m_generatedDoc.setPageSize(m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage()));
// m_generatedDoc.setPageSize(m_templateReader.getPageSize(getCurrentTemplatePage()) );
/**
* try {
* m_generatedDoc.newPage();
* } catch (DocumentException e) {
* log.error("Error Creating New Page - " + e.getMessage() ,e);
* throw new EngineProcessingException("Error Creating New Page - " + e.getMessage());
* }
*/
m_generatedDoc.newPage();
PdfImportedPage page = m_writer.getImportedPage(m_templateReader, getCurrentTemplatePage());
PdfContentByte cb = m_writer.getDirectContent();
// cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
cb.addTemplate(page, (float) transformA, (float) transformB, (float) transformC, (float) transformD, (float) transformE, (float) transformF);
log.debug("Matrix = [A=" + transformA + ", B=" + transformB + ", C=" + transformC + ", D=" + transformD + ", E=" + transformE + ", F=" + transformF + " ]");
}
use of com.lowagie.text.Rectangle in project jaffa-framework by jaffa-projects.
the class PdfHelper method scalePdfPages.
/**
* Scale the pages of the input pdfOutput document to the given pageSize.
* @param pdfOutput The PDF document to rescale, in the form of a ByteArrayOutputStream.
* @param pageSize The new page size to which to scale to PDF document, e.g. "A4".
* @param noEnlarge If true, center pages instead of enlarging them.
* Use noEnlarge if the new page size is larger than the old one
* and the pages should be centered instead of enlarged.
* @param preserveAspectRatio If true, the aspect ratio will be preserved.
* @return The PDF document with its pages scaled to the input pageSize.
*/
public static byte[] scalePdfPages(byte[] pdfOutput, String pageSize, boolean noEnlarge, boolean preserveAspectRatio) throws FormPrintException {
if (pageSize == null || pdfOutput == null) {
return pdfOutput;
}
// Get the dimensions of the given pageSize in PostScript points.
// A PostScript point is a 72th of an inch.
float dimX;
float dimY;
Rectangle rectangle;
try {
rectangle = PageSize.getRectangle(pageSize);
} catch (Exception ex) {
FormPrintException e = new PdfProcessingException("scalePdfPages - Invalid page size = " + pageSize + " ");
log.error(" scalePdfPages - Invalid page size: " + pageSize + ". " + ex.getMessage() + ". ");
throw e;
}
if (rectangle != null) {
dimX = rectangle.getWidth();
dimY = rectangle.getHeight();
} else {
FormPrintException e = new PdfProcessingException("scalePdfPages - Invalid page size: " + pageSize);
log.error(" scalePdfPages - Invalid page size: " + pageSize);
throw e;
}
// Create portrait and landscape rectangles for the given page size.
Rectangle portraitPageSize;
Rectangle landscapePageSize;
if (dimY > dimX) {
portraitPageSize = new Rectangle(dimX, dimY);
landscapePageSize = new Rectangle(dimY, dimX);
} else {
portraitPageSize = new Rectangle(dimY, dimX);
landscapePageSize = new Rectangle(dimX, dimY);
}
// Remove the document rotation before resizing the document.
byte[] output = removeRotation(pdfOutput);
PdfReader currentReader = null;
try {
currentReader = new PdfReader(output);
} catch (IOException ex) {
FormPrintException e = new PdfProcessingException("scalePdfPages - Failed to create a PDF Reader");
log.error(" scalePdfPages - Failed to create a PDF Reader ");
throw e;
}
OutputStream baos = new ByteArrayOutputStream();
Rectangle newSize = new Rectangle(dimX, dimY);
Document document = new Document(newSize, 0, 0, 0, 0);
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, baos);
} catch (DocumentException ex) {
FormPrintException e = new PdfProcessingException("scalePdfPages - Failed to create a PDF Writer");
log.error(" scalePdfPages - Failed to create a PDF Writer ");
throw e;
}
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page;
float offsetX, offsetY;
for (int i = 1; i <= currentReader.getNumberOfPages(); i++) {
Rectangle currentSize = currentReader.getPageSizeWithRotation(i);
if (currentReader.getPageRotation(i) != 0) {
FormPrintException e = new PdfProcessingException("Page Rotation, " + currentReader.getPageRotation(i) + ", must be removed to re-scale the form.");
log.error(" Page Rotation, " + currentReader.getPageRotation(i) + ", must be removed to re-scale the form. ");
throw e;
}
// Reset the page size for each page because there may be a mix of sizes in the document.
float currentWidth = currentSize.getWidth();
float currentHeight = currentSize.getHeight();
if (currentWidth > currentHeight) {
newSize = landscapePageSize;
} else {
newSize = portraitPageSize;
}
document.setPageSize(newSize);
document.newPage();
float factorX = newSize.getWidth() / currentSize.getWidth();
float factorY = newSize.getHeight() / currentSize.getHeight();
// and the pages should be centered instead of enlarged.
if (noEnlarge) {
if (factorX > 1) {
factorX = 1;
}
if (factorY > 1) {
factorY = 1;
}
}
if (preserveAspectRatio) {
factorX = Math.min(factorX, factorY);
factorY = factorX;
}
offsetX = (newSize.getWidth() - (currentSize.getWidth() * factorX)) / 2f;
offsetY = (newSize.getHeight() - (currentSize.getHeight() * factorY)) / 2f;
page = writer.getImportedPage(currentReader, i);
cb.addTemplate(page, factorX, 0, 0, factorY, offsetX, offsetY);
}
document.close();
return ((ByteArrayOutputStream) baos).toByteArray();
}
use of com.lowagie.text.Rectangle in project boxmaker by rahulbot.
the class Renderer method openDoc.
/**
* Create the document to write to (needed before any rendering can happen).
* @param widthMm the width of the document in millimeters
* @param heightMm the height of the document in millimeters
* @param fileName the name of the file to save
* @throws FileNotFoundException
* @throws DocumentException
*/
private void openDoc(float widthMm, float heightMm, String fileName) throws FileNotFoundException, DocumentException {
float docWidth = widthMm * DPI * INCH_PER_MM;
float docHeight = heightMm * DPI * INCH_PER_MM;
// System.out.println("doc = "+docWidth+" x "+docHeight);
doc = new Document(new Rectangle(docWidth, docHeight));
docPdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(filePath));
String appNameVersion = BoxMakerConstants.APP_NAME + " " + BoxMakerConstants.VERSION;
doc.addAuthor(appNameVersion);
doc.open();
doc.add(new Paragraph("Produced by " + BoxMakerConstants.APP_NAME + " " + BoxMakerConstants.VERSION + "\n" + " on " + new Date() + "\n" + BoxMakerConstants.WEBSITE_URL));
}
use of com.lowagie.text.Rectangle in project dhis2-core by dhis2.
the class DefaultPdfDataEntryFormService method addCell_WithCheckBox.
private void addCell_WithCheckBox(PdfPTable table, PdfWriter writer, PdfPCell cell, String strfldName) throws IOException, DocumentException {
float sizeDefault = PdfDataEntryFormUtil.UNITSIZE_DEFAULT;
RadioCheckField checkbox = new RadioCheckField(writer, new Rectangle(sizeDefault, sizeDefault), "Yes", "On");
checkbox.setBorderWidth(1);
checkbox.setBorderColor(Color.BLACK);
PdfFormField checkboxfield = checkbox.getCheckField();
checkboxfield.setFieldName(strfldName + "_" + PdfFieldCell.TPYEDEFINE_NAME + PdfFieldCell.TYPE_CHECKBOX);
setCheckboxAppearance(checkboxfield, writer.getDirectContent(), sizeDefault);
cell.setCellEvent(new PdfFieldCell(checkboxfield, sizeDefault, sizeDefault, PdfFieldCell.TYPE_CHECKBOX, writer));
table.addCell(cell);
}
use of com.lowagie.text.Rectangle in project dhis2-core by dhis2.
the class PdfFieldCell method cellLayout.
@Override
public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) {
try {
PdfContentByte canvasText = canvases[PdfPTable.TEXTCANVAS];
if (type == TYPE_RADIOBUTTON) {
if (parent != null) {
float leftLoc = rect.getLeft();
float rightLoc = rect.getLeft() + RADIOBUTTON_WIDTH;
String text;
String value;
for (int i = 0; i < texts.length; i++) {
text = texts[i];
value = values[i];
Rectangle radioRec = new Rectangle(leftLoc, rect.getTop() - height, rightLoc, rect.getTop());
RadioCheckField rf = new RadioCheckField(writer, radioRec, "RDBtn_" + text, value);
if (value != null && value.equals(checkValue)) {
rf.setChecked(true);
}
rf.setBorderColor(GrayColor.GRAYBLACK);
rf.setBackgroundColor(GrayColor.GRAYWHITE);
rf.setCheckType(RadioCheckField.TYPE_CIRCLE);
parent.addKid(rf.getRadioField());
leftLoc = rightLoc;
rightLoc += width;
ColumnText.showTextAligned(canvasText, Element.ALIGN_LEFT, new Phrase(text), leftLoc + RADIOBUTTON_TEXTOFFSET, height, 0);
leftLoc = rightLoc;
rightLoc += RADIOBUTTON_WIDTH;
}
writer.addAnnotation(parent);
}
} else if (type == TYPE_BUTTON) {
// Add the push button
PushbuttonField button = new PushbuttonField(writer, rect, name);
button.setBackgroundColor(new GrayColor(0.75f));
button.setBorderColor(GrayColor.GRAYBLACK);
button.setBorderWidth(1);
button.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
button.setTextColor(GrayColor.GRAYBLACK);
button.setFontSize(PdfDataEntryFormUtil.UNITSIZE_DEFAULT);
button.setText(text);
button.setLayout(PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT);
button.setScaleIcon(PushbuttonField.SCALE_ICON_ALWAYS);
button.setProportionalIcon(true);
button.setIconHorizontalAdjustment(0);
formField = button.getField();
formField.setAction(PdfAction.javaScript(jsAction, writer));
} else if (type == TYPE_CHECKBOX) {
float extraCheckBoxOffset_Left = 2.0f;
float extraCheckBoxOffset_Top = 1.5f;
formField.setWidget(new Rectangle(rect.getLeft() + OFFSET_LEFT + extraCheckBoxOffset_Left, rect.getTop() - height - OFFSET_TOP - extraCheckBoxOffset_Top, rect.getLeft() + width + OFFSET_LEFT + extraCheckBoxOffset_Left, rect.getTop() - OFFSET_TOP - extraCheckBoxOffset_Top), PdfAnnotation.HIGHLIGHT_NONE);
} else {
if (type == TYPE_TEXT_ORGUNIT) {
formField.setAdditionalActions(PdfName.BL, PdfAction.javaScript("if(event.value == '') app.alert('Please enter org unit identifier');", writer));
}
// TYPE_TEXT_NUMBER and TYPE_CHECKBOX cases included as well
// here
formField.setWidget(new Rectangle(rect.getLeft() + OFFSET_LEFT, rect.getTop() - height - OFFSET_TOP, rect.getLeft() + width + OFFSET_LEFT, rect.getTop() - OFFSET_TOP), PdfAnnotation.HIGHLIGHT_NONE);
}
writer.addAnnotation(formField);
} catch (DocumentException ex) {
throw new RuntimeException(ex.getMessage());
} catch (IOException ex) {
throw new RuntimeException(ex.getMessage());
}
}
Aggregations