Search in sources :

Example 16 with PDFont

use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.

the class TestOptionalContentGroups method testOCGGeneration.

/**
 * Tests OCG generation.
 * @throws Exception if an error occurs
 */
public void testOCGGeneration() throws Exception {
    PDDocument doc = new PDDocument();
    try {
        // Create new page
        PDPage page = new PDPage();
        doc.addPage(page);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
            page.setResources(resources);
        }
        // Prepare OCG functionality
        PDOptionalContentProperties ocprops = new PDOptionalContentProperties();
        doc.getDocumentCatalog().setOCProperties(ocprops);
        // ocprops.setBaseState(BaseState.ON); //ON=default
        // Create OCG for background
        PDOptionalContentGroup background = new PDOptionalContentGroup("background");
        ocprops.addGroup(background);
        assertTrue(ocprops.isGroupEnabled("background"));
        // Create OCG for enabled
        PDOptionalContentGroup enabled = new PDOptionalContentGroup("enabled");
        ocprops.addGroup(enabled);
        assertFalse(ocprops.setGroupEnabled("enabled", true));
        assertTrue(ocprops.isGroupEnabled("enabled"));
        // Create OCG for disabled
        PDOptionalContentGroup disabled = new PDOptionalContentGroup("disabled");
        ocprops.addGroup(disabled);
        assertFalse(ocprops.setGroupEnabled("disabled", true));
        assertTrue(ocprops.isGroupEnabled("disabled"));
        assertTrue(ocprops.setGroupEnabled("disabled", false));
        assertFalse(ocprops.isGroupEnabled("disabled"));
        // Setup page content stream and paint background/title
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        contentStream.beginMarkedContent(COSName.OC, background);
        contentStream.beginText();
        contentStream.setFont(font, 14);
        contentStream.newLineAtOffset(80, 700);
        contentStream.showText("PDF 1.5: Optional Content Groups");
        contentStream.endText();
        font = PDType1Font.HELVETICA;
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(80, 680);
        contentStream.showText("You should see a green textline, but no red text line.");
        contentStream.endText();
        contentStream.endMarkedContent();
        // Paint enabled layer
        contentStream.beginMarkedContent(COSName.OC, enabled);
        contentStream.setNonStrokingColor(AWTColor.GREEN);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(80, 600);
        contentStream.showText("This is from an enabled layer. If you see this, that's good.");
        contentStream.endText();
        contentStream.endMarkedContent();
        // Paint disabled layer
        contentStream.beginMarkedContent(COSName.OC, disabled);
        contentStream.setNonStrokingColor(AWTColor.RED);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(80, 500);
        contentStream.showText("This is from a disabled layer. If you see this, that's NOT good!");
        contentStream.endText();
        contentStream.endMarkedContent();
        contentStream.close();
        File targetFile = new File(testResultsDir, "ocg-generation.pdf");
        doc.save(targetFile.getAbsolutePath());
    } finally {
        doc.close();
    }
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Example 17 with PDFont

use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.

the class SetFontAndSize method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    if (arguments.size() < 2) {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    COSBase base1 = arguments.get(1);
    if (!(base0 instanceof COSName)) {
        return;
    }
    if (!(base1 instanceof COSNumber)) {
        return;
    }
    COSName fontName = (COSName) base0;
    float fontSize = ((COSNumber) base1).floatValue();
    context.getGraphicsState().getTextState().setFontSize(fontSize);
    PDFont font = context.getResources().getFont(fontName);
    if (font == null) {
        Log.w("PdfBox-Android", "font '" + fontName.getName() + "' not found in resources");
    }
    context.getGraphicsState().getTextState().setFont(font);
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) COSName(com.tom_roush.pdfbox.cos.COSName) MissingOperandException(com.tom_roush.pdfbox.contentstream.operator.MissingOperandException) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 18 with PDFont

use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.

the class PDFreeTextAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    float[] pathsArray = new float[0];
    if (PDAnnotationMarkup.IT_FREE_TEXT_CALLOUT.equals(annotation.getIntent())) {
        pathsArray = annotation.getCallout();
        if (pathsArray == null || pathsArray.length != 4 && pathsArray.length != 6) {
            pathsArray = new float[0];
        }
    }
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
    PDAppearanceContentStream cs = null;
    try {
        cs = getNormalAppearanceAsContentStream(true);
        // The fill color is the /C entry, there is no /IC entry defined
        boolean hasBackground = cs.setNonStrokingColorOnDemand(annotation.getColor());
        setOpacity(cs, annotation.getConstantOpacity());
        // Adobe uses the last non stroking color from /DA as stroking color!
        // But if there is a color in /DS, then that one is used for text.
        PDColor strokingColor = extractNonStrokingColor(annotation);
        boolean hasStroke = cs.setStrokingColorOnDemand(strokingColor);
        PDColor textColor = strokingColor;
        String defaultStyleString = annotation.getDefaultStyleString();
        if (defaultStyleString != null) {
            Matcher m = COLOR_PATTERN.matcher(defaultStyleString);
            if (m.find()) {
                int color = Integer.parseInt(m.group(1), 16);
                float r = ((color >> 16) & 0xFF) / 255f;
                float g = ((color >> 8) & 0xFF) / 255f;
                float b = (color & 0xFF) / 255f;
                textColor = new PDColor(new float[] { r, g, b }, PDDeviceRGB.INSTANCE);
            }
        }
        if (ab.dashArray != null) {
            cs.setLineDashPattern(ab.dashArray, 0);
        }
        cs.setLineWidth(ab.width);
        // see CTAN-example-Annotations.pdf
        for (int i = 0; i < pathsArray.length / 2; ++i) {
            float x = pathsArray[i * 2];
            float y = pathsArray[i * 2 + 1];
            if (i == 0) {
                if (SHORT_STYLES.contains(annotation.getLineEndingStyle())) {
                    // modify coordinate to shorten the segment
                    // https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
                    float x1 = pathsArray[2];
                    float y1 = pathsArray[3];
                    float len = (float) (Math.sqrt(Math.pow(x - x1, 2) + Math.pow(y - y1, 2)));
                    if (Float.compare(len, 0) != 0) {
                        x += (x1 - x) / len * ab.width;
                        y += (y1 - y) / len * ab.width;
                    }
                }
                cs.moveTo(x, y);
            } else {
                cs.lineTo(x, y);
            }
        }
        if (pathsArray.length > 0) {
            cs.stroke();
        }
        // paint the styles here and after line(s) draw, to avoid line crossing a filled shape
        if (PDAnnotationMarkup.IT_FREE_TEXT_CALLOUT.equals(annotation.getIntent()) && // check only needed to avoid q cm Q if LE_NONE
        !LE_NONE.equals(annotation.getLineEndingStyle()) && pathsArray.length >= 4) {
            float x2 = pathsArray[2];
            float y2 = pathsArray[3];
            float x1 = pathsArray[0];
            float y1 = pathsArray[1];
            cs.saveGraphicsState();
            if (ANGLED_STYLES.contains(annotation.getLineEndingStyle())) {
                // do a transform so that first "arm" is imagined flat,
                // like in line handler.
                // The alternative would be to apply the transform to the
                // LE shape coordinates directly, which would be more work
                // and produce code difficult to understand
                double angle = Math.atan2(y2 - y1, x2 - x1);
                cs.transform(Matrix.getRotateInstance(angle, x1, y1));
            } else {
                cs.transform(Matrix.getTranslateInstance(x1, y1));
            }
            drawStyle(annotation.getLineEndingStyle(), cs, 0, 0, ab.width, hasStroke, hasBackground, false);
            cs.restoreGraphicsState();
        }
        PDRectangle borderBox;
        PDBorderEffectDictionary borderEffect = annotation.getBorderEffect();
        if (borderEffect != null && borderEffect.getStyle().equals(PDBorderEffectDictionary.STYLE_CLOUDY)) {
            // Adobe draws the text with the original rectangle in mind.
            // but if there is an /RD, then writing area get smaller.
            // do this here because /RD is overwritten in a few lines
            borderBox = applyRectDifferences(getRectangle(), annotation.getRectDifferences());
            // TODO this segment was copied from square handler. Refactor?
            CloudyBorder cloudyBorder = new CloudyBorder(cs, borderEffect.getIntensity(), ab.width, getRectangle());
            cloudyBorder.createCloudyRectangle(annotation.getRectDifference());
            annotation.setRectangle(cloudyBorder.getRectangle());
            annotation.setRectDifference(cloudyBorder.getRectDifference());
            PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
            appearanceStream.setBBox(cloudyBorder.getBBox());
            appearanceStream.setMatrix(cloudyBorder.getMatrix());
        } else {
            // handle the border box
            // 
            // There are two options. The handling is not part of the PDF specification but
            // implementation specific to Adobe Reader
            // - if /RD is set the border box is the /Rect entry inset by the respective
            // border difference.
            // - if /RD is not set then we don't touch /RD etc because Adobe doesn't either.
            borderBox = applyRectDifferences(getRectangle(), annotation.getRectDifferences());
            annotation.getNormalAppearanceStream().setBBox(borderBox);
            // note that borderBox is not modified
            PDRectangle paddedRectangle = getPaddedRectangle(borderBox, ab.width / 2);
            cs.addRect(paddedRectangle.getLowerLeftX(), paddedRectangle.getLowerLeftY(), paddedRectangle.getWidth(), paddedRectangle.getHeight());
        }
        cs.drawShape(ab.width, hasStroke, hasBackground);
        // rotation is an undocumented feature, but Adobe uses it. Examples can be found
        // in pdf_commenting_new.pdf file, page 3.
        int rotation = annotation.getCOSObject().getInt(COSName.ROTATE, 0);
        cs.transform(Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0));
        float xOffset;
        float yOffset;
        float width = rotation == 90 || rotation == 270 ? borderBox.getHeight() : borderBox.getWidth();
        // strategy to write formatted text is somewhat inspired by
        // AppearanceGeneratorHelper.insertGeneratedAppearance()
        PDFont font = PDType1Font.HELVETICA;
        float clipY;
        float clipWidth = width - ab.width * 4;
        float clipHeight = rotation == 90 || rotation == 270 ? borderBox.getWidth() - ab.width * 4 : borderBox.getHeight() - ab.width * 4;
        extractFontDetails(annotation);
        if (document != null && document.getDocumentCatalog().getAcroForm() != null) {
            // Try to get font from AcroForm default resources
            // Sample file: https://gitlab.freedesktop.org/poppler/poppler/issues/6
            PDResources defaultResources = document.getDocumentCatalog().getAcroForm().getDefaultResources();
            if (defaultResources != null) {
                PDFont defaultResourcesFont = defaultResources.getFont(fontName);
                if (defaultResourcesFont != null) {
                    font = defaultResourcesFont;
                }
            }
        }
        // value used by Adobe, no idea where it comes from, actual font bbox max y is 0.931
        // gathered by creating an annotation with width 0.
        float yDelta = 0.7896f;
        switch(rotation) {
            case 180:
                xOffset = -borderBox.getUpperRightX() + ab.width * 2;
                yOffset = -borderBox.getLowerLeftY() - ab.width * 2 - yDelta * fontSize;
                clipY = -borderBox.getUpperRightY() + ab.width * 2;
                break;
            case 90:
                xOffset = borderBox.getLowerLeftY() + ab.width * 2;
                yOffset = -borderBox.getLowerLeftX() - ab.width * 2 - yDelta * fontSize;
                clipY = -borderBox.getUpperRightX() + ab.width * 2;
                break;
            case 270:
                xOffset = -borderBox.getUpperRightY() + ab.width * 2;
                yOffset = borderBox.getUpperRightX() - ab.width * 2 - yDelta * fontSize;
                clipY = borderBox.getLowerLeftX() + ab.width * 2;
                break;
            case 0:
            default:
                xOffset = borderBox.getLowerLeftX() + ab.width * 2;
                yOffset = borderBox.getUpperRightY() - ab.width * 2 - yDelta * fontSize;
                clipY = borderBox.getLowerLeftY() + ab.width * 2;
                break;
        }
        // clip writing area
        cs.addRect(xOffset, clipY, clipWidth, clipHeight);
        cs.clip();
        cs.beginText();
        cs.setFont(font, fontSize);
        cs.setNonStrokingColor(textColor.getComponents());
        AppearanceStyle appearanceStyle = new AppearanceStyle();
        appearanceStyle.setFont(font);
        appearanceStyle.setFontSize(fontSize);
        PlainTextFormatter formatter = new PlainTextFormatter.Builder(cs).style(appearanceStyle).text(new PlainText(annotation.getContents())).width(width - ab.width * 4).wrapLines(true).initialOffset(xOffset, yOffset).build();
        try {
            formatter.format();
        } catch (IllegalArgumentException ex) {
            throw new IOException(ex);
        }
        cs.endText();
        if (pathsArray.length > 0) {
            PDRectangle rect = getRectangle();
            // Adjust rectangle
            // important to do this after the rectangle has been painted, because the
            // final rectangle will be bigger due to callout
            // CTAN-example-Annotations.pdf p1
            // TODO in a class structure this should be overridable
            float minX = Float.MAX_VALUE;
            float minY = Float.MAX_VALUE;
            float maxX = Float.MIN_VALUE;
            float maxY = Float.MIN_VALUE;
            for (int i = 0; i < pathsArray.length / 2; ++i) {
                float x = pathsArray[i * 2];
                float y = pathsArray[i * 2 + 1];
                minX = Math.min(minX, x);
                minY = Math.min(minY, y);
                maxX = Math.max(maxX, x);
                maxY = Math.max(maxY, y);
            }
            // arrow length is 9 * width at about 30° => 10 * width seems to be enough
            rect.setLowerLeftX(Math.min(minX - ab.width * 10, rect.getLowerLeftX()));
            rect.setLowerLeftY(Math.min(minY - ab.width * 10, rect.getLowerLeftY()));
            rect.setUpperRightX(Math.max(maxX + ab.width * 10, rect.getUpperRightX()));
            rect.setUpperRightY(Math.max(maxY + ab.width * 10, rect.getUpperRightY()));
            annotation.setRectangle(rect);
            // need to set the BBox too, because rectangle modification came later
            annotation.getNormalAppearanceStream().setBBox(getRectangle());
        // TODO when callout is used, /RD should be so that the result is the writable part
        }
    } catch (IOException ex) {
        Log.e("PdfBox-Android", ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(cs);
    }
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) Matcher(java.util.regex.Matcher) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDBorderEffectDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderEffectDictionary) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) AppearanceStyle(com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.AppearanceStyle) PDAppearanceContentStream(com.tom_roush.pdfbox.pdmodel.PDAppearanceContentStream) PlainText(com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.PlainText) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PlainTextFormatter(com.tom_roush.pdfbox.pdmodel.interactive.annotation.layout.PlainTextFormatter)

