文章18 | 阅读 10873 | 点赞0
本章的例子,请参考我翻译的博文:itext7学习笔记——第7章,里面有详细的解释,有什么不懂得也可以评论或者私信我!
在第一章里面的fox和dog赛跑的基础上,我们创建PDF/UA标准的文档,代码如下:
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.utils.PdfMerger;
import com.itextpdf.pdfa.PdfADocument;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class C07E04_MergePDFADocuments {
public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm";
public static final String SRC1 = "src/main/resources/pdf/quick_brown_fox_PDFA-1a.pdf";
public static final String SRC2 = "src/main/resources/pdf/united_states_PDFA-1a.pdf";
public static final String DEST = "results/chapter07/merged_PDFA-1a_documents.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new C07E04_MergePDFADocuments().createPdf(DEST);
}
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)));
//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/A-1a example");
//Create PdfMerger instance
PdfMerger merger = new PdfMerger(pdf);
//Add pages from the first document
PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1));
merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages());
//Add pages from the second pdf document
PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2));
merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages());
//Close the documents
firstSourcePdf.close();
secondSourcePdf.close();
pdf.close();
}
}
PDF/A-1是ISO 19005的一部分,同时也有a和b之分,我们创建PDF/A-1b标准的文档,代码如下:
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
import com.itextpdf.kernel.pdf.PdfOutputIntent;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.pdfa.PdfADocument;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class C07E02_QuickBrownFox_PDFA_1b {
public static final String DOG = "src/main/resources/img/dog.bmp";
public static final String FOX = "src/main/resources/img/fox.bmp";
public static final String FONT = "src/main/resources/font/FreeSans.ttf";
public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm";
public static final String DEST = "results/chapter07/quick_brown_fox_PDFA-1b.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new C07E02_QuickBrownFox_PDFA_1b().createPdf(DEST);
}
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();
}
}
PDF/A-1是ISO 19005的一部分,同时也有a和b之分,我们创建PDF/A-1a标准的文档,代码如下:
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
import com.itextpdf.kernel.pdf.PdfOutputIntent;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.pdfa.PdfADocument;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class C07E02_QuickBrownFox_PDFA_1a {
public static final String DOG = "src/main/resources/img/dog.bmp";
public static final String FOX = "src/main/resources/img/fox.bmp";
public static final String FONT = "src/main/resources/font/FreeSans.ttf";
public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm";
public static final String DEST = "results/chapter07/quick_brown_fox_PDFA-1a.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new C07E02_QuickBrownFox_PDFA_1a().createPdf(DEST);
}
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();
}
}
PDF/A-2和PDF/A-3是ISO 19005的增强部分,3比2对附件没有格式限制,我们创建PDF/A-3a标准的文档,代码如下:
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.pdfa.PdfADocument;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.StringTokenizer;
public class C07E03_UnitedStates_PDFA_3a {
public static final String DATA = "src/main/resources/data/united_states.csv";
public static final String FONT = "src/main/resources/font/FreeSans.ttf";
public static final String BOLD_FONT = "src/main/resources/font/FreeSansBold.ttf";
public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm";
public static final String DEST = "results/chapter07/united_states_PDFA-3a.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new C07E03_UnitedStates_PDFA_3a().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
PdfAConformanceLevel.PDF_A_3A,
new PdfOutputIntent("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", new FileInputStream(INTENT)));
Document document = new Document(pdf, PageSize.A4.rotate());
document.setMargins(20, 20, 20, 20);
//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/A-3 example");
//Add attachment
PdfDictionary parameters = new PdfDictionary();
parameters.put(PdfName.ModDate, new PdfDate().getPdfObject());
PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(
pdf, Files.readAllBytes(Paths.get(DATA)), "united_states.csv",
"united_states.csv", new PdfName("text/csv"), parameters,
PdfName.Data, false);
fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
pdf.addFileAttachment("united_states.csv", fileSpec);
PdfArray array = new PdfArray();
array.add(fileSpec.getPdfObject().getIndirectReference());
pdf.getCatalog().put(new PdfName("AF"), array);
//Embed fonts
PdfFont font = PdfFontFactory.createFont(FONT, true);
PdfFont bold = PdfFontFactory.createFont(BOLD_FONT, true);
// Create content
Table table = new Table(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
table.setWidthPercent(100);
BufferedReader br = new BufferedReader(new FileReader(DATA));
String line = br.readLine();
process(table, line, bold, true);
while ((line = br.readLine()) != null) {
process(table, line, font, false);
}
br.close();
document.add(table);
//Close document
document.close();
}
public void process(Table table, String line, PdfFont font, boolean isHeader) {
StringTokenizer tokenizer = new StringTokenizer(line, ";");
while (tokenizer.hasMoreTokens()) {
if (isHeader) {
table.addHeaderCell(new Cell().setHorizontalAlignment(HorizontalAlignment.CENTER).add(new Paragraph(tokenizer.nextToken()).setHorizontalAlignment(HorizontalAlignment.CENTER).setFont(font)));
} else {
table.addCell(new Cell().setHorizontalAlignment(HorizontalAlignment.CENTER).add(new Paragraph(tokenizer.nextToken()).setHorizontalAlignment(HorizontalAlignment.CENTER).setFont(font)));
}
}
}
}
当拼接PDF/A文件的时候,最值得我们注意的是,我们拼接的各个文档必须都是PDF/A文件。在这里,我们把两个PDF/A-1A的文档拼接起来,代码如下:
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.utils.PdfMerger;
import com.itextpdf.pdfa.PdfADocument;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class C07E04_MergePDFADocuments {
public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm";
public static final String SRC1 = "src/main/resources/pdf/quick_brown_fox_PDFA-1a.pdf";
public static final String SRC2 = "src/main/resources/pdf/united_states_PDFA-1a.pdf";
public static final String DEST = "results/chapter07/merged_PDFA-1a_documents.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new C07E04_MergePDFADocuments().createPdf(DEST);
}
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)));
//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/A-1a example");
//Create PdfMerger instance
PdfMerger merger = new PdfMerger(pdf);
//Add pages from the first document
PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1));
merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages());
//Add pages from the second pdf document
PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2));
merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages());
//Close the documents
firstSourcePdf.close();
secondSourcePdf.close();
pdf.close();
}
}
本章代码可在如下地址下载(IDEA工程):iText7——第七章源代码工程
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/u012397189/article/details/78885790
内容来源于网络,如有侵权,请联系作者删除!