com.lowagie.text.Rectangle Java Examples

The following examples show how to use com.lowagie.text.Rectangle. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PdfReader.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Gets the box size. Allowed names are: "crop", "trim", "art", "bleed" and "media".
 * @param index the page number. The first page is 1
 * @param boxName the box name
 * @return the box rectangle or null
 */
public Rectangle getBoxSize(int index, String boxName) {
    PdfDictionary page = pageRefs.getPageNRelease(index);
    PdfArray box = null;
    if (boxName.equals("trim"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.TRIMBOX));
    else if (boxName.equals("art"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.ARTBOX));
    else if (boxName.equals("bleed"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.BLEEDBOX));
    else if (boxName.equals("crop"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.CROPBOX));
    else if (boxName.equals("media"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.MEDIABOX));
    if (box == null)
        return null;
    return getNormalizedRectangle(box);
}
 
Example #2
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void insertTable_ProgramStage( PdfPTable mainTable, PdfWriter writer, ProgramStage programStage )
    throws IOException, DocumentException
{
    Rectangle rectangle = new Rectangle( TEXTBOXWIDTH, PdfDataEntryFormUtil.CONTENT_HEIGHT_DEFAULT );

    // Add Program Stage Sections
    if ( programStage.getProgramStageSections().size() > 0 )
    {
        // Sectioned Ones
        for ( ProgramStageSection section : programStage.getProgramStageSections() )
        {
            insertTable_ProgramStageSections( mainTable, rectangle, writer, section.getDataElements() );
        }
    }
    else
    {
        // Default one
        insertTable_ProgramStageSections( mainTable, rectangle, writer, programStage.getDataElements() );
    }
}
 
Example #3
Source File: LegendIconComponentImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
private void drawPoint(PdfContext context, Rectangle iconRect, Color fillColor, Color strokeColor) {
	float baseWidth = iconRect.getWidth() / 10;
	SymbolInfo symbol = styleInfo.getSymbol();
	if (symbol.getImage() != null) {
		try {
			Image pointImage = Image.getInstance(symbol.getImage().getHref());
			context.drawImage(pointImage, iconRect, iconRect);
		} catch (Exception ex) { // NOSONAR
			log.error("Not able to create image for POINT Symbol", ex);
		}
	} else if (symbol.getRect() != null) {
		context.fillRectangle(iconRect, fillColor);
		context.strokeRectangle(iconRect, strokeColor, baseWidth / 2);
	} else {
		context.fillEllipse(iconRect, fillColor);
		context.strokeEllipse(iconRect, strokeColor, baseWidth / 2);
	}
}
 
Example #4
Source File: PdfAnnotation.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
public static PdfAnnotation createMarkup(PdfWriter writer, Rectangle rect, String contents, int type, float quadPoints[]) {
	PdfAnnotation annot = new PdfAnnotation(writer, rect);
	PdfName name = PdfName.HIGHLIGHT;
	switch (type) {
		case MARKUP_UNDERLINE:
			name = PdfName.UNDERLINE;
			break;
		case MARKUP_STRIKEOUT:
			name = PdfName.STRIKEOUT;
			break;
		case MARKUP_SQUIGGLY:
			name = PdfName.SQUIGGLY;
			break;
	}
	annot.put(PdfName.SUBTYPE, name);
	annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
	PdfArray array = new PdfArray();
	for (float quadPoint : quadPoints) {
		array.add(new PdfNumber(quadPoint));
	}
	annot.put(PdfName.QUADPOINTS, array);
	return annot;
}
 
Example #5
Source File: PdfDocumentWriter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void processLogicalPage( final LogicalPageKey key, final LogicalPageBox logicalPage ) throws DocumentException {

    final float width = (float) StrictGeomUtility.toExternalValue( logicalPage.getPageWidth() );
    final float height = (float) StrictGeomUtility.toExternalValue( logicalPage.getPageHeight() );

    final Rectangle pageSize = new Rectangle( width, height );

    final Document document = getDocument();
    document.setPageSize( pageSize );
    document.setMargins( 0, 0, 0, 0 );

    if ( awaitOpenDocument ) {
      document.open();
      awaitOpenDocument = false;
    }

    final Graphics2D graphics = new PdfGraphics2D( writer.getDirectContent(), width, height, metaData );
    // and now process the box ..
    final PdfLogicalPageDrawable logicalPageDrawable = createLogicalPageDrawable( logicalPage, null );
    logicalPageDrawable.draw( graphics, new Rectangle2D.Double( 0, 0, width, height ) );

    graphics.dispose();

    document.newPage();
  }
 
