Java Code Examples for org.apache.pdfbox.pdmodel.PDPageContentStream#setNonStrokingColor()

The following examples show how to use org.apache.pdfbox.pdmodel.PDPageContentStream#setNonStrokingColor() . 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: JoinPages.java    From testarea-pdfbox2 with Apache License 2.0 7 votes vote down vote up
/**
 * @see #testJoinSmallAndBig()
 */
PDDocument prepareSmallPdf() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(new PDRectangle(72, 72));
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.setNonStrokingColor(Color.YELLOW);
    contentStream.addRect(0, 0, 72, 72);
    contentStream.fill();
    contentStream.setNonStrokingColor(Color.BLACK);
    PDFont font = PDType1Font.HELVETICA;
    contentStream.beginText();
    contentStream.setFont(font, 18);
    contentStream.newLineAtOffset(2, 54);
    contentStream.showText("small");
    contentStream.newLineAtOffset(0, -48);
    contentStream.showText("page");
    contentStream.endText();
    contentStream.close();
    return document;
}
 
Example 2
Source File: VerticalTextCellDrawer.java    From easytable with MIT License 6 votes vote down vote up
protected void drawText(String text, PDFont font, int fontSize, Color color, float x, float y, PDPageContentStream contentStream) throws IOException {
    // Rotate by 90 degrees counter clockwise
    final AffineTransform transform = AffineTransform.getTranslateInstance(x, y);
    transform.concatenate(AffineTransform.getRotateInstance(Math.PI * 0.5));
    transform.concatenate(AffineTransform.getTranslateInstance(-x, -y - fontSize));

    contentStream.moveTo(x, y);
    contentStream.beginText();

    // Do the transformation :)
    contentStream.setTextMatrix(transform);

    contentStream.setNonStrokingColor(color);
    contentStream.setFont(font, fontSize);
    contentStream.newLineAtOffset(x, y);
    contentStream.showText(text);
    contentStream.endText();
    contentStream.setCharacterSpacing(0);
}
 
Example 3
Source File: PdfManipulator.java    From estatio with Apache License 2.0 6 votes vote down vote up
private float addLines(
        final float x, final float y,
        final float height,
        final List<Line> lines,
        final PDPageContentStream cs) throws IOException {
    cs.setFont(TEXT_FONT, TEXT_FONT_SIZE);
    float yLine = y + height - TEXT_LINE_HEIGHT;
    for (Line line : lines) {

        cs.setNonStrokingColor(line.color);
        cs.beginText();
        cs.newLineAtOffset(x + TEXT_X_PADDING, yLine);
        cs.showText(line.text);
        cs.endText();

        yLine -= TEXT_LINE_HEIGHT;
    }

    return yLine;
}
 
Example 4
Source File: JoinPages.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * @see #testJoinSmallAndBig()
 */
PDDocument prepareBiggerPdf() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A5);
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.setNonStrokingColor(Color.GREEN);
    contentStream.addRect(0, 0, PDRectangle.A5.getWidth(), PDRectangle.A5.getHeight());
    contentStream.fill();
    contentStream.setNonStrokingColor(Color.BLACK);
    PDFont font = PDType1Font.HELVETICA;
    contentStream.beginText();
    contentStream.setFont(font, 18);
    contentStream.newLineAtOffset(2, PDRectangle.A5.getHeight() - 24);
    contentStream.showText("This is the Bigger page");
    contentStream.newLineAtOffset(0, -48);
    contentStream.showText("BIGGER!");
    contentStream.endText();
    contentStream.close();
    return document;
}
 
Example 5
Source File: PDFCreator.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void writeText(PDPageContentStream contentStream, Color color, PDFont font, float fontSize, boolean rotate, float x, float y, String... text)
		throws IOException {
	contentStream.beginText();
	// set font and font size
	contentStream.setFont(font, fontSize);
	// set text color
	contentStream.setNonStrokingColor(color.getRed(), color.getGreen(), color.getBlue());
	if (rotate) {
		// rotate the text according to the page rotation
		contentStream.setTextMatrix(Matrix.getRotateInstance(Math.PI / 2, x, y));
	} else {
		contentStream.setTextMatrix(Matrix.getTranslateInstance(x, y));
	}
	if (text.length > 1) {
		contentStream.setLeading(25f);

	}
	for (String line : text) {
		contentStream.showText(line);
		contentStream.newLine();
	}
	contentStream.endText();
}
 
Example 6
Source File: PDFCreator.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void drawRect(PDPageContentStream contentStream, Color color, Rectangle rect, boolean fill) throws IOException {
	contentStream.addRect(rect.x, rect.y, rect.width, rect.height);
	if (fill) {
		contentStream.setNonStrokingColor(color);
		contentStream.fill();
	} else {
		contentStream.setStrokingColor(color);
		contentStream.stroke();
	}
}
 
Example 7
Source File: AppearanceGeneratorHelper.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void insertGeneratedListboxSelectionHighlight(PDPageContentStream contents, PDAppearanceStream appearanceStream,
        PDFont font, float fontSize) throws IOException
{
    List<Integer> indexEntries = ((PDListBox) field).getSelectedOptionsIndex();
    List<String> values = ((PDListBox) field).getValue();
    List<String> options = ((PDListBox) field).getOptionsExportValues();
    
    if (!values.isEmpty() && !options.isEmpty() && indexEntries.isEmpty())
    {
        // create indexEntries from options
        indexEntries = new ArrayList<Integer>();
        for (String v : values)
        {
            indexEntries.add(options.indexOf(v));
        }
    }

    // The first entry which shall be presented might be adjusted by the optional TI key
    // If this entry is present the first entry to be displayed is the keys value otherwise
    // display starts with the first entry in Opt.
    int topIndex = ((PDListBox) field).getTopIndex();
    
    float highlightBoxHeight = font.getBoundingBox().getHeight() * fontSize / FONTSCALE;       

    // the padding area 
    PDRectangle paddingEdge = applyPadding(appearanceStream.getBBox(), 1);

    for (int selectedIndex : indexEntries)
    {
        contents.setNonStrokingColor(HIGHLIGHT_COLOR[0], HIGHLIGHT_COLOR[1], HIGHLIGHT_COLOR[2]);

        contents.addRect(paddingEdge.getLowerLeftX(),
                paddingEdge.getUpperRightY() - highlightBoxHeight * (selectedIndex - topIndex + 1) + 2,
                paddingEdge.getWidth(),
                highlightBoxHeight);
        contents.fill();
    }
    contents.setNonStrokingColor(0);
}
 
Example 8
Source File: PDDefaultAppearanceString.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Writes the DA string to the given content stream.
 */
void writeTo(PDPageContentStream contents, float zeroFontSize) throws IOException
{
    float fontSize = getFontSize();
    if (fontSize == 0)
    {
        fontSize = zeroFontSize;
    }
    contents.setFont(getFont(), fontSize);
    
    if (getFontColor() != null)
    {
        contents.setNonStrokingColor(getFontColor());
    }
}
 
Example 9
Source File: PdfUtilExport.java    From zest-writer with GNU General Public License v3.0 5 votes vote down vote up
private void addCoverpage() throws IOException {
    float leading = 1.5f * FONT_SIZE_TITLE;
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    FONT_STYLE_COVER = PDTrueTypeFont.loadTTF(document, MainApp.class.getResourceAsStream(FONT_MERRIWEATHER_BOLD));
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.setNonStrokingColor(25, 81, 107);
    contentStream.fillRect(0, 0, page.getMediaBox().getWidth(), (page.getMediaBox().getHeight() / 2) - 10);
    contentStream.fillRect(0, (page.getMediaBox().getHeight() / 2) + 10, page.getMediaBox().getWidth(), (page.getMediaBox().getHeight() / 2) - 10);
    contentStream.setNonStrokingColor(248, 173, 50);
    contentStream.fillRect(0, (page.getMediaBox().getHeight() / 2) - 10, page.getMediaBox().getWidth(), 20);

    contentStream.beginText();
    contentStream.setNonStrokingColor(Color.WHITE);
    contentStream.setFont(FONT_STYLE_COVER, FONT_SIZE_AUTHOR);
    contentStream.newLineAtOffset(20, 20);
    contentStream.showText(authorContent);
    contentStream.setFont(FONT_STYLE_COVER, FONT_SIZE_TITLE);
    contentStream.newLineAtOffset((page.getMediaBox().getWidth() / 2) - 20, 600);
    List<String> lines = wrapText((page.getMediaBox().getWidth() / 2) - 20);
    for (String line : lines) {
        contentStream.showText(line);
        contentStream.newLineAtOffset(0, -leading);
    }
    contentStream.endText();

    contentStream.close();
    File temp = File.createTempFile("coverpage-zds", ".pdf");
    document.save(temp);
    document.close();

    PDFMergerUtility mergerUtility = new PDFMergerUtility();
    mergerUtility.addSource(temp);
    mergerUtility.addSource(destPdfPath);
    mergerUtility.setDestinationFileName(destPdfPath);
    mergerUtility.mergeDocuments();
}
 
Example 10
Source File: DrawingUtil.java    From easytable with MIT License 5 votes vote down vote up
public static void drawRectangle(PDPageContentStream contentStream, PositionedRectangle rectangle)
        throws IOException {
    contentStream.setNonStrokingColor(rectangle.getColor());

    contentStream.addRect(rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight());
    contentStream.fill();

    // Reset NonStrokingColor to default value
    contentStream.setNonStrokingColor(Color.BLACK);
}
 
Example 11
Source File: DrawingUtil.java    From easytable with MIT License 5 votes vote down vote up
public static void drawText(PDPageContentStream contentStream, PositionedStyledText styledText) throws IOException {
    contentStream.beginText();
    contentStream.setNonStrokingColor(styledText.getColor());
    contentStream.setFont(styledText.getFont(), styledText.getFontSize());
    contentStream.newLineAtOffset(styledText.getX(), styledText.getY());
    contentStream.showText(styledText.getText());
    contentStream.endText();
    contentStream.setCharacterSpacing(0);
}
 
Example 12
Source File: NativePdfBoxVisibleSignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void setBackground(PDPageContentStream cs, Color color, PDRectangle rect) throws IOException {
	if (color != null) {
           setAlphaChannel(cs, color);
           cs.setNonStrokingColor(color);
           // fill a whole box with the background color
           cs.addRect(rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getWidth(), rect.getHeight());
           cs.fill();
           cleanTransparency(cs);
   	}
}
 
Example 13
Source File: PdfManipulator.java    From estatio with Apache License 2.0 5 votes vote down vote up
private void addBox(
        final float x, final float y, final float height,
        final PDPageContentStream cs) throws IOException {
    cs.setLineWidth(BOX_LINE_WIDTH);
    cs.setStrokingColor(Color.DARK_GRAY);
    cs.addRect(
            x - (float) BOX_LINE_WIDTH,
            y - (float) BOX_LINE_WIDTH,
            BOX_WIDTH + 2* (float) BOX_LINE_WIDTH,
            height + 2* (float) BOX_LINE_WIDTH);
    cs.stroke();
    cs.setNonStrokingColor(BOX_FILL);
    cs.addRect(x, y, BOX_WIDTH, height);
    cs.fill();
}
 
Example 14
Source File: GradeElementRenderer.java    From PDF4Teachers with Apache License 2.0 5 votes vote down vote up
public void renderElement(GradeElement element, PDPageContentStream contentStream, PDPage page, float pageWidth, float pageHeight, float pageRealWidth, float pageRealHeight, float startX, float startY) throws IOException {

        if(!element.isVisible()) return;

        // COLOR
        Color color = element.getColor();
        contentStream.setNonStrokingColor(new java.awt.Color((float) color.getRed(), (float) color.getGreen(), (float) color.getBlue(), (float) color.getOpacity()));

        // FONT
        boolean bold = false;
        if (FontUtils.getFontWeight(element.getFont()) == FontWeight.BOLD) bold = true;
        boolean italic = false;
        if (FontUtils.getFontPosture(element.getFont()) == FontPosture.ITALIC) italic = true;
        InputStream fontFile = FontUtils.getFontFile(element.getFont().getFamily(), italic, bold);
        element.setFont(FontUtils.getFont(element.getFont().getFamily(), italic, bold, element.getFont().getSize() / 596.0 * pageWidth));

        contentStream.beginText();

        // CUSTOM STREAM

        Map.Entry<String, String> entry = Map.entry(element.getFont().getFamily(), FontUtils.getFontFileName(italic, bold));

        if(!fonts.containsKey(entry)){
            PDType0Font font = PDType0Font.load(doc, fontFile);
            contentStream.setFont(font, (float) element.getFont().getSize());
            fonts.put(entry, font);
        }else{
            contentStream.setFont(fonts.get(entry), (float) element.getFont().getSize());
        }

        float bottomMargin = pageRealHeight-pageHeight-startY;
        contentStream.newLineAtOffset(startX + element.getRealX() / Element.GRID_WIDTH * pageWidth, bottomMargin + pageRealHeight - element.getBaseLineY() - element.getRealY() / Element.GRID_HEIGHT * pageHeight);
        try{
            contentStream.showText(element.getText());
        }catch(IllegalArgumentException e){
            e.printStackTrace();
            System.err.println("Erreur : impossible d'écrire la note : \"" + element.getText() + "\" avec la police " + element.getFont().getFamily());
            System.err.println("Message d'erreur : " + e.getMessage());
        }
        contentStream.endText();

    }
 
Example 15
Source File: AppearanceGeneratorHelper.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private void insertGeneratedListboxAppearance(PDPageContentStream contents, PDAppearanceStream appearanceStream,
        PDRectangle contentRect, PDFont font, float fontSize) throws IOException
{
    contents.setNonStrokingColor(0);
    
    int q = field.getQ();

    if (q == PDVariableText.QUADDING_CENTERED || q == PDVariableText.QUADDING_RIGHT)
    {
        float fieldWidth = appearanceStream.getBBox().getWidth();
        float stringWidth = (font.getStringWidth(value) / FONTSCALE) * fontSize;
        float adjustAmount = fieldWidth - stringWidth - 4;

        if (q == PDVariableText.QUADDING_CENTERED)
        {
            adjustAmount = adjustAmount / 2.0f;
        }

        contents.newLineAtOffset(adjustAmount, 0);
    }
    else if (q != PDVariableText.QUADDING_LEFT)
    {
        throw new IOException("Error: Unknown justification value:" + q);
    }

    List<String> options = ((PDListBox) field).getOptionsDisplayValues();
    int numOptions = options.size();

    float yTextPos = contentRect.getUpperRightY();

    int topIndex = ((PDListBox) field).getTopIndex();
    
    for (int i = topIndex; i < numOptions; i++)
    {
       
        if (i == topIndex)
        {
            yTextPos = yTextPos - font.getFontDescriptor().getAscent() / FONTSCALE * fontSize;
        }
        else
        {
            yTextPos = yTextPos - font.getBoundingBox().getHeight() / FONTSCALE * fontSize;
            contents.beginText();
        }

        contents.newLineAtOffset(contentRect.getLowerLeftX(), yTextPos);
        contents.showText(options.get(i));

        if (i != (numOptions - 1))
        {
            contents.endText();
        }
    }
}
 
Example 16
Source File: TestEmptySignatureField.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/37601092/pdfbox-identify-specific-pages-and-functionalities-recommendations">
 * PDFBox identify specific pages and functionalities recommendations
 * </a>
 * 
 * <p>
 * This test shows how to add an empty signature field with a custom appearance
 * to an existing PDF.
 * </p>
 */
