バーコード4jライブラリ(code128bean、その他のバーコードBean)からバーコードを生成し、既存のpdfに追加しようとしています。バーコード イメージは、以下のコードを使用してローカルに作成されています。
//Create the barcode bean
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
//width exactly one pixel
//bean.setCodeset(2);
code128Bean.doQuietZone(false);
//Open output file
File outputFile = new File("D:/barcode4jcod128.png"); //I dont want to create it
OutputStream code128Stream = new FileOutputStream(outputFile);
try {
//Set up the canvas provider for monochrome PNG output
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
code128Stream, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
code128Bean.generateBarcode(canvas1, "123456");
//Signal end of generation
canvas1.finish();
} finally {
code128Stream.close();
}
- 私の問題は、画像を作成してファイルシステムにローカルに保存し、それを画像としてpdfに追加したくないことです。動的に作成したいだけです。つまり、バーコード画像を動的に作成してpdfに追加するだけです。
- メソッド
PDPage.PAGE_SIZE_A4
から取得した既存の PDPages にページサイズ ( など) を設定するにはどうすればよいですか( )catalog.getAllPages()
List<PDPage> pages = catalog.getAllPages();
誰かがこれについて助けることができますか?
ティルマンさん、ご協力ありがとうございます。これが私がしたことです
public static BufferedImage geBufferedImageForCode128Bean(String barcodeString) {
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
code128Bean.doQuietZone(false);
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
//Generate the barcode
code128Bean.generateBarcode(canvas1, barcodeString);
return canvas1.getBufferedImage();
}
// main code
PDDocument finalDoc = new PDDocument();
BufferedImage bufferedImage = geBufferedImageForCode128Bean("12345");
PDXObjectImage pdImage = new PDPixelMap(doc, bufferedImage);
PDPageContentStream contentStream = new PDPageContentStream(
finalDoc, pdPage, true, true, true
);
contentStream.drawXObject(pdImage, 100, 600, 50, 20);
contentStream.close();
finalDoc.addPage(pdPage);
finalDoc.save(new File("D:/Test75.pdf"));
バーコードが作成されていますが、縦に作成されています。横向きで見たいです。ご協力いただきありがとうございます。