Example #6
Source File: PdfReader.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Gets the box size. Allowed names are: "crop", "trim", "art", "bleed" and "media".
 *
 * @param index the page number. The first page is 1
 * @param boxName the box name
 * @return the box rectangle or null
 */
public Rectangle getBoxSize(int index, String boxName) {
	PdfDictionary page = pageRefs.getPageNRelease(index);
	PdfArray box = null;
	if (boxName.equals("trim")) {
		box = (PdfArray) getPdfObjectRelease(page.get(PdfName.TRIMBOX));
	} else if (boxName.equals("art")) {
		box = (PdfArray) getPdfObjectRelease(page.get(PdfName.ARTBOX));
	} else if (boxName.equals("bleed")) {
		box = (PdfArray) getPdfObjectRelease(page.get(PdfName.BLEEDBOX));
	} else if (boxName.equals("crop")) {
		box = (PdfArray) getPdfObjectRelease(page.get(PdfName.CROPBOX));
	} else if (boxName.equals("media")) {
		box = (PdfArray) getPdfObjectRelease(page.get(PdfName.MEDIABOX));
	}
	if (box == null) {
		return null;
	}
	return getNormalizedRectangle(box);
}
 
Example #7
Source File: PdfReader.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Gets the box size. Allowed names are: "crop", "trim", "art", "bleed" and "media".
 * @param index the page number. The first page is 1
 * @param boxName the box name
 * @return the box rectangle or null
 */
public Rectangle getBoxSize(int index, String boxName) {
    PdfDictionary page = pageRefs.getPageNRelease(index);
    PdfArray box = null;
    if (boxName.equals("trim"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.TRIMBOX));
    else if (boxName.equals("art"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.ARTBOX));
    else if (boxName.equals("bleed"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.BLEEDBOX));
    else if (boxName.equals("crop"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.CROPBOX));
    else if (boxName.equals("media"))
        box = (PdfArray)getPdfObjectRelease(page.get(PdfName.MEDIABOX));
    if (box == null)
        return null;
    return getNormalizedRectangle(box);
}
 
Example #8
Source File: PdfAppearance.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PdfContentByte getDuplicate() {
	PdfAppearance tpl = new PdfAppearance();
	tpl.writer = writer;
	tpl.pdf = pdf;
	tpl.thisReference = thisReference;
	tpl.pageResources = pageResources;
	tpl.bBox = new Rectangle(bBox);
	tpl.group = group;
	tpl.layer = layer;
	if (matrix != null) {
		tpl.matrix = new PdfArray(matrix);
	}
	tpl.separator = separator;
	return tpl;
}
 
Example #9
Source File: SimplePdfTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testSimplePdf() throws FileNotFoundException, DocumentException {
	// create document
	Document document = PdfTestBase.createPdf("testSimplePdf.pdf");
	try {
		// new page with a rectangle
		document.open();
		document.newPage();
		Annotation ann = new Annotation("Title", "Text");
		Rectangle rect = new Rectangle(100, 100);
		document.add(ann);
		document.add(rect);
	} finally {
		// close document
		if (document != null)
			document.close();
	}

}
 
Example #10
Source File: PdfAnnotation.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * @param writer
 * @param rect
 * @param title
 * @param contents
 * @param open
 * @param icon
 * @return a PdfAnnotation
 */
public static PdfAnnotation createText(PdfWriter writer, Rectangle rect, String title, String contents, boolean open, String icon) {
	PdfAnnotation annot = new PdfAnnotation(writer, rect);
	annot.put(PdfName.SUBTYPE, PdfName.TEXT);
	if (title != null) {
		annot.put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
	}
	if (contents != null) {
		annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
	}
	if (open) {
		annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
	}
	if (icon != null) {
		annot.put(PdfName.NAME, new PdfName(icon));
	}
	return annot;
}
 