@Test
public void testAddEmptySignatureField() throws IOException
{
    try (   InputStream sourceStream = getClass().getResourceAsStream("test.pdf");
            OutputStream output = new FileOutputStream(new File(RESULT_FOLDER, "test-with-empty-sig-field.pdf")))
    {
        PDFont font = PDType1Font.HELVETICA;
        PDResources resources = new PDResources();
        resources.put(COSName.getPDFName("Helv"), font);

        PDDocument document = Loader.loadPDF(sourceStream);
        PDAcroForm acroForm = new PDAcroForm(document);
        acroForm.setDefaultResources(resources);
        document.getDocumentCatalog().setAcroForm(acroForm);

        PDRectangle rect = new PDRectangle(50, 750, 200, 50);

        PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
        PDAppearanceStream appearanceStream = new PDAppearanceStream(document);
        appearanceStream.setBBox(rect.createRetranslatedRectangle());
        appearanceStream.setResources(resources);
        appearanceDictionary.setNormalAppearance(appearanceStream);
        PDPageContentStream contentStream = new PDPageContentStream(document, appearanceStream);
        contentStream.setStrokingColor(Color.BLACK);
        contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
        contentStream.setLineWidth(2);
        contentStream.addRect(0, 0, rect.getWidth(), rect.getHeight());
        contentStream.fill();
        contentStream.moveTo(1 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.lineTo(2 * rect.getHeight() / 4, 3 * rect.getHeight() / 4);
        contentStream.moveTo(1 * rect.getHeight() / 4, 3 * rect.getHeight() / 4);
        contentStream.lineTo(2 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.moveTo(3 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.lineTo(rect.getWidth() - rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.stroke();
        contentStream.setNonStrokingColor(Color.DARK_GRAY);
        contentStream.beginText();
        contentStream.setFont(font, rect.getHeight() / 5);
        contentStream.newLineAtOffset(3 * rect.getHeight() / 4, -font.getBoundingBox().getLowerLeftY() * rect.getHeight() / 5000);
        contentStream.showText("Customer");
        contentStream.endText();
        contentStream.close();

        PDSignatureField signatureField = new PDSignatureField(acroForm);
        signatureField.setPartialName("SignatureField");
        PDPage page = document.getPage(0);

        PDAnnotationWidget widget = signatureField.getWidgets().get(0);
        widget.setAppearance(appearanceDictionary);
        widget.setRectangle(rect);
        widget.setPage(page);

        page.getAnnotations().add(widget);
        acroForm.getFields().add(signatureField);

        document.save(output);
        document.close();
    }
}
 
Example 17
Source File: RectanglesOverText.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/46080131/text-coordinates-when-stripping-from-pdfbox">
 * Text coordinates when stripping from PDFBox
 * </a>
 * <p>
 * This test applies the OP's code to an arbitrary PDF file and it did work properly
 * (well, it did only cover the text from the baseline upwards but that is to be expected).
 * </p>
 */
@Test
public void testCoverTextByRectanglesInput() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("input.pdf")  ) {
        PDDocument doc = Loader.loadPDF(resource);

        myStripper stripper = new myStripper();

        stripper.setStartPage(1); // fix it to first page just to test it
        stripper.setEndPage(1);
        stripper.getText(doc);

        TextLine line = stripper.lines.get(1); // the line i want to paint on

        float minx = -1;
        float maxx = -1;

        for (TextPosition pos: line.textPositions)
        {
            if (pos == null)
                continue;

            if (minx == -1 || pos.getTextMatrix().getTranslateX() < minx) {
                minx = pos.getTextMatrix().getTranslateX();
            }
            if (maxx == -1 || pos.getTextMatrix().getTranslateX() > maxx) {
                maxx = pos.getTextMatrix().getTranslateX();
            }
        }

        TextPosition firstPosition = line.textPositions.get(0);
        TextPosition lastPosition = line.textPositions.get(line.textPositions.size() - 1);

        float x = minx;
        float y = firstPosition.getTextMatrix().getTranslateY();
        float w = (maxx - minx) + lastPosition.getWidth();
        float h = lastPosition.getHeightDir();

        PDPageContentStream contentStream = new PDPageContentStream(doc, doc.getPage(0), PDPageContentStream.AppendMode.APPEND, false);

        contentStream.setNonStrokingColor(Color.RED);
        contentStream.addRect(x, y, w, h);
        contentStream.fill();
        contentStream.close();

        File fileout = new File(RESULT_FOLDER, "input-withRectangles.pdf");
        doc.save(fileout);
        doc.close();
    }
}
 
Example 18
Source File: RectanglesOverText.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/46080131/text-coordinates-when-stripping-from-pdfbox">
 * Text coordinates when stripping from PDFBox
 * </a>
 * <br/>
 * <a href="https://download-a.akamaihd.net/files/media_mwb/b7/mwb_I_201711.pdf">
 * mwb_I_201711.pdf
 * </a>
 * <p>
 * This test applies the OP's code to his example PDF file and indeed, there is an offset!
 * This is due to the <code>LegacyPDFStreamEngine</code> method <code>showGlyph</code>
 * which manipulates the text rendering matrix to make the lower left corner of the
 * crop box the origin. In the current version of this test, that offset is corrected,
 * see below. 
 * </p>
 */
@Test
public void testCoverTextByRectanglesMwbI201711() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("mwb_I_201711.pdf")  ) {
        PDDocument doc = Loader.loadPDF(resource);

        myStripper stripper = new myStripper();

        stripper.setStartPage(1); // fix it to first page just to test it
        stripper.setEndPage(1);
        stripper.getText(doc);

        TextLine line = stripper.lines.get(1); // the line i want to paint on

        float minx = -1;
        float maxx = -1;

        for (TextPosition pos: line.textPositions)
        {
            if (pos == null)
                continue;

            if (minx == -1 || pos.getTextMatrix().getTranslateX() < minx) {
                minx = pos.getTextMatrix().getTranslateX();
            }
            if (maxx == -1 || pos.getTextMatrix().getTranslateX() > maxx) {
                maxx = pos.getTextMatrix().getTranslateX();
            }
        }

        TextPosition firstPosition = line.textPositions.get(0);
        TextPosition lastPosition = line.textPositions.get(line.textPositions.size() - 1);

        // corrected x and y
        PDRectangle cropBox = doc.getPage(0).getCropBox();

        float x = minx + cropBox.getLowerLeftX();
        float y = firstPosition.getTextMatrix().getTranslateY() + cropBox.getLowerLeftY();
        float w = (maxx - minx) + lastPosition.getWidth();
        float h = lastPosition.getHeightDir();

        PDPageContentStream contentStream = new PDPageContentStream(doc, doc.getPage(0), PDPageContentStream.AppendMode.APPEND, false, true);

        contentStream.setNonStrokingColor(Color.RED);
        contentStream.addRect(x, y, w, h);
        contentStream.fill();
        contentStream.close();

        File fileout = new File(RESULT_FOLDER, "mwb_I_201711-withRectangles.pdf");
        doc.save(fileout);
        doc.close();
    }
}
 
