@@ -108,7 +108,7 @@ func convertPageToGrayscale(page *model.PdfPage) error {
108108 }
109109 page .SetContentStreams ([]string {string (grayContent )}, core .NewFlateEncoder ())
110110
111- //fmt.Printf("Processed contents: %s\n", grayContent)
111+ // fmt.Printf("Processed contents: %s\n", grayContent)
112112
113113 return nil
114114}
@@ -493,7 +493,7 @@ func transformContentStreamToGrayscale(contents string, resources *model.PdfPage
493493
494494 _ , xtype := resources .GetXObjectByName (* name )
495495 if xtype == model .XObjectTypeImage {
496- //fmt.Printf(" XObject Image: %s\n", *name)
496+ // fmt.Printf(" XObject Image: %s\n", *name)
497497
498498 ximg , err := resources .GetXObjectImageByName (* name )
499499 if err != nil {
@@ -542,14 +542,19 @@ func transformContentStreamToGrayscale(contents string, resources *model.PdfPage
542542 }
543543 }
544544
545+ // If there's image Smask.
546+ if ximg .SMask != nil {
547+ ximgGray .SMask = ximg .SMask
548+ }
549+
545550 // Update the entry.
546551 err = resources .SetXObjectImageByName (* name , ximgGray )
547552 if err != nil {
548553 fmt .Printf ("Failed setting x object: %v (%s)\n " , err , string (* name ))
549554 return err
550555 }
551556 } else if xtype == model .XObjectTypeForm {
552- //fmt.Printf(" XObject Form: %s\n", *name)
557+ // fmt.Printf(" XObject Form: %s\n", *name)
553558
554559 // Go through the XObject Form content stream.
555560 xform , err := resources .GetXObjectFormByName (* name )
@@ -596,14 +601,14 @@ func transformContentStreamToGrayscale(contents string, resources *model.PdfPage
596601
597602 // For debug purposes: (high level logging).
598603 //
599- //fmt.Printf("=== Unprocessed - Full list\n")
600- //for idx, op := range operations {
604+ // fmt.Printf("=== Unprocessed - Full list\n")
605+ // for idx, op := range operations {
601606 // fmt.Printf("U. Operation %d: %s - Params: %v\n", idx+1, op.Operand, op.Params)
602- //}
603- //fmt.Printf("=== Processed - Full list\n")
604- //for idx, op := range *processedOperations {
607+ // }
608+ // fmt.Printf("=== Processed - Full list\n")
609+ // for idx, op := range *processedOperations {
605610 // fmt.Printf("P. Operation %d: %s - Params: %v\n", idx+1, op.Operand, op.Params)
606- //}
611+ // }
607612
608613 return processedOperations .Bytes (), nil
609614}
@@ -679,6 +684,20 @@ func convertShadingToGray(shading *model.PdfShading) (*model.PdfShading, error)
679684 rgbToGrayPsProgram .Append (ps .MakeOperand ("add" ))
680685 transformFunc .Program = rgbToGrayPsProgram
681686
687+ // This is a bit of a hack, but we need to keep the /Bounds to shading function even it's empty array.
688+ if shCtx , ok := shading .GetContext ().(* model.PdfShadingType2 ); ok {
689+ // Update the function bounds to grayscale.
690+ var updateDict []model.PdfFunction
691+ for _ , fc := range shCtx .Function {
692+ if fcDict , ok := core .GetDict (fc .ToPdfObject ()); ok {
693+ arr := core .MakeArray ()
694+ fcDict .Set ("Bounds" , arr )
695+ }
696+ }
697+ shCtx .Function = updateDict
698+ shading .SetContext (shCtx .PdfShading .GetContext ())
699+ }
700+
682701 // Define the DeviceN colorspace that performs the R,G,B -> Gray conversion for us.
683702 transformcs := model .NewPdfColorspaceDeviceN ()
684703 transformcs .AlternateSpace = model .NewPdfColorspaceDeviceGray ()
0 commit comments