Example #11
Source File: RasterLayerComponentImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Add image in the document.
 *
 * @param context
 *            PDF context
 * @param imageResult
 *            image
 * @throws BadElementException
 *             PDF construction problem
 * @throws IOException
 *             PDF construction problem
 */
protected void addImage(PdfContext context, ImageResult imageResult) throws BadElementException, IOException {
	Bbox imageBounds = imageResult.getRasterImage().getBounds();
	float scaleFactor = (float) (72 / getMap().getRasterResolution());
	float width = (float) imageBounds.getWidth() * scaleFactor;
	float height = (float) imageBounds.getHeight() * scaleFactor;
	// subtract screen position of lower-left corner
	float x = (float) (imageBounds.getX() - rasterScale * bbox.getMinX()) * scaleFactor;
	// shift y to lowerleft corner, flip y to user space and subtract
	// screen position of lower-left
	// corner
	float y = (float) (-imageBounds.getY() - imageBounds.getHeight() - rasterScale * bbox.getMinY()) * scaleFactor;
	if (log.isDebugEnabled()) {
		log.debug("adding image, width=" + width + ",height=" + height + ",x=" + x + ",y=" + y);
	}
	// opacity
	log.debug("before drawImage");
	context.drawImage(Image.getInstance(imageResult.getImage()), new Rectangle(x, y, x + width, y + height),
			getSize(), getOpacity());
	log.debug("after drawImage");
}
 
Example #12
Source File: PdfCopy.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
void applyRotation(PdfDictionary pageN, ByteBuffer out) {
    if (!cstp.rotateContents)
        return;
    Rectangle page = reader.getPageSizeWithRotation(pageN);
    int rotation = page.getRotation();
    switch (rotation) {
        case 90:
            out.append(PdfContents.ROTATE90);
            out.append(page.getTop());
            out.append(' ').append('0').append(PdfContents.ROTATEFINAL);
            break;
        case 180:
            out.append(PdfContents.ROTATE180);
            out.append(page.getRight());
            out.append(' ');
            out.append(page.getTop());
            out.append(PdfContents.ROTATEFINAL);
            break;
        case 270:
            out.append(PdfContents.ROTATE270);
            out.append('0').append(' ');
            out.append(page.getRight());
            out.append(PdfContents.ROTATEFINAL);
            break;
    }
}
 
Example #13
Source File: RasterLayerComponentImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Add image with a exception message in the PDF document.
 *
 * @param context
 *            PDF context
 * @param e
 *            exception to put in image
 */
protected void addLoadError(PdfContext context, ImageException e) {
	Bbox imageBounds = e.getRasterImage().getBounds();
	float scaleFactor = (float) (72 / getMap().getRasterResolution());
	float width = (float) imageBounds.getWidth() * scaleFactor;
	float height = (float) imageBounds.getHeight() * scaleFactor;
	// subtract screen position of lower-left corner
	float x = (float) (imageBounds.getX() - rasterScale * bbox.getMinX()) * scaleFactor;
	// shift y to lower left corner, flip y to user space and subtract
	// screen position of lower-left
	// corner
	float y = (float) (-imageBounds.getY() - imageBounds.getHeight() - rasterScale * bbox.getMinY()) * scaleFactor;
	if (log.isDebugEnabled()) {
		log.debug("adding failed message=" + width + ",height=" + height + ",x=" + x + ",y=" + y);
	}
	float textHeight = context.getTextSize("failed", ERROR_FONT).getHeight() * 3f;
	Rectangle rec = new Rectangle(x, y, x + width, y + height);
	context.strokeRectangle(rec, Color.RED, 0.5f);
	context.drawText(getNlsString("RasterLayerComponent.loaderror.line1"), ERROR_FONT, new Rectangle(x, y
			+ textHeight, x + width, y + height), Color.RED);
	context.drawText(getNlsString("RasterLayerComponent.loaderror.line2"), ERROR_FONT, rec, Color.RED);
	context.drawText(getNlsString("RasterLayerComponent.loaderror.line3"), ERROR_FONT, new Rectangle(x, y
			- textHeight, x + width, y + height), Color.RED);
}
 
Example #14
Source File: PdfExamGridTable.java    From unitime with Apache License 2.0 6 votes vote down vote up
public void export(OutputStream out) throws Exception {
    int nrCols = getNrColumns();
    iDocument = (iForm.getDispMode()==sDispModeInRowHorizontal || iForm.getDispMode()==sDispModeInRowVertical ?
        new Document(new Rectangle(Math.max(PageSize.LETTER.getWidth(),60.0f+100.0f*nrCols),Math.max(PageSize.LETTER.getHeight(),60.0f+150f*nrCols)).rotate(), 30, 30, 30, 30)
    :
        new Document(new Rectangle(Math.max(PageSize.LETTER.getWidth(),60.0f+100.0f*nrCols),Math.max(PageSize.LETTER.getHeight(),60.0f+150f*nrCols)), 30, 30, 30, 30));

    PdfEventHandler.initFooter(iDocument, out);
    iDocument.open();
    
    printTable();

    printLegend();

    iDocument.close();
}
 
Example #15
Source File: ImageExporter.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
private void exportVectorGraphics(String formatName, File outputFile) throws ImageExportException {
	Component component = printableComponent.getExportComponent();
	int width = component.getWidth();
	int height = component.getHeight();
	try (FileOutputStream fs = new FileOutputStream(outputFile)) {
		switch (formatName) {
			case PDF:
				// create pdf document with slightly increased width and height
				// (otherwise the image gets cut off)
				Document document = new Document(new Rectangle(width + 5, height + 5));
				PdfWriter writer = PdfWriter.getInstance(document, fs);
				document.open();
				PdfContentByte cb = writer.getDirectContent();
				PdfTemplate tp = cb.createTemplate(width, height);
				Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
				component.print(g2);
				g2.dispose();
				cb.addTemplate(tp, 0, 0);
				document.close();
				break;
			case SVG:
				exportFreeHep(component, fs, new SVGGraphics2D(fs, new Dimension(width, height)));
				break;
			case EPS:
				exportFreeHep(component, fs, new PSGraphics2D(fs, new Dimension(width, height)));
				break;
			default:
				// cannot happen
				break;
		}
	} catch (Exception e) {
		throw new ImageExportException(I18N.getMessage(I18N.getUserErrorMessagesBundle(),
				"error.image_export.export_failed"), e);
	}
}
 
Example #16
Source File: PdfStamperImp.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
void applyRotation(PdfDictionary pageN, ByteBuffer out) {
	if (!rotateContents) {
		return;
	}
	Rectangle page = reader.getPageSizeWithRotation(pageN);
	int rotation = page.getRotation();
	switch (rotation) {
		case 90:
			out.append(PdfContents.ROTATE90);
			out.append(page.getTop());
			out.append(' ').append('0').append(PdfContents.ROTATEFINAL);
			break;
		case 180:
			out.append(PdfContents.ROTATE180);
			out.append(page.getRight());
			out.append(' ');
			out.append(page.getTop());
			out.append(PdfContents.ROTATEFINAL);
			break;
		case 270:
			out.append(PdfContents.ROTATE270);
			out.append('0').append(' ');
			out.append(page.getRight());
			out.append(PdfContents.ROTATEFINAL);
			break;
	}
}
 
Example #17
Source File: FieldPositioningEvents.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
 */
public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) {
	if (cellField == null || (fieldWriter == null && parent == null)) throw new ExceptionConverter(new IllegalArgumentException("You have used the wrong constructor for this FieldPositioningEvents class."));
	cellField.put(PdfName.RECT, new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)));
	if (parent == null)
		fieldWriter.addAnnotation(cellField);
	else
		parent.addKid(cellField);
}
 
Example #18
Source File: PdfAnnotation.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a link.
 * 
 * @param writer
 * @param rect
 * @param highlight
 * @return A PdfAnnotation
 */
protected static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight) {
	PdfAnnotation annot = new PdfAnnotation(writer, rect);
	annot.put(PdfName.SUBTYPE, PdfName.LINK);
	if (!highlight.equals(HIGHLIGHT_INVERT)) {
		annot.put(PdfName.H, highlight);
	}
	return annot;
}
 
Example #19
Source File: PdfReader.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Normalizes a <CODE>Rectangle</CODE> so that llx and lly are smaller than urx and ury.
 * @param box the original rectangle
 * @return a normalized <CODE>Rectangle</CODE>
 */    
public static Rectangle getNormalizedRectangle(PdfArray box) {
    ArrayList rect = box.getArrayList();
    float llx = ((PdfNumber)rect.get(0)).floatValue();
    float lly = ((PdfNumber)rect.get(1)).floatValue();
    float urx = ((PdfNumber)rect.get(2)).floatValue();
    float ury = ((PdfNumber)rect.get(3)).floatValue();
    return new Rectangle(Math.min(llx, urx), Math.min(lly, ury),
    Math.max(llx, urx), Math.max(lly, ury));
}
 
Example #20
Source File: PdfDocument.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Sets the pagesize.
 *
 * @param pageSize the new pagesize
 * @return <CODE>true</CODE> if the page size was set
 */
public boolean setPageSize(Rectangle pageSize) {
    if (writer != null && writer.isPaused()) {
        return false;
    }
    nextPageSize = new Rectangle(pageSize);
    return true;
}
 
Example #21
Source File: PdfContext.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Draw a rectangle's interior with this color.
 *
 * @param rect rectangle
 * @param color colour
 */
public void fillRectangle(Rectangle rect, Color color) {
	template.saveState();
	setFill(color);
	template.rectangle(origX + rect.getLeft(), origY + rect.getBottom(), rect.getWidth(), rect.getHeight());
	template.fill();
	template.restoreState();
}
 
Example #22
Source File: PdfAnnotation.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param writer
 * @param rect
 * @param title
 * @param contents
 * @param open
 * @param icon
 * @return a PdfAnnotation
 */
public static PdfAnnotation createText(PdfWriter writer, Rectangle rect, String title, String contents, boolean open, String icon) {
    PdfAnnotation annot = new PdfAnnotation(writer, rect);
    annot.put(PdfName.SUBTYPE, PdfName.TEXT);
    if (title != null)
        annot.put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
    if (contents != null)
        annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
    if (open)
        annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
    if (icon != null) {
        annot.put(PdfName.NAME, new PdfName(icon));
    }
    return annot;
}
 
Example #23
Source File: PdfAcroForm.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param radiogroup
 * @param value
 * @param llx
 * @param lly
 * @param urx
 * @param ury
 * @return a PdfFormField
 */
public PdfFormField addRadioButton(PdfFormField radiogroup, String value, float llx, float lly, float urx, float ury) {
    PdfFormField radio = PdfFormField.createEmpty(writer);
    radio.setWidget(new Rectangle(llx, lly, urx, ury), PdfAnnotation.HIGHLIGHT_TOGGLE);
    String name = ((PdfName)radiogroup.get(PdfName.V)).toString().substring(1);
    if (name.equals(value)) {
        radio.setAppearanceState(value);
    }
    else {
        radio.setAppearanceState("Off");
    }
    drawRadioAppearences(radio, value, llx, lly, urx, ury);
    radiogroup.addKid(radio);
    return radio;
}
 
Example #24
Source File: DefaultPdfDataEntryFormService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void generatePDFDataEntryForm( Document document, PdfWriter writer, String dataSetUid, int typeId,
    Rectangle pageSize, PdfFormFontSettings pdfFormFontSettings, I18nFormat format )
{
    try
    {
        this.pdfFormFontSettings = pdfFormFontSettings;
        this.format = format;

        document.setPageSize( pageSize );

        document.open();

        if ( typeId == PdfDataEntryFormUtil.DATATYPE_DATASET )
        {
            setDataSet_DocumentContent( document, writer, dataSetUid );
        }
        else if ( typeId == PdfDataEntryFormUtil.DATATYPE_PROGRAMSTAGE )
        {
            setProgramStage_DocumentContent( document, writer, dataSetUid );
        }
    }
    catch ( Exception ex )
    {
        throw new RuntimeException( ex );
    }
    finally
    {
        document.close();
    }
}
 
Example #25
Source File: PdfAnnotation.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param writer
 * @param rect
 * @param title
 * @param contents
 * @param open
 * @param icon
 * @return a PdfAnnotation
 */
