Problems scaling PDF

Help
abevig
2011-08-08
2013-05-28
  • abevig
    abevig
    2011-08-08

    I want to add a  scale/translate operation at the beginning of a page of PDF text read from a file.  I have tried variations on the following, but it always throws a NullPointerException when saving.  What is the correct way to scale existing PDF content?

    COSObject operands = new COSObject;
    operands = COSFixed.create(vtScale);
    operands = COSFixed.create(0.0f);
    operands = COSFixed.create(0.0f);
    operands = COSFixed.create(hzScale);
    operands = COSFixed.create(vtTranslate);
    operands = COSFixed.create(hzTranslate);
    CSOperation operation = new CSOperation(CSOperators.CSO_cm.getToken(), operands);

    CSContent content = CSContent.createNew();
    content.addOperation(0, operation);
    page.prependContentStream(content);

    java.lang.IllegalStateException: java.lang.NullPointerException
    at de.intarsys.pdf.cos.COSTrailer.createFileID(COSTrailer.java:179)
    at de.intarsys.pdf.cos.COSTrailer.updateFileID(COSTrailer.java:272)
    at de.intarsys.pdf.writer.COSWriter.basicWriteDocument(COSWriter.java:298)
    at de.intarsys.pdf.writer.COSWriter.writeDocument(COSWriter.java:805)
    at de.intarsys.pdf.st.STDocument.save(STDocument.java:1089)
    at de.intarsys.pdf.cos.COSDocument.save(COSDocument.java:656)
    at de.intarsys.pdf.pd.PDDocument.save(PDDocument.java:843)
    at de.intarsys.pdf.pd.PDDocument.save(PDDocument.java:819)

     
  • mtraut
    mtraut
    2011-08-08

    Your technique seem right. You might want to use CSCretor for a slightly more elegant solution.

    The error seems to be caused by a unexpected entry in the info dict - so you may patch COSTrailer.createFileID

    <code>
    ..
    if (infoDict != null) {
    for (Iterator it = infoDict.cosGetDict().iterator(); it
    .hasNext();) {
    COSObject object = (COSObject) it.next();
    String tempString = object.stringValue();
    if (tempString != null) {
    digest.update(tempString.getBytes());
    }
    }
    }
    ..
    </code>

    Or maybe you can kill / cleanup the info dict