Description: Use external libitext-java and headers generated by gjavah
Author: Jochen Schmitt <Jochen@herr-schmitt.de> (modified for Debian by Johann Felix Soden <johfel@gmx.de>

--- a/pdftk/attachments.cc
+++ b/pdftk/attachments.cc
@@ -51,7 +51,11 @@
 
 #include "com/lowagie/text/Document.h"
 #include "com/lowagie/text/Rectangle.h"
+// Ewww, PdfName has a field called NULL.
+#undef NULL
 #include "com/lowagie/text/pdf/PdfName.h"
+#define NULL __null
+
 #include "com/lowagie/text/pdf/PdfString.h"
 #include "com/lowagie/text/pdf/PdfNumber.h"
 #include "com/lowagie/text/pdf/PdfArray.h"
@@ -148,8 +152,8 @@
 
 					itext::Rectangle* crop_box_p= 
 						input_reader_p->getCropBox( m_input_attach_file_pagenum );
-					float corner_top= crop_box_p->top()- margin;
-					float corner_left= crop_box_p->left()+ margin;
+					float corner_top= crop_box_p->getTop()- margin;
+					float corner_left= crop_box_p->getLeft()+ margin;
 
 					itext::PdfArray* annots_p= (itext::PdfArray*)
 						input_reader_p->getPdfObject( page_p->get( itext::PdfName::ANNOTS ) );
@@ -187,11 +191,11 @@
 								string filename= drop_path(*vit);
 
 								// wrap our location over page bounds, if needed
-								if( crop_box_p->right() < corner_left+ trans ) {
-									corner_left= crop_box_p->left()+ margin;
+								if( crop_box_p->getRight() < corner_left+ trans ) {
+									corner_left= crop_box_p->getLeft()+ margin;
 								}
-								if( corner_top- trans< crop_box_p->bottom() ) {
-									corner_top= crop_box_p->top()- margin;
+								if( corner_top- trans< crop_box_p->getBottom() ) {
+									corner_top= crop_box_p->getTop()- margin;
 								}
 
 								itext::Rectangle* annot_bbox_p= 
--- a/pdftk/Makefile.Base
+++ b/pdftk/Makefile.Base
@@ -15,34 +15,12 @@
 # them into fonts.a resulted in none of these resources appearing in pdftk
 # (on MinGW gcj 3.4.1)
 #
-itext_libs= \
-$(java_libs_root)/com/lowagie/text/pdf/pdf.a \
-$(java_libs_root)/com/lowagie/text/text.a \
-$(java_libs_root)/com/lowagie/text/pdf/pdf.a \
-$(java_libs_root)/com/lowagie/text/pdf/codec/codec.a \
-$(java_libs_root)/com/lowagie/text/pdf/codec/wmf/wmf.a \
-$(java_libs_root)/com/lowagie/text/pdf/fonts/fonts.a \
-$(java_libs_root)/com/lowagie/text/markup/markup.a \
-$(java_libs_root)/com/lowagie/text/xml/xmp/xmp.a \
-$(java_libs_root)/com/lowagie/bc/asn1/asn1.a \
-$(java_libs_root)/com/lowagie/text/pdf/codec/postscript/postscript.a \
+itext_libs=$(GCJ_ITEXT_SO)
 
 # font metric files, included as resources
 afms= $(wildcard $(java_libs_root)/com/lowagie/text/pdf/fonts/*.afm)
 afm_objects= $(patsubst %.afm, %.o, $(afms))
 
-# older versions of libgcj might not have the MD5 algorithm,
-# so I added it here; these *_local java files were grabbed from
-# libgcj CVS on March 7, 2004; diffed September 5, 2006 w/ gcc 4.1.1
-#
-# gnu/gcj/convert/Input_UnicodeBig.java was grabbed March 26, 2004; diffed September 5, 2006 w/ gcc 4.1.1
-#
-libgcj_local_libs = \
-$(java_libs_root)/java_local/security/security.a \
-$(java_libs_root)/gnu_local/java/security/provider/provider.a \
-$(java_libs_root)/gnu_local/java/security/security.a \
-$(java_libs_root)/gnu/gcj/convert/convert.a
-
 # this must already be set according to your platform Makefile;
 # we're just appending to it, here
 #
@@ -51,19 +29,18 @@
 all : java_libs pdftk
 
 java_libs :
-	$(MAKE) -C $(java_libs_root)
 
-attachments.o : attachments.cc attachments.h pdftk.h $(itext_libs) $(libgcj_local_libs)
-	$(CXX) attachments.cc -I$(java_libs_root) $(CPPFLAGS) -c
+attachments.o : attachments.cc attachments.h pdftk.h
+	$(CXX) attachments.cc -I$(java_libs_root) $(CXXFLAGS) $(CPPFLAGS) -c
 
-report.o : report.cc report.h pdftk.h $(itext_libs) $(libgcj_local_libs)
-	$(CXX) report.cc -I$(java_libs_root) $(CPPFLAGS) -c
+report.o : report.cc report.h pdftk.h
+	$(CXX) report.cc -I$(java_libs_root) $(CXXFLAGS) $(CPPFLAGS)  -c
 
-pdftk.o : pdftk.cc pdftk.h attachments.h report.h $(itext_libs) $(libgcj_local_libs)
-	$(CXX) pdftk.cc -I$(java_libs_root) $(CPPFLAGS) -c
+pdftk.o : pdftk.cc pdftk.h attachments.h report.h
+	$(CXX) pdftk.cc -I$(java_libs_root) $(CXXFLAGS) $(CPPFLAGS)  -c
 
-pdftk : pdftk.o attachments.o report.o $(itext_libs) $(libgcj_local_libs) $(afm_objects)
-	$(CXX) pdftk.o attachments.o report.o $(itext_libs) $(libgcj_local_libs) $(afm_objects) -I$(java_libs_root) $(CPPFLAGS) $(CXXFLAGS) -o pdftk
+pdftk : pdftk.o attachments.o report.o $(afm_objects)
+	$(GCJ) pdftk.o attachments.o report.o $(itext_libs) $(afm_objects) -I$(java_libs_root) $(GCJFLAGS) -lstdc++ -o pdftk
 
 install:
 	/usr/bin/install pdftk /usr/local/bin 
--- a/pdftk/pdftk.cc
+++ b/pdftk/pdftk.cc
@@ -50,7 +50,12 @@
 
 #include "com/lowagie/text/Document.h"
 #include "com/lowagie/text/Rectangle.h"
+
+// Ewww, PdfName has a field called NULL.
+#undef NULL
 #include "com/lowagie/text/pdf/PdfName.h"
+#define NULL __null
+
 #include "com/lowagie/text/pdf/PdfString.h"
 #include "com/lowagie/text/pdf/PdfNumber.h"
 #include "com/lowagie/text/pdf/PdfArray.h"
@@ -191,18 +196,24 @@
 		// store in this java object so the gc can trace it
 		g_dont_collect_p->addElement( reader );
 
-		input_pdf_p->m_authorized_b= ( !reader->encrypted || reader->passwordIsOwner );
+		input_pdf_p->m_authorized_b= reader->isOpenedWithFullPermissions();
 		if( !input_pdf_p->m_authorized_b ) {
 			open_success_b= false;
 		}
 	}
 	catch( java::io::IOException* ioe_p ) { // file open error
-		if( ioe_p->getMessage()->equals( JvNewStringLatin1( "Bad password" ) ) ) {
+		if( ioe_p->getMessage()->equals( JvNewStringLatin1( "Bad user password" ) ) ) {
 			input_pdf_p->m_authorized_b= false;
 		}
+		else {
+			cerr << string((const char*) elements(ioe_p->getMessage()->getBytes()),
+					(int) ioe_p->getMessage()->getBytes()->length)
+			  	<< endl;
+		}
 		open_success_b= false;
 	}
 	catch( java::lang::Throwable* t_p ) { // unexpected error
+		t_p->printStackTrace();
 		cerr << "Error: Unexpected Exception in open_reader()" << endl;
 		open_success_b= false;
 							
@@ -2017,14 +2028,14 @@
 				output_doc_p->addCreator( jv_creator_p );
 
 				// un/compress output streams?
-				if( m_output_uncompress_b ) {
-					writer_p->filterStreams= true;
-					writer_p->compressStreams= false;
-				}
-				else if( m_output_compress_b ) {
-					writer_p->filterStreams= false;
-					writer_p->compressStreams= true;
-				}
+// 				if( m_output_uncompress_b ) {
+// 					writer_p->filterStreams= true;
+// 					writer_p->compressStreams= false;
+// 				}
+// 				else if( m_output_compress_b ) {
+// 					writer_p->filterStreams= false;
+// 					writer_p->compressStreams= true;
+// 				}
 
 				// encrypt output?
 				if( m_output_encryption_strength!= none_enc ||
@@ -2033,7 +2044,7 @@
 					{
 						// if no stregth is given, default to 128 bit,
 						// (which is incompatible w/ Acrobat 4)
-						bool bit128_b=
+						jboolean bit128_b=
 							( m_output_encryption_strength!= bits40_enc );
 
 						writer_p->setEncryption( output_user_pw_p,
@@ -2057,7 +2068,8 @@
 							input_reader_p->getPdfObject( trailer_p->get( itext::PdfName::ID ) );
 						if( file_id_p && file_id_p->isArray() ) {
 
-							writer_p->setFileID( file_id_p );
+							// Absent from itext-2.1.4
+// 							writer_p->setFileID( file_id_p );
 						}
 					}
 
@@ -2168,12 +2180,14 @@
 
 					// un/compress output streams?
 					if( m_output_uncompress_b ) {
-						writer_p->filterStreams= true;
-						writer_p->compressStreams= false;
+						// Absent from itext-2.1.4
+// 						writer_p->filterStreams= true;
+// 						writer_p->compressStreams= false;
 					}
 					else if( m_output_compress_b ) {
-						writer_p->filterStreams= false;
-						writer_p->compressStreams= true;
+						// Absent from itext-2.1.4
+// 						writer_p->filterStreams= false;
+// 						writer_p->compressStreams= true;
 					}
 
 					// encrypt output?
@@ -2183,7 +2197,7 @@
 						{
 							// if no stregth is given, default to 128 bit,
 							// (which is incompatible w/ Acrobat 4)
-							bool bit128_b=
+							jboolean bit128_b=
 								( m_output_encryption_strength!= bits40_enc );
 
 							writer_p->setEncryption( output_user_pw_p,
@@ -2403,13 +2417,15 @@
 				// un/compress output streams?
 				if( m_output_uncompress_b ) {
 					add_marks_to_pages( input_reader_p );
-					writer_p->filterStreams= true;
-					writer_p->compressStreams= false;
+					// Absent from itext-2.1.4
+// 					writer_p->filterStreams= true;
+// 					writer_p->compressStreams= false;
 				}
 				else if( m_output_compress_b ) {
 					remove_marks_from_pages( input_reader_p );
-					writer_p->filterStreams= false;
-					writer_p->compressStreams= true;
+					// Absent from itext-2.1.4
+// 					writer_p->filterStreams= false;
+// 					writer_p->compressStreams= true;
 				}
 
 				// encrypt output?
@@ -2420,13 +2436,13 @@
 
 						// if no stregth is given, default to 128 bit,
 						// (which is incompatible w/ Acrobat 4)
-						bool bit128_b=
+						jboolean bit128_b=
 							( m_output_encryption_strength!= bits40_enc );
 
 						writer_p->setEncryption( output_user_pw_p,
 																			output_owner_pw_p,
 																			m_output_user_perms,
-																			bit128_b );
+																		  bit128_b );
 					}
 
 				// fill form fields?
@@ -2435,8 +2451,11 @@
 					{
 						itext::AcroFields* fields_p= writer_p->getAcroFields();
 						fields_p->setGenerateAppearances( true ); // have iText create field appearances
-						if( fdf_reader_p && fields_p->setFields( fdf_reader_p ) ||
-								xfdf_reader_p && fields_p->setFields( xfdf_reader_p ) )
+						if( fdf_reader_p ) 
+							fields_p->setFields( fdf_reader_p );
+						if(	xfdf_reader_p )
+							fields_p->setFields( xfdf_reader_p );
+
 							{ // Rich Text input found
 
 								// set the PDF so that Acrobat will create appearances;
@@ -2486,16 +2505,16 @@
 							doc_page_size_p= doc_page_size_p->rotate();
 						}
 
-						jfloat h_scale= doc_page_size_p->width() / mark_page_size_p->width();
-						jfloat v_scale= doc_page_size_p->height() / mark_page_size_p->height();
+						jfloat h_scale= doc_page_size_p->getWidth() / mark_page_size_p->getWidth();
+						jfloat v_scale= doc_page_size_p->getHeight() / mark_page_size_p->getHeight();
 						jfloat mark_scale= (h_scale< v_scale) ? h_scale : v_scale;
 
-						jfloat h_trans= (jfloat)(doc_page_size_p->left()- mark_page_size_p->left()* mark_scale +
-																		 (doc_page_size_p->width()- 
-																			mark_page_size_p->width()* mark_scale) / 2.0);
-						jfloat v_trans= (jfloat)(doc_page_size_p->bottom()- mark_page_size_p->bottom()* mark_scale +
-																		 (doc_page_size_p->height()- 
-																			mark_page_size_p->height()* mark_scale) / 2.0);
+						jfloat h_trans= (jfloat)(doc_page_size_p->getLeft()- mark_page_size_p->getLeft()* mark_scale +
+																		 (doc_page_size_p->getWidth()- 
+																			mark_page_size_p->getWidth()* mark_scale) / 2.0);
+						jfloat v_trans= (jfloat)(doc_page_size_p->getBottom()- mark_page_size_p->getBottom()* mark_scale +
+																		 (doc_page_size_p->getHeight()- 
+																			mark_page_size_p->getHeight()* mark_scale) / 2.0);
           
 						com::lowagie::text::pdf::PdfContentByte* content_byte_p= 
 							( background_b ) ? writer_p->getUnderContent( ii ) : writer_p->getOverContent( ii );
@@ -2512,20 +2531,20 @@
 																					 0, -1* mark_scale,
 																					 mark_scale, 0,
 																					 h_trans, 
-																					 v_trans+ mark_page_size_p->height()* mark_scale );
+																					 v_trans+ mark_page_size_p->getHeight()* mark_scale );
 						}
 						else if( mark_page_rotation== 180 ) {
 							content_byte_p->addTemplate( mark_page_p, 
 																					 -1* mark_scale, 0,
 																					 0, -1* mark_scale,
-																					 h_trans+ mark_page_size_p->width()* mark_scale, 
-																					 v_trans+ mark_page_size_p->height()* mark_scale );
+																					 h_trans+ mark_page_size_p->getWidth()* mark_scale, 
+																					 v_trans+ mark_page_size_p->getHeight()* mark_scale );
 						}
 						else if( mark_page_rotation== 270 ) {
 							content_byte_p->addTemplate( mark_page_p, 
 																					 0, mark_scale,
 																					 -1* mark_scale, 0,
-																					 h_trans+ mark_page_size_p->width()* mark_scale, v_trans );
+																					 h_trans+ mark_page_size_p->getWidth()* mark_scale, v_trans );
 						}
 					}
 				}
@@ -2537,7 +2556,7 @@
 				}
 
 				// done; write output
-				writer_p->close();
+				writer_p->close(NULL);
 			}
 			break;
 
@@ -2663,15 +2682,15 @@
 			JvCreateJavaVM(NULL);
 			JvAttachCurrentThread(NULL, NULL);
 
-			JvInitClass(&java::System::class$);
-			JvInitClass(&java::util::ArrayList::class$);
-			JvInitClass(&java::util::Iterator::class$);
-
-			JvInitClass(&itext::PdfObject::class$);
-			JvInitClass(&itext::PdfName::class$);
-			JvInitClass(&itext::PdfDictionary::class$);
-			JvInitClass(&itext::PdfOutline::class$);
-			JvInitClass(&itext::PdfBoolean::class$);
+// 			JvInitClass(&java::System::class$);
+// 			JvInitClass(&java::util::ArrayList::class$);
+// 			JvInitClass(&java::util::Iterator::class$);
+
+// 			JvInitClass(&itext::PdfObject::class$);
+// 			JvInitClass(&itext::PdfName::class$);
+// 			JvInitClass(&itext::PdfDictionary::class$);
+// 			JvInitClass(&itext::PdfOutline::class$);
+// 			JvInitClass(&itext::PdfBoolean::class$);
 
 			TK_Session tk_session( argc, argv );
 
--- a/pdftk/report.cc
+++ b/pdftk/report.cc
@@ -50,8 +50,13 @@
 
 #include "com/lowagie/text/Document.h"
 #include "com/lowagie/text/Rectangle.h"
+#undef NULL
+
+// Ewww, PdfName has a field called NULL.
 #include "com/lowagie/text/pdf/PdfObject.h"
 #include "com/lowagie/text/pdf/PdfName.h"
+#define NULL __null
+
 #include "com/lowagie/text/pdf/PdfString.h"
 #include "com/lowagie/text/pdf/PdfNumber.h"
 #include "com/lowagie/text/pdf/PdfArray.h"
@@ -1315,11 +1320,12 @@
 				if( xmp_str_p ) {
 					xmp_str_p->put( itext::PdfName::TYPE, itext::PdfName::METADATA );
 					xmp_str_p->put( itext::PdfName::SUBTYPE, itext::PdfName::XML );
-			
-					itext::PdfIndirectReference* xmp_str_ref_p=
-						(itext::PdfIndirectReference*)reader_p->getPRIndirectReference( xmp_str_p );
 
-					catalog_p->put( itext::PdfName::METADATA, xmp_str_ref_p );
+//				FIXME: PdfReader.getPRIndirectReference is absent from itext-2.1.4
+// 					itext::PdfIndirectReference* xmp_str_ref_p=
+// 						(itext::PdfIndirectReference*)reader_p->getPRIndirectReference( xmp_str_p );
+
+// 					catalog_p->put( itext::PdfName::METADATA, xmp_str_ref_p );
 				}
 				else {
 					ret_val_b= false;
