Java源码示例:com.itextpdf.layout.element.Image
示例1
public void createPdf(String dest) throws IOException {
//Initialize PDFA document with output intent
PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
PdfAConformanceLevel.PDF_A_1B,
new PdfOutputIntent("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", new FileInputStream(INTENT)));
Document document = new Document(pdf);
//Fonts need to be embedded
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.setFont(font);
p.add(new Text("The quick brown "));
Image foxImage = new Image(ImageDataFactory.create(FOX));
p.add(foxImage);
p.add(" jumps over the lazy ");
Image dogImage = new Image(ImageDataFactory.create(DOG));
p.add(dogImage);
document.add(p);
document.close();
}
示例2
public void createPdf(String dest) throws IOException {
//Initialize PDF writer
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(writer);
// Initialize document
Document document = new Document(pdf);
// Compose Paragraph
Image fox = new Image(ImageDataFactory.create(FOX));
Image dog = new Image(ImageDataFactory.create(DOG));
Paragraph p = new Paragraph("The quick brown ")
.add(fox)
.add(" jumps over the lazy ")
.add(dog);
// Add Paragraph to document
document.add(p);
//Close document
document.close();
}
示例3
public void createPdf(String dest) throws IOException {
//Initialize PDF writer
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(writer);
// Initialize document
Document document = new Document(pdf);
// Compose Paragraph
Image fox = new Image(ImageDataFactory.create(FOX));
Image dog = new Image(ImageDataFactory.create(DOG));
Paragraph p = new Paragraph("The quick brown ")
.add(fox)
.add(" jumps over the lazy ")
.add(dog);
// Add Paragraph to document
document.add(p);
//Close document
document.close();
}
示例4
public void createPdf(String dest) throws IOException {
//Initialize PDFA document with output intent
PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
PdfAConformanceLevel.PDF_A_1A,
new PdfOutputIntent("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", new FileInputStream(INTENT)));
Document document = new Document(pdf);
//Setting some required parameters
pdf.setTagged();
//Fonts need to be embedded
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.setFont(font);
p.add(new Text("The quick brown "));
Image foxImage = new Image(ImageDataFactory.create(FOX));
//Set alt text
foxImage.getAccessibilityProperties().setAlternateDescription("Fox");
p.add(foxImage);
p.add(" jumps over the lazy ");
Image dogImage = new Image(ImageDataFactory.create(DOG));
//Set alt text
dogImage.getAccessibilityProperties().setAlternateDescription("Dog");
p.add(dogImage);
document.add(p);
document.close();
}
示例5
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest, new WriterProperties().addXmpMetadata()));
Document document = new Document(pdf);
//Setting some required parameters
pdf.setTagged();
pdf.getCatalog().setLang(new PdfString("en-US"));
pdf.getCatalog().setViewerPreferences(
new PdfViewerPreferences().setDisplayDocTitle(true));
PdfDocumentInfo info = pdf.getDocumentInfo();
info.setTitle("iText7 PDF/UA example");
//Fonts need to be embedded
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.setFont(font);
p.add(new Text("The quick brown "));
Image foxImage = new Image(ImageDataFactory.create(FOX));
//PDF/UA: Set alt text
foxImage.getAccessibilityProperties().setAlternateDescription("Fox");
p.add(foxImage);
p.add(" jumps over the lazy ");
Image dogImage = new Image(ImageDataFactory.create(DOG));
//PDF/UA: Set alt text
dogImage.getAccessibilityProperties().setAlternateDescription("Dog");
p.add(dogImage);
document.add(p);
document.close();
}
示例6
public void createPdf(String dest) throws IOException {
//Initialize PDFA document with output intent
PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
PdfAConformanceLevel.PDF_A_1A,
new PdfOutputIntent("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", new FileInputStream(INTENT)));
Document document = new Document(pdf);
//Setting some required parameters
pdf.setTagged();
//Fonts need to be embedded
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.setFont(font);
p.add(new Text("The quick brown "));
Image foxImage = new Image(ImageDataFactory.create(FOX));
//Set alt text
foxImage.getAccessibilityProperties().setAlternateDescription("Fox");
p.add(foxImage);
p.add(" jumps over the lazy ");
Image dogImage = new Image(ImageDataFactory.create(DOG));
//Set alt text
dogImage.getAccessibilityProperties().setAlternateDescription("Dog");
p.add(dogImage);
document.add(p);
document.close();
}
示例7
public void createPdf(String dest) throws IOException {
//Initialize PDFA document with output intent
PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
PdfAConformanceLevel.PDF_A_1B,
new PdfOutputIntent("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", new FileInputStream(INTENT)));
Document document = new Document(pdf);
//Fonts need to be embedded
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.setFont(font);
p.add(new Text("The quick brown "));
Image foxImage = new Image(ImageDataFactory.create(FOX));
p.add(foxImage);
p.add(" jumps over the lazy ");
Image dogImage = new Image(ImageDataFactory.create(DOG));
p.add(dogImage);
document.add(p);
document.close();
}
示例8
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest, new WriterProperties().addXmpMetadata()));
Document document = new Document(pdf);
//Setting some required parameters
pdf.setTagged();
pdf.getCatalog().setLang(new PdfString("en-US"));
pdf.getCatalog().setViewerPreferences(
new PdfViewerPreferences().setDisplayDocTitle(true));
PdfDocumentInfo info = pdf.getDocumentInfo();
info.setTitle("iText7 PDF/UA example");
//Fonts need to be embedded
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.setFont(font);
p.add(new Text("The quick brown "));
Image foxImage = new Image(ImageDataFactory.create(FOX));
//PDF/UA: Set alt text
foxImage.getAccessibilityProperties().setAlternateDescription("Fox");
p.add(foxImage);
p.add(" jumps over the lazy ");
Image dogImage = new Image(ImageDataFactory.create(DOG));
//PDF/UA: Set alt text
dogImage.getAccessibilityProperties().setAlternateDescription("Dog");
p.add(dogImage);
document.add(p);
document.close();
}
示例9
/**
* 将图片合成为一个PDF
* @param inputImage 图片,格式为图片格式
* @param outputPDF 输出文件
* @throws FileNotFoundException
* @throws MalformedURLException
*/
public static void generatePDFFromImage(File[] inputImage,File outputPDF) throws FileNotFoundException, MalformedURLException {
List<Image>images=new LinkedList<>();
for(File file:inputImage){
images.add(new Image(ImageDataFactory.create(file.getPath())));
}
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputPDF.getPath()));
images.forEach(image -> pdfDoc.addNewPage(new PageSize(new Rectangle(image.getImageScaledWidth(), image.getImageScaledHeight()))));
BackgroundEventHandler handler = new BackgroundEventHandler(images);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, handler);
pdfDoc.close();
}
示例10
public static void generatePDFFromImage(File[] inputImage,File outputPDF,Book book) throws FileNotFoundException{
List<File> sorted = Arrays.asList(inputImage);
Collections.sort(sorted, Comparator.comparing(File::getName));
inputImage=sorted.toArray(new File[]{});
List<Image>images=new LinkedList<>();
boolean hasException=false;
for(File file:inputImage){
try {
images.add(new Image(ImageDataFactory.create(file.getPath())));
} catch (MalformedURLException e) {
System.err.println(file.getPath());
e.printStackTrace();
} catch (com.itextpdf.io.IOException eee){
hasException=true;
}
}
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputPDF.getPath()));
PdfDocumentInfo info=pdfDoc.getDocumentInfo();
if(book.getName()!=null&&!book.getName().equals("null")){
info.setTitle(book.getName());
}
if(book.getAuthor()!=null&&!book.getAuthor().equals("null")){
info.setAuthor(book.getAuthor());
}
if(book.getTheme()!=null&&!book.getTheme().equals("null")){
info.setSubject(book.getTheme());
}
StringBuffer keyword=new StringBuffer();
if(book.getPublishDate()!=null&&!book.getPublishDate().equals("null")){
keyword.append("出版时间:"+book.getPublishDate()+"\n");
}
if(book.getBookClass()!=null&&!book.getBookClass().equals("null")){
keyword.append("分类:"+book.getDetailBookClass().replaceAll("图书馆",""));
}
info.setKeywords(keyword.toString());
if(hasException){
System.err.println(book.getName()+" 图片格式异常");
info.setCreator("exception");
}
images.forEach(image -> pdfDoc.addNewPage(new PageSize(new Rectangle(image.getImageScaledWidth(), image.getImageScaledHeight()))));
BackgroundEventHandler handler = new BackgroundEventHandler(images);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, handler);
pdfDoc.close();
}
示例11
public BackgroundEventHandler(List<Image> images) {
this.images = images;
}
示例12
@Override
public void render(@NotNull Document reportDocument) throws IOException {
String circosPath = patientReport.circosPath();
try {
Image circosImage = new Image(ImageDataFactory.create(circosPath));
circosImage.setMaxHeight(400);
circosImage.setHorizontalAlignment(HorizontalAlignment.CENTER);
circosImage.setMarginBottom(8);
reportDocument.add(circosImage);
} catch (MalformedURLException e) {
throw new IOException("Failed to read circos plot image at " + circosPath);
}
Table table = new Table(UnitValue.createPercentArray(new float[] { 10, 1, 10, 1, 10 }));
table.setWidth(contentWidth());
table.addCell(TableUtil.createLayoutCell()
.add(new Div().add(createContentParagraph("The outer first circle",
" shows the chromosomes. The darker shaded areas represent large gaps in the human reference genome: "
+ "i.e. regions of centromeres, heterochromatin & missing short arms."))
.add(createContentParagraph("The second circle ",
"shows all tumor specific variants (incl. exon, intron and intergenic regions) and "
+ "are divided into an outer ring of single nucleotide polymorphism (SNP) allele "
+ "frequencies and an inner ring of short insertion/deletion (INDEL) locations. Variant allele "
+ "frequencies have been corrected for tumor purity and scale from 0 to 100%. Each dot represents "
+ "a single variant and are colored according to the type of base change "
+ "(e.g. C>T/G>A in red) and are in concordance with the coloring used in Alexandrov et al. 2013 "
+ "Nature paper that describes the use of mutational signatures. "
+ "INDELs are colored yellow and red for insertions and deletions respectively."))));
table.addCell(TableUtil.createLayoutCell());
table.addCell(TableUtil.createLayoutCell()
.add(new Div().add(createContentParagraph("The third circle",
" shows all observed tumor purity adjusted "
+ "copy number changes, including both focal and chromosomal events. Copy number losses are "
+ "indicated in red, green shows regions of copy number gain. The scale ranges from 0 (complete loss) "
+ "to 6 (high level gains). If the absolute copy number is > 6 it is shown as 6 with a green dot on "
+ "the diagram.").add(createContentParagraph("The fourth circle",
" represents the observed 'minor allele "
+ "copy numbers’ across the chromosome. The range of the chart is from 0 to 3. The expected normal "
+ "minor allele copy number is 1, and anything below 1 is shown as a loss and represents a "
+ "LOH event (orange). Minor allele copy numbers above 1 indicate amplification events of both A and B "
+ "alleles at the indicated locations (blue).")))));
table.addCell(TableUtil.createLayoutCell());
table.addCell(TableUtil.createLayoutCell()
.add(new Div().add(createContentParagraph("The innermost circle",
" displays the observed structural "
+ "variants within or between the chromosomes. Translocations are indicated in blue, deletions in "
+ "red, insertions in yellow, tandem duplications in green and inversions in black."))));
reportDocument.add(table);
}