itextpdf通过for导出多个pdf出现com.itextpdf.kernel.PdfException: pdf.inderect.object.belong.to.other.pdf.docu

pom导入依赖包

	
		
			com.itextpdf
			barcodes
			7.0.0
			
		


		
			com.itextpdf
			font-asian
			7.0.0
		


		
			com.itextpdf
			forms
			7.0.0
			
		


		
			com.itextpdf
			hyph
			7.0.0
		


		
			com.itextpdf
			io
			7.0.0
		


		
			com.itextpdf
			kernel
			7.0.0
			
		


		
			com.itextpdf
			layout
			7.0.0
			
		


		
			com.itextpdf
			pdfa
			7.0.0
			
		


		
			com.itextpdf
			sign
			7.0.0
			
		

import java.io.IOException;

import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.TextAlignment;
 
/**
 * Simple List example.
 */
public class Demo {
        public static final String DEST = "C:/aa/test/";
        public static PdfFont cnFont = null;
        
    public static void main(String args[]) throws IOException {
        new Demo().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
    	for(int j =0 ;j<2;j++){
    		PdfWriter writer = new PdfWriter(dest + j + ".pdf");
    		PdfDocument pdf = new PdfDocument(writer);
    		
    		Document document = new Document(pdf);
    		document.add(createYxTitle("(aaaa)"));
    		document.add(createParagraph("协议编号:___aaa___",10).setTextAlignment(TextAlignment.RIGHT));
    		
    		Paragraph p1 = new Paragraph();
    		p1.setFont(getCnFont());
    		p1.setBold();
    		p1.setFontSize(12);
    		p1.add(new Text("甲方:小明 \n"));
    		p1.add(new Text("真实姓名:小明 \n"));
    		document.add(p1);
    		
    		Paragraph p = new Paragraph();
    		p.setFont(getCnFont());
    		p.setBold();
    		p.setFontSize(12);
    		Text t1 = new Text("乙方:夏红 \n");
    		Text t2 = new Text("地址:bj\n");
    		
    		p.add(t1);
    		p.add(t2);
    		document.add(p);
    		
    		document.close();
    		document.flush();
    	}
    }
    
    private Paragraph createParagraph(String text,int size){
    	Paragraph p = new Paragraph(text);
    	p.setFontSize(size);
    	p.setFont(getCnFont());
    	return p;
    }
    
    private Paragraph createYxTitle(String text){
    	 Paragraph p = new Paragraph();
         Text title = new Text("helloworld");
         title.setBold();
         Text term = new Text(text);
         term.setFontSize(12);
         p.add(title).add(term);
         p.setFont(getCnFont());
         p.setTextAlignment(TextAlignment.CENTER);
         return p;
    }
    
    /**
     * 
     * @Description: 获取支持中文的字体,为单例
     * @param @return   
     * @return PdfFont  
     * @throws
     * @author peng.wu
     * @date 2016年8月24日
     */
    private PdfFont getCnFont(){
		try {
			if(cnFont == null){
				cnFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", false);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return cnFont;
    }
}

运行上述程序出现如下异常


SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.itextpdf.kernel.PdfException: pdf.inderect.object.belong.to.other.pdf.document.Copy.object.to.current.pdf.document
	at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:184)
	at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:174)
	at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:104)
	at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:176)
	at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:104)
	at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:176)
	at com.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:104)
	at com.itextpdf.kernel.pdf.PdfWriter.writeToBody(PdfWriter.java:315)
	at com.itextpdf.kernel.pdf.PdfWriter.flushObject(PdfWriter.java:232)
	at com.itextpdf.kernel.pdf.PdfDocument.flushObject(PdfDocument.java:1257)
	at com.itextpdf.kernel.pdf.PdfObject.flush(PdfObject.java:147)
	at com.itextpdf.kernel.pdf.PdfObject.flush(PdfObject.java:124)
	at com.itextpdf.kernel.pdf.PdfObjectWrapper.flush(PdfObjectWrapper.java:97)
	at com.itextpdf.kernel.pdf.PdfPage.flush(PdfPage.java:410)
	at com.itextpdf.kernel.pdf.PdfPage.flush(PdfPage.java:367)
	at com.itextpdf.kernel.pdf.PdfDocument.close(PdfDocument.java:720)
	at com.itextpdf.layout.Document.close(Document.java:115)
	at com.sirding.pdf.Demo.createPdf(Demo.java:58)
	at com.sirding.pdf.Demo.main(Demo.java:27)


问题出现的原因在执行

document.close();
关流操作,下面是源码中的逻辑判断,红色是出现异常的位置

 private void write(PdfIndirectReference indirectReference) {
        if (document != null && !indirectReference.getDocument().equals(document)) {
            throw new PdfException(PdfException.PdfInderectObjectBelongToOtherPdfDocument);
        }
        if (indirectReference.getRefersTo() == null) {
            write(PdfNull.PDF_NULL);
        } else if (indirectReference.getGenNumber() == 0) {
            writeInteger(indirectReference.getObjNumber()).
                    writeBytes(endIndirectWithZeroGenNr);
        } else {
            writeInteger(indirectReference.getObjNumber()).
                    writeSpace().
                    writeInteger(indirectReference.getGenNumber()).
                    writeBytes(endIndirect);
        }
    }

!indirectReference.getDocument().equals(document)此部分是出现问题的具体位置,大致意思就是说当前使document与对象内部关联应用的对象不一致,查看demo发现定义了
 public static PdfFont cnFont = null;

 这样的静态成员变量,也就是第二关流或是flush时使用的document和内部关联的document不是同一个对象的应用,因此出现上述异常。 
  

解决办法:

 public static PdfFont cnFont = null;
改为局部变量即可。




你可能感兴趣的:(技术经验)