Skip to content

Instantly share code, notes, and snippets.

@ejmudrak
Created November 16, 2023 15:22
Show Gist options
  • Select an option

  • Save ejmudrak/32a240040a87b0e23a2b8239353c1dfd to your computer and use it in GitHub Desktop.

Select an option

Save ejmudrak/32a240040a87b0e23a2b8239353c1dfd to your computer and use it in GitHub Desktop.
Change document text color to black
# set_text_color.py
# import utils to load required shared libraries
from utils import inputPath, outputPath
from pdfixsdk.Pdfix import *
def SetTextColor(page: PdfPage, color: PdfColor):
pDoc = page.GetDoc()
content = page.GetContent()
for i in range(content.GetNumObjects()):
content_obj = content.GetObject(i)
if content_obj.GetObjectType() is kPdsPageText:
text_obj = content_obj
text_state = text_obj.GetTextState()
text_state.color_state.fill_color = color
text_obj.SetTextState(text_state)
pdfix = GetPdfix()
if pdfix is None:
raise Exception('Pdfix Initialization fail')
doc = pdfix.OpenDoc(outputPath + "/alt_text.pdf", "")
if doc is None:
raise Exception('Unable to open pdf : ' + pdfix.GetError())
# Creates black color
rgb_color_space = doc.CreateColorSpace(kColorSpaceDeviceRGB)
black = rgb_color_space.CreateColor()
black.SetValue(0,0) # sets red
black.SetValue(1,0) # sets green
black.SetValue(2,0) # sets blue
for i in range(doc.GetNumPages()):
page = doc.AcquirePage(i)
SetTextColor(page, black)
page.SetContent()
page.Release()
if not doc.Save(outputPath + "/output.pdf", kSaveFull):
raise Exception(pdfix.GetError())
doc.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment