Fonts and merging documents

This is a code example of iText PDF, discover more.

6th November 2015
admin-marketing

Switch code for this example

MergeAndAddFont.java
  1. package sandbox.fonts;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Font;
  6. import com.itextpdf.text.Paragraph;
  7. import com.itextpdf.text.pdf.BaseFont;
  8. import com.itextpdf.text.pdf.PdfCopy;
  9. import com.itextpdf.text.pdf.PdfDictionary;
  10. import com.itextpdf.text.pdf.PdfIndirectObject;
  11. import com.itextpdf.text.pdf.PdfName;
  12. import com.itextpdf.text.pdf.PdfNumber;
  13. import com.itextpdf.text.pdf.PdfObject;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfSmartCopy;
  16. import com.itextpdf.text.pdf.PdfStamper;
  17. import com.itextpdf.text.pdf.PdfStream;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19. import java.io.File;
  20.  
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.RandomAccessFile;
  24.  
  25. public class MergeAndAddFont {
  26.  
  27.     public static final String FONT = "resources/fonts/GravitasOne.ttf";
  28.     public static final String[] FILE_A = {
  29.         "results/fonts/testA1.pdf", "results/fonts/testA2.pdf", "results/fonts/testA3.pdf"
  30.     };
  31.     public static final String[] FILE_B = {
  32.         "results/fonts/testB1.pdf", "results/fonts/testB2.pdf", "results/fonts/testB3.pdf"
  33.     };
  34.     public static final String[] FILE_C = {
  35.         "results/fonts/testC1.pdf", "results/fonts/testC2.pdf", "results/fonts/testC3.pdf"
  36.     };
  37.     public static final String[] CONTENT = {
  38.         "abcdefgh", "ijklmnopq", "rstuvwxyz"
  39.     };
  40.     public static final String MERGED_A1 = "results/fonts/testA_merged1.pdf";
  41.     public static final String MERGED_A2 = "results/fonts/testA_merged2.pdf";
  42.     public static final String MERGED_B1 = "results/fonts/testB_merged1.pdf";
  43.     public static final String MERGED_B2 = "results/fonts/testB_merged2.pdf";
  44.     public static final String MERGED_C1 = "results/fonts/testC_merged1.pdf";
  45.     public static final String MERGED_C2 = "results/fonts/testC_merged2.pdf";
  46.    
  47.     public static void main(String[] args) throws DocumentException, IOException {
  48.         File file = new File(MERGED_A1);
  49.         file.getParentFile().mkdirs();
  50.         MergeAndAddFont app = new MergeAndAddFont();
  51.         for (int i = 0; i < FILE_A.length; i++) {
  52.             app.createPdf(FILE_A[i], CONTENT[i], true, true);
  53.         }
  54.         app.mergeFiles(FILE_A, MERGED_A1,false);
  55.         app.mergeFiles(FILE_A, MERGED_A2, true);
  56.         for (int i = 0; i < FILE_B.length; i++) {
  57.             app.createPdf(FILE_B[i], CONTENT[i], true, false);
  58.         }
  59.         app.mergeFiles(FILE_B, MERGED_B1,false);
  60.         app.mergeFiles(FILE_B, MERGED_B2, true);
  61.         for (int i = 0; i < FILE_C.length; i++) {
  62.             app.createPdf(FILE_C[i], CONTENT[i], false, false);
  63.         }
  64.         app.mergeFiles(FILE_C, MERGED_C1, true);
  65.         app.embedFont(MERGED_C1, FONT, MERGED_C2);
  66.     }
  67.  
  68.     public void createPdf(String filename, String text, boolean embedded, boolean subset) throws DocumentException, IOException {
  69.         // step 1
  70.         Document document = new Document();
  71.         // step 2
  72.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  73.         // step 3
  74.         document.open();
  75.         // step 4
  76.         BaseFont bf = BaseFont.createFont(FONT, BaseFont.WINANSI, embedded);
  77.         bf.setSubset(subset);
  78.         Font font = new Font(bf, 12);
  79.         document.add(new Paragraph(text, font));
  80.         // step 5
  81.         document.close();
  82.     }
  83.    
  84.     public void mergeFiles(String[] files, String result, boolean smart) throws IOException, DocumentException {
  85.         Document document = new Document();
  86.         PdfCopy copy;
  87.         if (smart)
  88.             copy = new PdfSmartCopy(document, new FileOutputStream(result));
  89.         else
  90.             copy = new PdfCopy(document, new FileOutputStream(result));
  91.         document.open();
  92.         PdfReader[] reader = new PdfReader[3];
  93.         for (int i = 0; i < files.length; i++) {
  94.             reader[i] = new PdfReader(files[i]);
  95.             copy.addDocument(reader[i]);
  96.             copy.freeReader(reader[i]);
  97.             reader[i].close();
  98.         }
  99.         document.close();
  100.     }
  101.    
  102.     private void embedFont(String merged, String fontfile, String result) throws IOException, DocumentException {
  103.         // the font file
  104.         RandomAccessFile raf = new RandomAccessFile(fontfile, "r");
  105.         byte fontbytes[] = new byte[(int)raf.length()];
  106.         raf.readFully(fontbytes);
  107.         raf.close();
  108.         // create a new stream for the font file
  109.         PdfStream stream = new PdfStream(fontbytes);
  110.         stream.flateCompress();
  111.         stream.put(PdfName.LENGTH1, new PdfNumber(fontbytes.length));
  112.         // create a reader object
  113.         PdfReader reader = new PdfReader(merged);
  114.         int n = reader.getXrefSize();
  115.         PdfObject object;
  116.         PdfDictionary font;
  117.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(result));
  118.         PdfName fontname = new PdfName(BaseFont.createFont(fontfile, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED).getPostscriptFontName());
  119.         for (int i = 0; i < n; i++) {
  120.             object = reader.getPdfObject(i);
  121.             if (object == null || !object.isDictionary())
  122.                 continue;
  123.             font = (PdfDictionary)object;
  124.             if (PdfName.FONTDESCRIPTOR.equals(font.get(PdfName.TYPE))
  125.                 && fontname.equals(font.get(PdfName.FONTNAME))) {
  126.                 PdfIndirectObject objref = stamper.getWriter().addToBody(stream);
  127.                 font.put(PdfName.FONTFILE2, objref.getIndirectReference());
  128.             }
  129.         }
  130.         stamper.close();
  131.         reader.close();
  132.     }
  133. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now