2014-07-25 40 views
0

我已经使用IText为我的PDF生成条形码。 我对这个特定的代码行有疑问。生成并使用我的PDF底部的条形码

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

当我点的输出文件billPayment.pdf,它覆盖现有数据,删除所有内容什么是PDF和刚才给我的条形码。 有没有反正我可以坚持我现有的数据以及生成的条形码?

+0

尝试追加方法? –

+1

'PdfWriter'是一个用于从头开始生成新PDF的类。从使用'FileOutputStream'(强调**'Output' **)的事实可以清楚地看到,该文件不会被读取。 “PdfStamper”是用于在现有PDF上标记某些东西的iText类,查找“PdfStamper”示例。 – mkl

回答

2

这里是生成使用开源技术PDF和具有条形码的例子:一个字符串(即SKU)的具有条形码 (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

生成PDF,使用开放源码的API。

验收标准是生成的条码应该能够使用任何条码扫描器进行扫描。 要生成条形码,我使用了“Zxing”API。以下示例中使用的jar包是:core-2.2.jar,javase.jar。 要生成PDF,我已经使用了PDFBox API。在这个例子中,我使用了以下jar:pdfbox-1.8.10.jar,pdfbox-app-1.8.10.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(); 
      } 
     }