0

I am struggling with replacing a text after the merge of document. I have one document which is created through IText from scratch. In this first pdf I am able to replace any text any time. The code for string replacement is below.

    URL    resource       = CobAccountOpeningLetterFieldServiceTest.class.getResource(SAMPLE_GENERATED_DOCUMENT);
    Date   date           = CobModelUtils.getUtilDateFromString("Nov 02, 2017");
    String region         = "en";
    String dateFromLocale = getDateFromLocale(region, date);

    PdfReader     reader = new PdfReader(resource);
    PdfDictionary dict   = reader.getPageN(1);
    PdfObject     object = dict.getDirectObject(PdfName.CONTENTS);
    if (object instanceof PRStream) {
        PRStream stream     = (PRStream) object;
        byte[]   data       = PdfReader.getStreamBytes(stream);
        String   dataString = new String(data, CHARACTER_ENCODING_SET);
        System.out.println(dataString);
        dataString = dataString.replace(TEMPORARY_DATE_PLACE_HOLDER, dateFromLocale);
        stream.setData(dataString.getBytes(CHARACTER_ENCODING_SET));
    }
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT4));
    stamper.close();
    reader.close();

This individually works fine, but when I merge any other document in it, the string replacement stops working on new PDF.

Code for merging the pdf is

private InputStream mergeDocument(List<InputStream> documentToMerge) throws DocumentException, IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Document              document              = new Document();
    PdfWriter             pdfWriter             = PdfWriter.getInstance(document, byteArrayOutputStream);
    document.open();
    PdfContentByte cb = pdfWriter.getDirectContent();
    for (InputStream inputStream : documentToMerge) {
        PdfReader pdfReader = new PdfReader(inputStream);
        PdfReader.unethicalreading = true;
        for (int pageOfCurrentReaderPDF = 1; pageOfCurrentReaderPDF <= pdfReader.getNumberOfPages(); pageOfCurrentReaderPDF++) {
            Rectangle r = pdfReader.getPageSize(pdfReader.getPageN(pageOfCurrentReaderPDF));
            document.setPageSize(r);
            document.newPage();
            PdfImportedPage page = pdfWriter.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
            cb.addTemplate(page, 0, 0);
        }
    }
    byteArrayOutputStream.flush();
    document.close();
    return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}

Any one have faced this issue earlier. Please guide, thanks,

SaChi
  • 1
  • 3
  • 1
    Your replacing code (concerning which I already mentioned in my answer to your previous question that it only works under special circumstances) only manipulates the *immediate* page content, not the content *of form XObjects referenced* from the page content. Now you took your merge code from bad example code which puts the copied page content into a form XObject. Thus, please use sensible merging code, e.g. read [Bruno's answer here](https://stackoverflow.com/a/23063576/1729265). – mkl Nov 07 '17 at 15:09
  • I would like to repeat: **use sensible merging code.** Where do people get this bad code? Why didn't you read the official documentation, @SaChi? Questions like yours are so frustrating! – Bruno Lowagie Nov 07 '17 at 18:21
  • Just in my defence, I have seen the official document but most of them have good example and working code with itext7, didn't found any good solution for itext5 (my company problem I am stuck with itext5 only) thanks for the explanation and directing me to right approach will surely try that – SaChi Nov 07 '17 at 18:53
  • I have seen that solution but the approved version (5.3.1) of IText jar we have in our company doesn't have addDocument method. – SaChi Nov 07 '17 at 19:00
  • thanks @BrunoLowagie & mkl I got approval to upgrade my jar to 5.5.6 and it's working after that, thanks again – SaChi Nov 07 '17 at 20:00

0 Answers0