def __exit__(self, exc_type, exc_value, exc_tp): CG.CGContextEndPage(self.context) return False
# 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)
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) else: x = x - xOffset drawWatermarkText(ctx, line, x, y, angle, scale, opacity) CG.CGContextEndPage(ctx) del pdf contextDone(ctx)