Created
March 29, 2018 15:04
-
-
Save inog/6dbd50e2fd45d4aa34647c66a8133b35 to your computer and use it in GitHub Desktop.
Play with pdfbox
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 com.mathai.pdf; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import org.apache.pdfbox.io.IOUtils; | |
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.apache.pdfbox.pdmodel.font.PDFont; | |
import org.apache.pdfbox.pdmodel.font.PDType0Font; | |
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; | |
import org.jboss.logging.Logger; | |
import com.mathai.controller.edv.FiberBarcodeDTO; | |
public class FiberBarcodePDF { | |
private static final Logger logger = Logger.getLogger(FiberBarcodePDF.class); | |
private static final float marginLeft = 50f; | |
private static final float marginTop = 20f; | |
private static final float fontSize = 10f; | |
private static final float fontSizeBC = 36f; | |
public static void main(String[] args) throws IOException { | |
FiberBarcodeDTO fiberDTO = new FiberBarcodeDTO(230483, 5545, 2018); | |
fiberDTO.setLength(120); | |
fiberDTO.setNuance("Blonde 22 /s"); | |
fiberDTO.setProduct("CoolFiber Produkt 20"); | |
fiberDTO.setQty(300); | |
fiberDTO.setPressing(0.8); | |
fiberDTO.setThickness(111); | |
fiberDTO.setType("FW-4S"); | |
File file = new File("C:\\Users\\ireschke\\temp\\barcode.pdf"); | |
FileOutputStream fos = new FileOutputStream(file); | |
ByteArrayOutputStream createPDF = createPDF(fiberDTO); | |
fos.write(createPDF.toByteArray()); | |
fos.flush(); | |
fos.close(); | |
} | |
public static ByteArrayOutputStream createPDF(FiberBarcodeDTO fiberDTO) throws IOException { | |
// logger.trace(String.format("Enter createPDF() %s.",fiberDTO)); | |
ByteArrayOutputStream output = new ByteArrayOutputStream(); | |
PDDocument pddocu = new PDDocument(); | |
final PDFont font = PDType0Font.load(pddocu, FiberBarcodePDF.class.getResourceAsStream("/Cousine-Regular.ttf")); | |
final PDFont fontBold = PDType0Font.load(pddocu, FiberBarcodePDF.class.getResourceAsStream("/Cousine-Bold.ttf")); | |
final PDFont font39 = PDType0Font.load(pddocu, FiberBarcodePDF.class.getResourceAsStream("/free3of9.ttf")); | |
InputStream imgStream = FiberBarcodePDF.class.getResourceAsStream("/GeorgeMathai.png"); | |
byte[] bytes = IOUtils.toByteArray(imgStream); | |
PDImageXObject pdImage = PDImageXObject.createFromByteArray(pddocu, bytes , "gm.png"); | |
PDPage pdPage = new PDPage(new PDRectangle(PDRectangle.A3.getHeight(),PDRectangle.A3.getWidth())); | |
PDPageContentStream content = new PDPageContentStream(pddocu, pdPage); | |
content.setLeading(10f); | |
PDRectangle mediaBox = pdPage.getMediaBox(); | |
float margin = 15f; | |
float startX = mediaBox.getLowerLeftX() + margin; | |
float startY = mediaBox.getUpperRightY() - margin; | |
float labelHeight = 99.21f; | |
float labelWidth = 227f; | |
placeLabel(fiberDTO, font, fontBold, font39, pdImage, content, startX, startY); | |
int maxCount = 31; | |
for (int i = 0 ; i< maxCount ; i++) { | |
placeLabel(fiberDTO, font, fontBold, font39, pdImage, content, startX, startY); | |
startY = startY - labelHeight ; | |
if (startY-labelHeight <=0 ) { | |
startY = mediaBox.getUpperRightY() - margin; | |
startX = startX + labelWidth + margin; | |
} | |
if (startX + labelWidth + margin >= mediaBox.getWidth() ) { | |
content.close(); | |
pddocu.addPage(pdPage); | |
pdPage = new PDPage(new PDRectangle(PDRectangle.A3.getHeight(),PDRectangle.A3.getWidth())); | |
content = new PDPageContentStream(pddocu, pdPage); | |
content.setLeading(10f); | |
startX = mediaBox.getLowerLeftX() + margin; | |
startY = mediaBox.getUpperRightY() - margin; | |
} | |
} | |
content.close(); | |
pddocu.addPage(pdPage); | |
pddocu.save(output); | |
pddocu.close(); | |
return output; | |
} | |
private static void placeLabel(FiberBarcodeDTO fiberDTO, final PDFont font, final PDFont fontBold, final PDFont font39, PDImageXObject pdImage, PDPageContentStream content, float startX, | |
float startY) throws IOException { | |
float ty = startY-25f; | |
content.beginText(); | |
content.setFont(font39, fontSizeBC); | |
content.newLineAtOffset(startX, ty); | |
content.showText(String.format("*%d*", fiberDTO.getBcnr())); | |
content.endText(); | |
content.drawImage(pdImage, startX + 120, ty, 70, 16); | |
content.beginText(); | |
content.setFont(font, fontSize); | |
ty = ty - 10f; | |
content.newLineAtOffset(startX, ty); | |
content.showText(String.format("%d ", fiberDTO.getBcnr())); | |
content.setFont(fontBold, fontSize); | |
content.showText(String.format("#%4d-%4d", fiberDTO.getAun1(), fiberDTO.getAujj())); | |
content.newLine(); | |
content.setFont(font, fontSize); | |
content.showText(String.format("%-20.20s", fiberDTO.getProduct())); | |
content.newLine(); | |
content.showText(String.format("Nuance:")); | |
content.setFont(fontBold, fontSize); | |
content.showText(String.format("%-7.7s", fiberDTO.getNuance())); | |
content.setFont(font, fontSize); | |
content.newLine(); | |
content.showText(String.format("%-5.5s Len:%3dmm Qty:%d", fiberDTO.getType(), fiberDTO.getLength(), fiberDTO.getQty())); | |
content.newLine(); | |
content.showText(String.format("Press:%1.1fmm Thick:%3d%%", fiberDTO.getPressing(),fiberDTO.getThickness())); | |
content.endText(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment