Example usage for org.apache.pdfbox.pdmodel PDResources getCOSObject

List of usage examples for org.apache.pdfbox.pdmodel PDResources getCOSObject

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDResources getCOSObject.

Prototype

@Override
public COSDictionary getCOSObject() 

Source Link

Document

Returns the underlying dictionary.

Usage

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createImageForm(PDResources imageFormResources, PDResources innerFormResource,
        PDStream imageFormStream, PDRectangle formrect, AffineTransform affineTransform, PDJpeg img)
        throws IOException {

    /*/*from w w w .ja va2 s. c om*/
     * if you need text on the visible signature
     *
     * PDFont font = PDTrueTypeFont.loadTTF(this.pdfStructure.getTemplate(),
     * new File("D:\\arial.ttf")); font.setFontEncoding(new
     * WinAnsiEncoding());
     *
     * Map<String, PDFont> fonts = new HashMap<String, PDFont>();
     * fonts.put("arial", font);
     */
    PDXObjectForm imageForm = new PDXObjectForm(imageFormStream);
    imageForm.setBBox(formrect);
    imageForm.setMatrix(affineTransform);
    imageForm.setResources(imageFormResources);
    imageForm.setFormType(1);
    /*
     * imageForm.getResources().addFont(font);
     * imageForm.getResources().setFonts(fonts);
     */

    imageFormResources.getCOSObject().setDirect(true);
    String imageFormName = innerFormResource.addXObject(imageForm, "n");
    String imageName = imageFormResources.addXObject(img, "img");
    this.getStructure().setImageForm(imageForm);
    this.getStructure().setImageFormName(imageFormName);
    this.getStructure().setImageName(imageName);
    logger.debug("Created image form");
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createWidgetDictionary(PDSignatureField signatureField, PDResources holderFormResources)
        throws IOException {

    COSDictionary widgetDict = signatureField.getWidget().getDictionary();
    widgetDict.setNeedToBeUpdate(true);/*from   w w  w .j  a va2 s.  c o  m*/
    widgetDict.setItem(COSName.DR, holderFormResources.getCOSObject());

    getStructure().setWidgetDictionary(widgetDict);
    logger.debug("WidgetDictionary has been created");
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureBuilder.java

License:EUPL

@Override
public void injectProcSetArray(PDFormXObject innerForm, PDPage page, PDResources innerFormResources,
        PDResources imageFormResources, PDResources holderFormResources, COSArray procSet) {
    innerForm.getResources().getCOSObject().setItem(COSName.PROC_SET, procSet); //
    page.getCOSObject().setItem(COSName.PROC_SET, procSet);
    innerFormResources.getCOSObject().setItem(COSName.PROC_SET, procSet);
    /*//  ww w  . j av  a 2  s .  c o m
     * imageFormResources.getCOSDictionary() .setItem(COSName.PROC_SET,
     * procSet);
     */
    holderFormResources.getCOSObject().setItem(COSName.PROC_SET, procSet);
    logger.debug("inserted ProcSet to PDF");
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createImageForm(PDResources imageFormResources, PDResources innerFormResource,
        PDStream imageFormStream, PDRectangle formrect, AffineTransform affineTransform, PDFormXObject img)
        throws IOException {

    /*//from ww w. j av a 2 s.c  om
     * if you need text on the visible signature
     * 
     * PDFont font = PDTrueTypeFont.loadTTF(this.pdfStructure.getTemplate(),
     * new File("D:\\arial.ttf")); font.setFontEncoding(new
     * WinAnsiEncoding());
     * 
     * Map<String, PDFont> fonts = new HashMap<String, PDFont>();
     * fonts.put("arial", font);
     */
    PDFormXObject imageForm = new PDFormXObject(imageFormStream);
    imageForm.setBBox(formrect);
    imageForm.setMatrix(affineTransform);
    imageForm.setResources(imageFormResources);
    imageForm.setFormType(1);
    /*
     * imageForm.getResources().addFont(font);
     * imageForm.getResources().setFonts(fonts);
     */

    imageFormResources.getCOSObject().setDirect(true);
    COSName imageFormName = innerFormResource.add(imageForm, "n");//TODO: pdfbox2 - is this right?
    COSName imageName = imageFormResources.add(img, "img");
    this.getStructure().setImageForm(imageForm);
    this.getStructure().setImageFormName(imageFormName);
    this.getStructure().setImageName(imageName);
    logger.debug("Created image form");
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureBuilder.java

License:EUPL

public void createWidgetDictionary(PDSignatureField signatureField, PDResources holderFormResources)
        throws IOException {

    COSDictionary widgetDict = signatureField.getWidgets().get(0).getCOSObject();//TODO: pdfbox2 - is this right was getWidget before?
    widgetDict.setNeedToBeUpdated(true);
    widgetDict.setItem(COSName.DR, holderFormResources.getCOSObject());

    getStructure().setWidgetDictionary(widgetDict);
    logger.debug("WidgetDictionary has been crated");
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

/**
 * Creates a stream (from FOP's PDF library) from a PDF page parsed with PDFBox.
 * @param sourceDoc the source PDF the given page to be copied belongs to
 * @param page the page to transform into a stream
 * @param key value to use as key for the stream
 * @param atdoc adjustment for stream//  w  w  w . j a  v a  2  s .c o  m
 * @param fontinfo fonts
 * @param pos rectangle
 * @return the stream
 * @throws IOException if an I/O error occurs
 */
public String createStreamFromPDFBoxPage(PDDocument sourceDoc, PDPage page, String key, AffineTransform atdoc,
        FontInfo fontinfo, Rectangle pos) throws IOException {
    handleAnnotations(sourceDoc, page, atdoc);
    if (pageNumbers.containsKey(targetPage.getPageIndex())) {
        pageNumbers.get(targetPage.getPageIndex()).set(0, targetPage.makeReference());
    }
    PDResources sourcePageResources = page.getResources();
    PDStream pdStream = getContents(page);

    COSDictionary fonts = (COSDictionary) sourcePageResources.getCOSObject().getDictionaryObject(COSName.FONT);
    COSDictionary fontsBackup = null;
    UniqueName uniqueName = new UniqueName(key, sourcePageResources);
    String newStream = null;
    if (fonts != null && pdfDoc.isMergeFontsEnabled()) {
        fontsBackup = new COSDictionary(fonts);
        MergeFontsPDFWriter m = new MergeFontsPDFWriter(fonts, fontinfo, uniqueName, parentFonts, currentMCID);
        newStream = m.writeText(pdStream);
        //            if (newStream != null) {
        //                for (Object f : fonts.keySet().toArray()) {
        //                    COSDictionary fontdata = (COSDictionary)fonts.getDictionaryObject((COSName)f);
        //                    if (getUniqueFontName(fontdata) != null) {
        //                        fonts.removeItem((COSName)f);
        //                    }
        //                }
        //            }
    }
    if (newStream == null) {
        PDFWriter writer = new PDFWriter(uniqueName, currentMCID);
        newStream = writer.writeText(pdStream);
        currentMCID = writer.getCurrentMCID();

    }
    pdStream = new PDStream(sourceDoc, new ByteArrayInputStream(newStream.getBytes("ISO-8859-1")));
    mergeXObj(sourcePageResources.getCOSObject(), fontinfo, uniqueName);
    PDFDictionary pageResources = (PDFDictionary) cloneForNewDocument(sourcePageResources.getCOSObject());

    PDFDictionary fontDict = (PDFDictionary) pageResources.get("Font");
    if (fontDict != null && pdfDoc.isMergeFontsEnabled()) {
        for (Map.Entry<String, Typeface> fontEntry : fontinfo.getUsedFonts().entrySet()) {
            Typeface font = fontEntry.getValue();
            if (font instanceof FOPPDFFont) {
                FOPPDFFont pdfFont = (FOPPDFFont) font;
                if (pdfFont.getRef() == null) {
                    pdfFont.setRef(new PDFDictionary());
                    pdfDoc.assignObjectNumber(pdfFont.getRef());
                }
                fontDict.put(fontEntry.getKey(), pdfFont.getRef());
            }
        }
    }
    updateXObj(sourcePageResources.getCOSObject(), pageResources);
    if (fontsBackup != null) {
        sourcePageResources.getCOSObject().setItem(COSName.FONT, fontsBackup);
    }

    COSStream originalPageContents = pdStream.getCOSObject();

    bindOptionalContent(sourceDoc);

    PDFStream pageStream;
    Set filter;
    //        if (originalPageContents instanceof COSStreamArray) {
    //            COSStreamArray array = (COSStreamArray)originalPageContents;
    //            pageStream = new PDFStream();
    //            InputStream in = array.getUnfilteredStream();
    //            OutputStream out = pageStream.getBufferOutputStream();
    //            IOUtils.copyLarge(in, out);
    //            filter = FILTER_FILTER;
    //        } else {
    pageStream = (PDFStream) cloneForNewDocument(originalPageContents);
    filter = Collections.EMPTY_SET;
    //        }
    if (pageStream == null) {
        pageStream = new PDFStream();
    }
    if (originalPageContents != null) {
        transferDict(originalPageContents, pageStream, filter);
    }

    transferPageDict(fonts, uniqueName, sourcePageResources);

    PDRectangle mediaBox = page.getMediaBox();
    PDRectangle cropBox = page.getCropBox();
    PDRectangle viewBox = cropBox != null ? cropBox : mediaBox;

    //Handle the /Rotation entry on the page dict
    int rotation = PDFUtil.getNormalizedRotation(page);

    //Transform to FOP's user space
    float w = (float) pos.getWidth() / 1000f;
    float h = (float) pos.getHeight() / 1000f;
    if (rotation == 90 || rotation == 270) {
        float tmp = w;
        w = h;
        h = tmp;
    }
    atdoc.setTransform(AffineTransform.getScaleInstance(w / viewBox.getWidth(), h / viewBox.getHeight()));
    atdoc.translate(0, viewBox.getHeight());
    atdoc.rotate(-Math.PI);
    atdoc.scale(-1, 1);
    atdoc.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());

    rotate(rotation, viewBox, atdoc);

    StringBuilder boxStr = new StringBuilder();
    boxStr.append(PDFNumber.doubleOut(mediaBox.getLowerLeftX())).append(' ')
            .append(PDFNumber.doubleOut(mediaBox.getLowerLeftY())).append(' ')
            .append(PDFNumber.doubleOut(mediaBox.getWidth())).append(' ')
            .append(PDFNumber.doubleOut(mediaBox.getHeight())).append(" re W n\n");
    return boxStr.toString() + IOUtils.toString(pdStream.createInputStream(null), "ISO-8859-1");
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private void transferPageDict(COSDictionary fonts, UniqueName uniqueName, PDResources sourcePageResources)
        throws IOException {
    if (fonts != null) {
        for (Map.Entry<COSName, COSBase> f : fonts.entrySet()) {
            String name = uniqueName.getName(f.getKey());
            targetPage.getPDFResources().addFont(name, (PDFDictionary) cloneForNewDocument(f.getValue()));
        }/*from  w w w .j a va 2  s  .co  m*/
    }
    for (Map.Entry<COSName, COSBase> e : sourcePageResources.getCOSObject().entrySet()) {
        transferDict(e, uniqueName);
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.UniqueName.java

License:Apache License

public UniqueName(String key, PDResources sourcePageResources) {
    this.key = Integer.toString(key.hashCode());
    resourceNames = getResourceNames(sourcePageResources.getCOSObject());
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

License:Apache License

private COSDictionary getFont(PDDocument doc, String internalname) throws IOException {
    PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
    PDResources sourcePageResources = page.getResources();
    COSDictionary fonts = (COSDictionary) sourcePageResources.getCOSObject().getDictionaryObject(COSName.FONT);
    return (COSDictionary) fonts.getDictionaryObject(internalname);
}