Hi all
I am trying to concatenate multiple pdf files which contain layers, but it
seems that the concatenated file loses the layers.
My code to concatenate is basically the same as the one below, except that it's
C#.
(problem described more in detail below the code)
------------------------------------------------------------------
* This class demonstrates copying a PDF file using iText.
* @author Mark Thompson
*/
package com.lowagie.tools;
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class concat_pdf extends java.lang.Object {
/**
* This class can be used to concatenate existing PDF files.
* (This was an example known as PdfCopy.java)
* @param args the command line arguments
*/
public static void main(String args[]) {
if (args.length < 2) {
System.err.println("arguments: file1 [file2 ...] destfile");
}
else {
try {
int f = 0;
String outFile = args[args.length-1];
Document document = null;
PdfCopy writer = null;
while (f < args.length-1) {
// we create a reader for a certain document
PdfReader reader = new PdfReader(args[f]);
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
System.out.println("There are " + n + " pages in " + args[f]);
if (f == 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 PdfCopy(document, new FileOutputStream(outFile));
// step 3: we open the document
document.open();
}
// step 4: we add content
PdfImportedPage page;
for (int i = 0; i < n; ) {
++i;
page = writer.getImportedPage(reader, i);
writer.addPage(page);
System.out.println("Processed page " + i);
}
PRAcroForm form = reader.getAcroForm();
if (form != null)
writer.copyAcroForm(reader);
f++;
}
// step 5: we close the document
document.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
------------------------------------------------------------------
After the concatenation, which I am able to do fine, I tried to add the layers
once again on each page by using the code below :
------------------------------------------------------------------
//Read teh concatenated file
PdfReader reader3 = new PdfReader(outFile);
Document document3 = new Document(reader3.GetPageSizeWithRotation(1));
PdfStamper stamp = null;
int n3 = reader3.NumberOfPages;
stamp = new PdfStamper(reader3, new FileStream("C:\\TEST.PDF",
System.IO.FileMode.CreateNew));
PdfWriter writer2 = stamp.Writer;
writer2.PdfVersion = '6'; // "PdfWriter.VERSION_1_6"; just to make sure I get a
version 6, apparently the PDFCopy object creates version 1.4
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI,
BaseFont.NOT_EMBEDDED);
PdfLayer baseL = new PdfLayer("BaseLayer", writer2);
PdfContentByte cb;
for (int i = 1; i <= n3; i++)
{
cb = stamp.GetOverContent(i);
baseL.OnPanel = true;
baseL.SetPrint("BaseLayer", false);
baseL.View = true;
cb.BeginText();
cb.SetTextMatrix(50, 790);
cb.SetLeading(24);
cb.SetFontAndSize(bf, 18);
cb.BeginLayer(baseL);
cb.NewlineShowText("This test is written to the layer only");
cb.EndLayer();
cb.EndText();
}
document3.Close();
reader3.Close();
stamp.Close();
------------------------------------------------------------------
Can you please help me out with this problem?
One more thing, If I open the concatenated file in a text editor and I search
for the layer name (BaseLayer in my case) I do see it there, but it does not
show in Acrobat. How can I concatenate files and keep the layers the original
files had?
Thanks
Chris
--------------------------------------------------------
THIS COMMUNICATION IS INTENDED ONLY FOR THE USE OF THE INDIVIDUAL OR ENTITY TO
WHICH IT IS ADDRESSED AND CONTAINS OR MAY CONTAIN INFORMATION THAT IS
PRIVILEGED, CONFIDENTIAL OR EXEMPT FROM DISCLOSURE UNDER APPLICABLE LAW. If
the reader of this communication is not the intended recipient (or the employee
or agent responsible for delivering to the intended recipient), you are hereby
notified that any dissemination, distribution, or copying of this communication
is strictly prohibited. If you have received this communication in error,
please disregard and delete this communication. Do not disseminate or retain
any copy of this communication, or any attachments.
--------------------------------------------------------
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/