Example 19
Source File: NativePdfBoxVisibleSignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Draws a custom text with the specified parameters
 * @param cs
 *		{@link PDPageContentStream} current stream
 * @param dimensionAndPosition
 *		{@link SignatureFieldDimensionAndPosition} size and position to place the text to
 * @param textParameters
 *		{@link SignatureImageTextParameters} text to place on the signature field
 * @throws IOException
 *		in case of error
 */
private void setText(PDPageContentStream cs, SignatureFieldDimensionAndPosition dimensionAndPosition, 
		SignatureImageParameters parameters) throws IOException {
	SignatureImageTextParameters textParameters = parameters.getTextParameters();
   	if (textParameters != null && Utils.isStringNotEmpty(textParameters.getText())) {
   		setTextBackground(cs, textParameters, dimensionAndPosition);
   		DSSFont dssFont = textParameters.getFont();
           float fontSize = dssFont.getSize();
           fontSize *= ImageUtils.getScaleFactor(parameters.getZoom());
           cs.beginText();
           cs.setFont(pdFont, fontSize);
           cs.setNonStrokingColor(textParameters.getTextColor());
           setAlphaChannel(cs, textParameters.getTextColor());
           
           PdfBoxFontMetrics pdfBoxFontMetrics = new PdfBoxFontMetrics(pdFont);
           
           String[] strings = pdfBoxFontMetrics.getLines(textParameters.getText());
           
           float properSize = CommonDrawerUtils.computeProperSize(textParameters.getFont().getSize(), parameters.getDpi());
           
           float fontHeight = pdfBoxFontMetrics.getHeight(textParameters.getText(), properSize);
           cs.setLeading(textSizeWithDpi(fontHeight, dimensionAndPosition.getyDpi()));
           
           cs.newLineAtOffset(dimensionAndPosition.getTextX(),
           		// align vertical position
           		dimensionAndPosition.getTextHeight() + dimensionAndPosition.getTextY() - fontSize);

           float previousOffset = 0;
           for (String str : strings) {
               float stringWidth = pdfBoxFontMetrics.getWidth(str, fontSize);
               float offsetX = 0;
               switch (textParameters.getSignerTextHorizontalAlignment()) {
				case RIGHT:
					offsetX = dimensionAndPosition.getTextWidth() - stringWidth - 
							textSizeWithDpi(textParameters.getPadding()*2, dimensionAndPosition.getxDpi()) - previousOffset;
					break;
				case CENTER:
					offsetX = (dimensionAndPosition.getTextWidth() - stringWidth) / 2 - 
							textSizeWithDpi(textParameters.getPadding(), dimensionAndPosition.getxDpi()) - previousOffset;
					break;
				default:
					break;
			}
               previousOffset += offsetX;
			cs.newLineAtOffset(offsetX, 0); // relative offset
               cs.showText(str);
               cs.newLine();
           }
           cs.endText();
           cleanTransparency(cs);
   	}
}