Hi,
while investigating some problems with concatenation of pdf files the
question came up if it is neccessary to recreate the
"font-subset-prefix" to prevent a name clash. I do not see a obvious
problem not doing this in the pdf reference manual. Acrobat does not
seem to care. If you run the attached Java class two nearly identical
pdf files will be generated "StandardType1Fonts1.pdf" and
"StandardType1Fonts2.pdf". In another step these two files are joined in
another file "out.pdf". There seems to be no prefix processing.
PDFSmartcopy throws out one copy of the font. The last file "out2.pdf"
is created by copying "StandardType1Fonts1.pdf" twice into the same
file. Because of using the same input file twice the prefix is always
the same. There is no big difference - Acrobat shows only one font
because of the same name.
Are there any known problems using itext's PDFCopy/PDFSmartCopy to merge
pdf files?
Best regards,
Carsten
package com.lowagie.examples;
import java.io.*;
import java.util.*;
import java.util.List;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.*;
public class CreatetwofilesandConcat {
public CreatetwofilesandConcat() {
boolean embed=BaseFont.EMBEDDED;
Document.compress = false;
final String file1 = "StandardType1Fonts1.pdf";
createTestfile(file1,embed);
final String file2 = "StandardType1Fonts2.pdf";
createTestfile(file2,embed);
// Here the truetype font subset of each file has two different prefixes
ConcatTwoFiles(file1, file2, "out.pdf");
// Here the truetype font subset of each file has equal prefixes
ConcatTwoFiles(file1, file1, "out2.pdf");
}
private void ConcatTwoFiles(String file1, String file2, String file3) {
try {
String[] files = new String[2];
files[0] = file1;
files[1] = file2;
int pageOffset = 0;
List<HashMap<String, Object>>
master = new ArrayList<HashMap<String, Object>> ();
Document document = null;
PdfCopy writer = null;
for (int i = 0; i < 2; i++) {
// we create a reader for a certain document
PdfReader reader = new PdfReader(files[i]);
reader.consolidateNamedDestinations();
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
List<HashMap<String, Object>>
bookmarks = SimpleBookmark.getBookmark(reader);
if (bookmarks != null) {
if (pageOffset != 0) {
SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
}
master.addAll(bookmarks);
}
pageOffset += n;
System.out.println("There are " + n + " pages in " + files[i]);
if (i == 0) {
// step 1: creation of a document-object
document = new Document(reader.getPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
writer = new PdfSmartCopy(document, new FileOutputStream(file3));
// writer = new PdfCopy(document, new FileOutputStream(file3));
// step 3: we open the document
document.open();
}
// step 4: we add content
PdfImportedPage page;
for (int p = 0; p < n; ) {
++p;
page = writer.getImportedPage(reader, p);
writer.addPage(page);
System.out.println("Processed page " + p);
}
}
if (!master.isEmpty()) {
writer.setOutlines(master);
}
// step 5: we close the document
document.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void createTestfile(String outputfilename, boolean embed) {
System.out.println(outputfilename);
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
PdfWriter.getInstance(document,
new FileOutputStream(outputfilename));
// step 3: we open the document
document.open();
// step 4:
// the 14 standard fonts in PDF: do not use this Font constructor!
// this is for demonstration purposes only, use FontFactory!
Font[] fonts = new Font[3];
fonts[0] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.NORMAL);
fonts[1] = new Font(Font.SYMBOL);
fonts[2] = new Font(Font.ZAPFDINGBATS);
// add the content
for (int i = 0; i < 3; i++) {
document.add(new Paragraph("quick brown fox jumps over the lazy dog",
fonts[i]));
}
BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\comicbd.ttf",
BaseFont.CP1252,
embed);
Font font = new Font(bfComic, 12);
String text1 = "This is the quite popular True Type font 'Comic'.";
document.add(new Paragraph(text1, font));
}
catch (DocumentException de) {
System.err.println(de.getMessage());
}
catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
public static void main(String[] args) {
CreatetwofilesandConcat ct = new CreatetwofilesandConcat();
};
}
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/