• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

JAR with import statements seems to mess with same classes loaded via java loader

Explorer ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

Hi Guys and Gals,

Having a weird issue with javaloader and a new JAR I had a developer write for me. My existing code uses itext 2.1.7 and its loaded via Mark Mandel's javaloader (https://github.com/markmandel/JavaLoader) 

The existing code merges PDF's together (and does a better job than CFPDF and isn't crippled. Its not complicated code either. The part of the code in question:

local.outStream = server.javaLoader.create("java.io.FileOutputStream").init( local.fulloutputpath );

local.copy = server.javaLoader.create("com.lowagie.text.pdf.PdfCopyFields").init(local.outStream);

local.PdfReader = server.javaLoader.create("com.lowagie.text.pdf.PdfReader");

Then it does some work. and then closes the PDF:

local.copy.close();

That close() call throws an error java.lang.RuntimeException: The document is not open.  But it only throws that error if I have added this new JAR I had a guy write for me, to the java loader. I haven't even called the single method of this new class, and it is already breaking my existing code. How can that be? Is it the import statements? Do they somehow overwrite the existing itext libs?

The  JAVA source code is below - anybody have any idea how to re-write it (outside of re-writing it in CFML, which I guess is an option too..), so that the references to itext (is it the import statements causing an issue??) don't cause issues with other code and itext loaded via the javaloader:

package com.pdfutil;

import java.io.FileOutputStream;

import com.lowagie.text.pdf.PRStream;

import com.lowagie.text.pdf.PdfDictionary;

import com.lowagie.text.pdf.PdfName;

import com.lowagie.text.pdf.PdfObject;

import com.lowagie.text.pdf.PdfReader;

import com.lowagie.text.pdf.PdfStamper;

/**

* Utility class for Pdf page number

*

*/

public class PageNumberUtil {

public static void main(String[] args) {

// testing

String input = "PDF Numbering2.pdf";

String output = "PDF Numbering2_output.pdf";

boolean status = PageNumberUtil.updatePageNumbering(input, output);

System.out.println("status=" + status);

}

public static boolean updatePageNumbering(String inputPath, String outputPath) {

System.out.println("updatePageNumbering starting...");

boolean status = true;

try {

// create pdf reader

PdfReader reader = new PdfReader(inputPath);

// get total of page

Integer numOfPages = reader.getNumberOfPages();

// loop from 1 to numOfPages

for (Integer i = 1; i <= numOfPages; i++) {

PdfDictionary dictionary = reader.getPageN(i);

PdfObject object = dictionary.getDirectObject(PdfName.CONTENTS);

if (object instanceof PRStream) {

PRStream stream = (PRStream) object;

byte[] data = PdfReader.getStreamBytes(stream);

// find {pagenum} of {pagetotal} placeholders and replace

String dataStr = new String(data);

dataStr = dataStr.replace("{pagenum}", i.toString());

dataStr = dataStr.replace("{pagetotal}", numOfPages.toString());

stream.setData(dataStr.getBytes());

}

}

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPath));

stamper.close();

reader.close();

} catch (Exception e) {

e.printStackTrace();

status = false;

}

System.out.println("updatePageNumbering End...");

return status;

}

}

TOPICS
Advanced techniques

Views

406

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation
Resources
Documentation