Example 19 with PDFont

use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.

the class PDFStreamEngine method showTextStrings.

/**
 * Called when a string of text with spacing adjustments is to be shown.
 *
 * @param array array of encoded text strings and adjustments
 * @throws IOException if there was an error showing the text
 */
public void showTextStrings(COSArray array) throws IOException {
    PDTextState textState = getGraphicsState().getTextState();
    float fontSize = textState.getFontSize();
    float horizontalScaling = textState.getHorizontalScaling() / 100f;
    PDFont font = textState.getFont();
    boolean isVertical = false;
    if (font != null) {
        isVertical = font.isVertical();
    }
    for (COSBase obj : array) {
        if (obj instanceof COSNumber) {
            float tj = ((COSNumber) obj).floatValue();
            // calculate the combined displacements
            float tx;
            float ty;
            if (isVertical) {
                tx = 0;
                ty = -tj / 1000 * fontSize;
            } else {
                tx = -tj / 1000 * fontSize * horizontalScaling;
                ty = 0;
            }
            applyTextAdjustment(tx, ty);
        } else if (obj instanceof COSString) {
            byte[] string = ((COSString) obj).getBytes();
            showText(string);
        } else {
            throw new IOException("Unknown type in array for TJ operation:" + obj);
        }
    }
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDTextState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDTextState) IOException(java.io.IOException) COSString(com.tom_roush.pdfbox.cos.COSString)

Aggregations

PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)19 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)8 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)8 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)8 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)7 IOException (java.io.IOException)6 File (java.io.File)5 COSBase (com.tom_roush.pdfbox.cos.COSBase)4 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)3 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)3 Bitmap (android.graphics.Bitmap)2 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)2 COSName (com.tom_roush.pdfbox.cos.COSName)2 COSString (com.tom_roush.pdfbox.cos.COSString)2 PDTextState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDTextState)2 Matrix (com.tom_roush.pdfbox.util.Matrix)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)1 COSObject (com.tom_roush.pdfbox.cos.COSObject)1