生成并使用PDF底部的条形码

时间:2014-07-25 04:27:10

标签: java pdf barcode

我使用IText为我的pdf生成条形码。 我对这一特定代码行有疑问。

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Mobile/billPayment.pdf");

当我将输出文件指向billPayment.pdf时,它会覆盖现有数据,删除pdf上的所有内容,然后只给我条形码。 无论如何我可以保留现有数据以及生成的条形码吗?

1 个答案:

答案 0 :(得分:2)

以下是使用开源技术生成pdf并具有条形码的示例: (https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CCUQiBUwAWoVChMI6KiL5urIyAIVRSmICh1WhASJ&url=https%3A%2F%2Fplus.google.com%2F106756017574705877581%2Fposts%2FZLE22J3jPMa&usg=AFQjCNFFs6Vfdq4Sr-mtjcDXfgcX0SxNKw&sig2=swMxmIhVUqq2iiZnuwssow

使用开源API生成具有字符串条形码(即SKU)的PDF。

验收标准是应该能够使用任何条形码扫描仪扫描生成的条形码。 为了生成条形码,我使用了“Zxing”API。以下示例中使用的罐子是:core-2.2.jar,javase.jar。 为了生成PDF,我使用了PDFBox API。在这个例子中,我使用了以下jar:pdfbox-1.8.10.jar,pdfbox-app-1.8.10.jar。 上面提到的jar可以在以下链接中找到:

https://sites.google.com/site/sujeetcorp/files/PDFBox%26Zxing_jars.zip?attredirects=0&d=1

在下面的示例中,我创建了一个包含两个页面的PDF文件,每个页面上有两个不同的条形码。 这是代码:

        package com.barcode;
        import java.awt.Color;
        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.FileNotFoundException;
        import org.apache.pdfbox.pdmodel.PDDocument;
        import org.apache.pdfbox.pdmodel.PDPage;
        import org.apache.pdfbox.pdmodel.common.PDRectangle;
        import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
        import org.apache.pdfbox.pdmodel.font.PDFont;
        import org.apache.pdfbox.pdmodel.font.PDType1Font;

        import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap;
        import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
        import com.google.zxing.BarcodeFormat;
        import com.google.zxing.client.j2se.MatrixToImageWriter;
        import com.google.zxing.common.BitMatrix;
        import com.google.zxing.oned.Code128Writer;


        public class BarCode {

            public static void main (String[] args) throws Exception {
                 BitMatrix bitMatrix;

                 String outputFileName = "Simple.pdf";
                 File outPutFile=new File(outputFileName);     

                PDDocument document = new PDDocument();
                PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);          
                PDRectangle rect = page.getMediaBox();           
                document.addPage(page);

                // Create a new font object selecting one of the PDF base fonts

                PDFont fontMono = PDType1Font.COURIER;

                // Start a new content stream which will "hold" the to be created content
                PDPageContentStream cos = new PDPageContentStream(document, page);

                int line = 0;
             // add an image
                try {    

                    cos.beginText();
                    cos.setFont(fontMono, 20);
                    cos.setNonStrokingColor(Color.BLUE);
                    cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
                    cos.drawString("APL IPHONE 5C BLUE 16GB KIT");
                    cos.endText(); 

                    cos.beginText();
                    cos.setFont(fontMono, 20);
                    cos.setNonStrokingColor(Color.BLUE);
                    cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
                    cos.drawString("SKU:");
                    cos.endText();

                    bitMatrix = new Code128Writer().encode("M1G542LL/A", BarcodeFormat.CODE_128, 150, 80, null);          
                    BufferedImage buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);           
                    PDXObjectImage ximage = new PDPixelMap(document, buffImg);          
                    cos.drawXObject(ximage, 150, rect.getHeight() - 50*(++line), 150, 50);  

                    cos.beginText();
                    cos.setFont(fontMono, 10);
                    cos.setNonStrokingColor(Color.BLUE);
                    cos.moveTextPositionByAmount(200, rect.getHeight() - 50*(++line));
                    cos.drawString("M1G542LL/A");
                    cos.endText();            

                    cos.beginText();
                    cos.setFont(fontMono, 20);
                    cos.setNonStrokingColor(Color.BLUE);
                    cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
                    cos.drawString("IMEI:");
                    cos.endText();

                    bitMatrix = new Code128Writer().encode("123456789", BarcodeFormat.CODE_128, 150, 80, null);       
                    buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);       
                    ximage = new PDPixelMap(document, buffImg);                
                    cos.drawXObject(ximage, 150, rect.getHeight() - 50*(++line), 150, 50);   

                    cos.close();



                     page = new PDPage(PDPage.PAGE_SIZE_A4);          
                     rect = page.getMediaBox();           
                     document.addPage(page); 
                     line = 0;

                    // Start a new content stream which will "hold" the to be created content
                    cos = new PDPageContentStream(document, page);          
                    cos.beginText();
                    cos.setFont(fontMono, 20);
                    cos.setNonStrokingColor(Color.BLUE);
                    cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
                    cos.drawString("APL IPHONE 5C BLUE 16GB KIT");
                    cos.endText(); 

                    cos.beginText();
                    cos.setFont(fontMono, 20);
                    cos.setNonStrokingColor(Color.BLUE);
                    cos.moveTextPositionByAmount(100, rect.getHeight() - 60*(++line));
                    cos.drawString("SKU:");
                    cos.endText();


                    bitMatrix = new Code128Writer().encode("M1G542LL/A", BarcodeFormat.CODE_128, 150, 80, null);    
                    buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);

                    ximage = new PDPixelMap(document, buffImg);     
                    cos.drawXObject(ximage, 150, rect.getHeight() - 60*(++line), 150, 50);   
                    cos.beginText();
                    cos.setFont(fontMono, 10);
                    cos.setNonStrokingColor(Color.BLUE);
                    cos.moveTextPositionByAmount(200, rect.getHeight() - 60*(line)-10);
                    cos.drawString("M1G542LL/A");
                    cos.endText();


                    cos.beginText();
                    cos.setFont(fontMono, 20);
                    cos.setNonStrokingColor(Color.BLUE);
                    cos.moveTextPositionByAmount(100, rect.getHeight() - 60*(++line));
                    cos.drawString("IMEI:");
                    cos.endText();

                    bitMatrix = new Code128Writer().encode("352065061762230", BarcodeFormat.CODE_128, 150, 80, null);     
                    buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix);   
                    ximage = new PDPixelMap(document, buffImg);                
                    cos.drawXObject(ximage, 150, rect.getHeight() - 60*(++line), 150, 50);          


                } catch (FileNotFoundException fnfex) {
                    System.out.println("No image for you");
                }

                cos.close();

                document.save(outputFileName);       
                document.close();
            }
        }