2

Mac OS X API を使用して、Quartz フィルタを適用した PDF ファイルを保存しようとしています。これは、プレビュー アプリケーションの [名前を付けて保存] ダイアログから可能です。これまでのところ、次のコードを作成しました (Python と pyObjC を使用していますが、私にとっては重要ではありません)。

-- filter-pdf.py: 開始

from Foundation import *
from Quartz import *
import objc

page_rect = CGRectMake (0, 0, 612, 792)
fdict = NSDictionary.dictionaryWithContentsOfFile_("/System/Library/Filters/Blue
\ Tone.qfilter")
in_pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename ("test
.pdf"))
url = CFURLCreateWithFileSystemPath(None, "test_out.pdf", kCFURLPOSIXPathStyle, 
False)
c = CGPDFContextCreateWithURL(url, page_rect, fdict)

np = CGPDFDocumentGetNumberOfPages(in_pdf)
for ip in range (1, np+1):
        page = CGPDFDocumentGetPage(in_pdf, ip)
        r = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
        CGContextBeginPage(c, r)
        CGContextDrawPDFPage(c, page)
        CGContextEndPage(c)

-- filter-pdf.py: 終了

残念ながら、フィルター「ブルー トーン」が適用されていないため、出力 PDF は入力 PDF とまったく同じように見えます。

質問: 私が見逃したものは何ですか? フィルターを適用するには?

まあ、ドキュメンテーションは、「fdict」を作成して使用するそのような方法でフィルターが適用されることを約束していません。しかし、以前のバージョンの Mac で配布されていたサンプル コード /Developer/Examples/Quartz/Python/filter-pdf.py を (できる限り) 書き直しました (一方、このコードも機能しません)。

----- filter-pdf-old.py: 開始

from CoreGraphics import *
import sys, os, math, getopt, string

def usage ():
  print '''
usage: python filter-pdf.py FILTER INPUT-PDF OUTPUT-PDF

Apply a ColorSync Filter to a PDF document.
'''

def main ():

  page_rect = CGRectMake (0, 0, 612, 792)

  try:
    opts,args = getopt.getopt (sys.argv[1:], '', [])
  except getopt.GetoptError:
    usage ()
    sys.exit (1)

  if len (args) != 3:
    usage ()
    sys.exit (1)

  filter = CGContextFilterCreateDictionary (args[0])
  if not filter:
    print 'Unable to create context filter'
    sys.exit (1)
  pdf = CGPDFDocumentCreateWithProvider (CGDataProviderCreateWithFilename (args[1]))
  if not pdf:
    print 'Unable to open input file'
    sys.exit (1)

  c = CGPDFContextCreateWithFilename (args[2], page_rect, filter)
  if not c:
    print 'Unable to create output context'
    sys.exit (1)

  for p in range (1, pdf.getNumberOfPages () + 1):
    #r = pdf.getMediaBox (p)
    r = pdf.getPage(p).getBoxRect(p)
    c.beginPage (r)
    c.drawPDFDocument (r, pdf, p)
    c.endPage ()

  c.finish ()

if __name__ == '__main__':
  main ()

----- filter-pdf-old.py: 終了

================================================== =====================

答えに基づく作業コード:

from Foundation import *
from Quartz import *

pdf_url = NSURL.fileURLWithPath_("test.pdf")
pdf_doc = PDFDocument.alloc().initWithURL_(pdf_url)

furl = NSURL.fileURLWithPath_("/System/Library/Filters/Blue Tone.qfilter")
fobj = QuartzFilter.quartzFilterWithURL_(furl)
fdict = { 'QuartzFilter': fobj }
pdf_doc.writeToFile_withOptions_("test_out.pdf", fdict)
4

1 に答える 1

5

2 つの方法 - 既存のファイルを開いて変更する必要がある場合は、PDFKit の PDFDocument (参照) を使用し、必要なフィルターの「QuartzFilter」オプションを含むオプション dict を指定して PDFDocument の writeToFile_withOptions_ を使用します。

OTOH 独自の描画が必要で、手元に CGContext がある場合は、次の行に沿って何かを使用できます。

from Quartz import *
data = NSMutableData.dataWithCapacity_(1024**2)
dataConsumer = CGDataConsumerCreateWithCFData(data)
context = CGPDFContextCreate(dataConsumer, None, None)
f = QuartzFilter.quartzFilterWithURL_(NSURL.fileURLWithPath_("YourFltr.qfilter"))
f.applyToContext_(context)
# do your drawing
CGPDFContextClose(context)
# the PDF is in the data variable. Do whatever you need to do with the data (save to file...).
于 2010-04-29T19:41:39.647 に答える