When I was generating pdf on iText7, the following things happened:
com.itextpdf.kernel.PdfException: Pdf direct object belongs to other PDF document.Copy object to current pdf document.
atcom.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:195) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:185) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:115) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:187) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:115) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:187) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfOutputStream.write(PdfOutputStream.java:115) to [kernel-7.0.2.jar:na]
at com.itextpdf.kernel.pdf.PdfWriter.writeToBody (PdfWriter.java:383) to [kernel-7.0.2.jar:na]
at com.itextpdf.kernel.pdf.PdfWriter.flushObject (PdfWriter.java:289) to [kernel-7.0.2.jar:na]
at com.itextpdf.kernel.pdf.PdfDocument.flushObject (PdfDocument.java:1572) to [kernel-7.0.2.jar:na]
at com.itextpdf.kernel.pdf.PdfObject.flush(PdfObject.java:159) to [kernel-7.0.2.jar:na]
at com.itextpdf.kernel.pdf.PdfObject.flush(PdfObject.java:127) to [kernel-7.0.2.jar:na]
at com.itextpdf.kernel.pdf.PdfObjectWrapper.flush (PdfObjectWrapper.java:94) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfPage.flush (PdfPage.java:495) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfPage.flush (PdfPage.java:454) to [kernel-7.0.2.jar:na]
atcom.itextpdf.kernel.pdf.PdfDocument.close(PdfDocument.java:785) to [kernel-7.0.2.jar:na]
at com.itextpdf.layout.Document.close(Document.java:120) to [layout-7.0.2.jar:na]
at com.xcz.afbp.thirdparty.service.impl.GeneratePDFService.generatePDF (GeneratePDFService.java:160) to [classes/:na]
Code I generate
public void generatePDF (CreditQueryDataCreditQueryData, Map<String, UserCreditContentView>ContentViewMap, List<PackageCreditContentView>NeedRetrievedCreditCreditCreditCreditCreditCreditCreateCreatePDFile, etc.
if(!pdfFile.exists()){
boolean x=pdfFile.createNewFile();
if(!x){
LOG.error("Generated Statement Output" + pdfFile.getPath());
return;
}
}
PdfDocument pdf = new PdfDocument(new PdfWriter(newFileOutputStream(pdfFile)));
Document document = new Document (pdf, PageSize.A4);
document.setRenderer (new DocumentRenderer (document));
pdf.addEventHandler(PdfDocumentEvent.END_PAGE, new WatermarkingEventHandler());
try{
//operate code just add tableTableBtableC...
} catch(Exceptione){
LOG.info();
} US>finally
document.close(); // exception here
}
}
My style code for iText7 is
private PdfFont bfChinese=null;
Called in the service constructor.
I tried to set the font to static
but it didn't work. This is an exception to placethrow.
private void write (PdfIndirectReference indirectReference) {
if( document!=null&!indirectReference.getDocument().equals( document)){
through new PdfException (PdfException.PdfIndirectObjectBelongsToOtherPdfDocument);
}
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);
}
}
This means that there are two different documents, but you don't know when you created another document.
java pdf
A particular PdfFont
instance can only be used for one document; as soon as you use the PdfFont
instance, it is linked to that document and cannot be used in another document
For example,
class ThisGoesWrong {
protected PdfFont;
public ThisGoesWrong(){
font = PdfFontFactory.createFont(...);
}
public void createPdf(){
...
Paragrap = new Paragrap("test").setFont(font);
document.add(p);
...
}
}
The ThisGoesWrong
class creates the correct PDF when calling the first createPdf()
, but an exception appears when calling the second time.
This problem can be resolved as follows:
class ThisWorksOK {
public ThisWorksOK(){
}
public void createPdf(){
...
PdfFontfont=PdfFontFactory.createFont(...);
Paragrap = new Paragrap("test").setFont(font);
document.add(p);
...
}
}
You can use other third-party tools to create documents such as PDFs on components.
Tools to use: Spire.PDF for.NET
Detailed Code:
PdfDocument doc=newPdfDocument();
PdfPageBase page=newDoc.Pages.Add();
string message="Hello world!";
PdfFontfont=new PdfFont(PdfFontFamily.Helvetica, 13f);
PdfBrush brush = PdfBrush.Red;
PointFlocation=newPointF(20,20);
This allows you to successfully create PDF documents.
© 2024 OneMinuteCode. All rights reserved.