Exemple #1
0
    # Font must be the PostScript name (i.e. no spaces) (See Get Info in FontBook)
    xOffset, yOffset, angle, opacity = 110.0, 200.0, 45.0, 0.5
    font = selectFont('Helvetica-Bold', 150.0)
    text = "SAMPLE"

    for filename in sys.argv[1:]:
        shortName = os.path.splitext(filename)[0]
        outFilename = getFilename(shortName, " WM")
        pdf = createPDFDocumentFromPath(filename)
        metaDict = getDocInfo(filename)
        writeContext = createOutputContextWithPath(outFilename, metaDict)
        pages = Quartz.CGPDFDocumentGetNumberOfPages(pdf)

        if pdf:
            for i in range(1, (pages + 1)):
                page = Quartz.CGPDFDocumentGetPage(pdf, i)
                if page:
                    mbox = Quartz.CGPDFPageGetBoxRect(page,
                                                      Quartz.kCGPDFMediaBox)
                    #	if Quartz.CGRectIsEmpty(mbox): mbox = None
                    Quartz.CGContextBeginPage(writeContext, mbox)
                    Quartz.CGContextDrawPDFPage(writeContext, page)
                    astr = CFAttributedStringCreate(
                        kCFAllocatorDefault, text,
                        {kCTFontAttributeName: font})
                    line = CTLineCreateWithAttributedString(astr)
                    drawWatermarkText(writeContext, line, xOffset, yOffset,
                                      angle, opacity)
                    Quartz.CGContextEndPage(writeContext)
    del pdf
contextDone(writeContext)
Exemple #2
0
        ctx = createOutputContextWithPath(outFilename)
        pages = CG.CGPDFDocumentGetNumberOfPages(pdf)
        # OPTIONS: Set the RELATIVE distance from outside top corner of page;
        # For other uses, set the angle, scale, and opacity of text
        # Font must be the PostScript name (i.e. no spaces) (See Get Info in FontBook)
        xOffset, yOffset, angle, scale, opacity = 45.0, 45.0, 0.0, 1.0, 1.0
        font = CTFontCreateWithName('TimesNewRomanPSMT', 12.0, None)

        if pdf:
            for i in range(1, (pages + 1)):
                page = CG.CGPDFDocumentGetPage(pdf, i)
                if page:
                    mbox = CG.CGPDFPageGetBoxRect(page, CG.kCGPDFMediaBox)
                    if CG.CGRectIsEmpty(mbox): mbox = None
                    CG.CGContextBeginPage(ctx, mbox)
                    CG.CGContextDrawPDFPage(ctx, page)
                    text = str(i)
                    print i
                    astr = CFAttributedStringCreate(
                        kCFAllocatorDefault, text,
                        {kCTFontAttributeName: font})
                    line = CTLineCreateWithAttributedString(astr)
                    x = CG.CGRectGetWidth(mbox)
                    y = CG.CGRectGetHeight(mbox)
                    y -= yOffset
                    if i == 1:  # Don't put number on page 1
                        pass
                    elif i % 2 == 1:
                        x = xOffset
                        drawWatermarkText(ctx, line, x, y, angle, scale,
                                          opacity)