public static PdfAnnotation createText(PdfWriter writer, Rectangle rect, String title, String contents, boolean open, String icon) {
    PdfAnnotation annot = new PdfAnnotation(writer, rect);
    annot.put(PdfName.SUBTYPE, PdfName.TEXT);
    if (title != null)
        annot.put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
    if (contents != null)
        annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
    if (open)
        annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
    if (icon != null) {
        annot.put(PdfName.NAME, new PdfName(icon));
    }
    return annot;
}
 
Example #26
Source File: PdfDocument.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the pagesize.
 *
 * @param pageSize the new pagesize
 * @return <CODE>true</CODE> if the page size was set
 */
@Override
public boolean setPageSize(Rectangle pageSize) {
	if (writer != null && writer.isPaused()) {
		return false;
	}
	nextPageSize = new Rectangle(pageSize);
	return true;
}
 
Example #27
Source File: PdfDocument.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Sets the pagesize.
 *
 * @param pageSize the new pagesize
 * @return <CODE>true</CODE> if the page size was set
 */

public boolean setPageSize(Rectangle pageSize) {
    if (writer != null && writer.isPaused()) {
        return false;
    }
    nextPageSize = new Rectangle(pageSize);
    return true;
}
 
Example #28
Source File: CustomPageSizeTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a PDF document with a certain pagesize
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Rectangle pageSize = new Rectangle(216, 720);
	pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xDE));
	Document document = new Document(pageSize);

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file

	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("CustomPageSize.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we add some paragraphs to the document
	document.add(new Paragraph("The size of this page is 216x720 points."));
	document.add(new Paragraph("216pt / 72 points per inch = 3 inch"));
	document.add(new Paragraph("720pt / 72 points per inch = 10 inch"));
	document.add(new Paragraph("The size of this page is 3x10 inch."));
	document.add(new Paragraph("3 inch x 2.54 = 7.62 cm"));
	document.add(new Paragraph("10 inch x 2.54 = 25.4 cm"));
	document.add(new Paragraph("The size of this page is 7.62x25.4 cm."));
	document.add(new Paragraph("The backgroundcolor of the Rectangle used for this PageSize, is #FFFFDE."));
	document.add(new Paragraph("That's why the background of this document is yellowish..."));

	// step 5: we close the document
	document.close();
}
 
Example #29
Source File: EncriptionTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private byte[] createEncryptedPDF () throws DocumentException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	
	Rectangle rec = new Rectangle(PageSize.A4.getWidth(),
			PageSize.A4.getHeight());
	Document document = new Document(rec);		
	PdfWriter writer = PdfWriter.getInstance(document, baos);
	writer.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
	document.open();
	document.add(new Paragraph("Hello World"));
	document.close();
			
	return baos.toByteArray();
}
 
Example #30
Source File: PdfAnnotationsImp.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException {
     switch(annot.annotationType()) {
        case Annotation.URL_NET:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL) annot.attributes().get(Annotation.URL)));
        case Annotation.URL_AS_STRING:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE)));
        case Annotation.FILE_DEST:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), (String) annot.attributes().get(Annotation.DESTINATION)));
        case Annotation.SCREEN:
            boolean sparams[] = (boolean[])annot.attributes().get(Annotation.PARAMETERS);
            String fname = (String) annot.attributes().get(Annotation.FILE);
            String mimetype = (String) annot.attributes().get(Annotation.MIMETYPE);
            PdfFileSpecification fs;
            if (sparams[0])
                fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null);
            else
                fs = PdfFileSpecification.fileExtern(writer, fname);
            PdfAnnotation ann = PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()),
                    fname, fs, mimetype, sparams[1]);
            return ann;
        case Annotation.FILE_PAGE:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.FILE), ((Integer) annot.attributes().get(Annotation.PAGE)).intValue()));
        case Annotation.NAMED_DEST:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction(((Integer) annot.attributes().get(Annotation.NAMED)).intValue()));
        case Annotation.LAUNCH:
            return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String) annot.attributes().get(Annotation.APPLICATION),(String) annot.attributes().get(Annotation.PARAMETERS),(String) annot.attributes().get(Annotation.OPERATION),(String) annot.attributes().get(Annotation.DEFAULTDIR)));
        default:
     	   return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE));
    }
}