def centerText(y, text, font, pointSize): typeStyle = CTFontCreateWithName(font, pointSize, None) astr = CFAttributedStringCreate(kCFAllocatorDefault, text, { kCTFontAttributeName : typeStyle }) line = CTLineCreateWithAttributedString(astr) textWidth = astr.size().width if line: x = (pageSize[1][0]-textWidth)/2 # Quartz.CGContextSetAlpha(writeContext, opacity) Quartz.CGContextSetTextPosition(writeContext, x, y) CTLineDraw(line, writeContext) return
def centerText(y, text, font, pointSize): typeStyle = CTFontCreateWithName(font, pointSize, None) astr = CFAttributedStringCreate(kCFAllocatorDefault, text, {kCTFontAttributeName: typeStyle}) line = CTLineCreateWithAttributedString(astr) textWidth = astr.size().width if line: x = (pageSize[1][0] - textWidth) / 2 # Quartz.CGContextSetAlpha(writeContext, opacity) Quartz.CGContextSetTextPosition(writeContext, x, y) CTLineDraw(line, writeContext) return
nameOnly = os.path.basename(filename) outFilename = os.path.join(location, nameOnly) pdf = createPDFDocumentFromPath(filename) pages = Quartz.CGPDFDocumentGetNumberOfPages(pdf) metaDict = getDocInfo(filename) writeContext = createOutputContextWithPath(outFilename, metaDict) # Write page 1 with the added text if pdf: page = Quartz.CGPDFDocumentGetPage(pdf, 1) 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, scale, opacity) Quartz.CGContextEndPage(writeContext) # Write out the rest of the pages for i in range(2, (pages + 1)): page = Quartz.CGPDFDocumentGetPage(pdf, i) if page: Quartz.CGContextBeginPage(writeContext, mbox) Quartz.CGContextDrawPDFPage(writeContext, page) Quartz.CGContextEndPage(writeContext) del pdf contextDone(writeContext)
# 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 = selectFont('TimesNewRomanPSMT', 12.0) 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) text = str(i) astr = CFAttributedStringCreate( kCFAllocatorDefault, text, {kCTFontAttributeName: font}) line = CTLineCreateWithAttributedString(astr) x = Quartz.CGRectGetWidth(mbox) y = Quartz.CGRectGetHeight(mbox) y -= yOffset if i == 1: # Don't put number on page 1 pass elif i % 2 == 1: # Move right hand number in by its own width. textWidth = astr.size().width x = x - xOffset x = x - textWidth drawWatermarkText(writeContext, line, x, y, angle, scale, opacity) else: x = xOffset
# 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 = selectFont('TimesNewRomanPSMT', 12.0) 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) text = str(i) astr = CFAttributedStringCreate(kCFAllocatorDefault, text, { kCTFontAttributeName : font }) line = CTLineCreateWithAttributedString(astr) x = Quartz.CGRectGetWidth(mbox) y = Quartz.CGRectGetHeight(mbox) y -= yOffset if i == 1: # Don't put number on page 1 pass elif i%2 == 1: # Move right hand number in by its own width. textWidth = astr.size().width x = x - xOffset x = x - textWidth drawWatermarkText(writeContext, line, x , y, angle, scale, opacity) else: x = xOffset drawWatermarkText(writeContext, line, x, y, angle, scale, opacity)