Created
February 21, 2020 15:03
-
-
Save vandeseer/d5c8ac342e52cbfa521ce0a55849b7e4 to your computer and use it in GitHub Desktop.
Easytable Artificially Bold Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.vandeseer.integrationtest.custom; | |
import org.apache.pdfbox.pdmodel.PDDocument; | |
import org.apache.pdfbox.pdmodel.PDPage; | |
import org.apache.pdfbox.pdmodel.PDPageContentStream; | |
import org.apache.pdfbox.pdmodel.common.PDRectangle; | |
import org.vandeseer.easytable.TableDrawer; | |
import org.vandeseer.easytable.drawing.DrawingContext; | |
import org.vandeseer.easytable.drawing.PositionedStyledText; | |
import org.vandeseer.easytable.drawing.cell.TextCellDrawer; | |
import org.vandeseer.easytable.structure.Row; | |
import org.vandeseer.easytable.structure.Table; | |
import org.vandeseer.easytable.structure.cell.TextCell; | |
import java.io.IOException; | |
import static org.apache.pdfbox.pdmodel.font.PDType1Font.HELVETICA; | |
public class CustomCellDrawer { | |
private static final TextCellDrawer CUSTOM_DRAWER = new TextCellDrawer() { | |
@Override | |
protected void drawText(DrawingContext drawingContext, PositionedStyledText styledText) throws IOException { | |
PDPageContentStream contentStream = drawingContext.getContentStream(); | |
contentStream.beginText(); | |
contentStream.appendRawCommands("2 Tr\n"); // <-- This is the change needed | |
contentStream.setNonStrokingColor(styledText.getColor()); | |
contentStream.setFont(styledText.getFont(), styledText.getFontSize()); | |
contentStream.newLineAtOffset(styledText.getX(), styledText.getY()); | |
contentStream.showText(styledText.getText()); | |
contentStream.appendRawCommands("0 Tr\n"); // <-- Reset | |
contentStream.endText(); | |
contentStream.setCharacterSpacing(0); | |
} | |
}; | |
public static void main(String[] args) throws IOException { | |
try(final PDDocument document = new PDDocument()) { | |
final PDPage page = new PDPage(PDRectangle.A4); | |
document.addPage(page); | |
try (final PDPageContentStream contentStream = new PDPageContentStream(document, page)) { | |
TableDrawer.builder() | |
.contentStream(contentStream) | |
.table(createSimpleTable()) | |
.startX(50) | |
.startY(page.getMediaBox().getHeight() - 50) | |
.build() | |
.draw(); | |
} | |
document.save("target/customCellDrawerNoLombok.pdf"); | |
} | |
} | |
private static Table createSimpleTable() { | |
return Table.builder() | |
.addColumnsOfWidth(100, 100, 100, 100) | |
.fontSize(8) | |
.font(HELVETICA) | |
.addRow(Row.builder() | |
.add(TextCell.builder().drawer(CUSTOM_DRAWER).borderWidth(1).text("One").build()) | |
.add(TextCell.builder().borderWidth(1).text("Two").build()) | |
.add(TextCell.builder().borderWidth(1).text("Three").build()) | |
.add(TextCell.builder().borderWidth(1).text("Four").build()) | |
.build()) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment