diff options
Diffstat (limited to 'unipdf/pdfutil')
| -rw-r--r-- | unipdf/pdfutil/pdfutil.go | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/unipdf/pdfutil/pdfutil.go b/unipdf/pdfutil/pdfutil.go new file mode 100644 index 0000000..cbbe1d6 --- /dev/null +++ b/unipdf/pdfutil/pdfutil.go @@ -0,0 +1,116 @@ +// +// Copyright 2020 FoxyUtils ehf. All rights reserved. +// +// This is a commercial product and requires a license to operate. +// A trial license can be obtained at https://unidoc.io +// +// DO NOT EDIT: generated by unitwist Go source code obfuscator. +// +// Use of this source code is governed by the UniDoc End User License Agreement +// terms that can be accessed at https://unidoc.io/eula/ + +package pdfutil ;import (_e "errors";_g "fmt";_gc "github.com/unidoc/unipdf/v4/common";_ag "github.com/unidoc/unipdf/v4/contentstream";_f "github.com/unidoc/unipdf/v4/contentstream/draw";_eb "github.com/unidoc/unipdf/v4/core";_c "github.com/unidoc/unipdf/v4/model"; +_a "github.com/unidoc/unipdf/v4/ps";_de "os";);func _agge (_abfef *_c .PdfPattern )(*_c .PdfPattern ,error ){if _abfef .IsTiling (){_ggc :=_abfef .GetAsTilingPattern ();if _ggc .IsColored (){_abfd ,_bbgc :=_ggc .GetContentStream ();if _bbgc !=nil {return nil ,_bbgc ; +};_bgba ,_bbgc :=_gbb (string (_abfd ),_ggc .Resources );if _bbgc !=nil {return nil ,_bbgc ;};_bbgc =_ggc .SetContentStream (_bgba ,nil );if _bbgc !=nil {return nil ,_bbgc ;};_ =_ggc .ToPdfObject ();};}else if _abfef .IsShading (){_ebf :=_abfef .GetAsShadingPattern (); +_dfaa ,_ggd :=_add (_ebf .Shading );if _ggd !=nil {return nil ,_ggd ;};_ebf .Shading =_dfaa ;_ =_ebf .ToPdfObject ();};return _abfef ,nil ;}; + +// NormalizePage performs the following operations on the passed in page: +// - Normalize the page rotation. +// Rotates the contents of the page according to the Rotate entry, thus +// flattening the rotation. The Rotate entry of the page is set to nil. +// - Normalize the media box. +// If the media box of the page is offsetted (Llx != 0 or Lly != 0), +// the contents of the page are translated to (-Llx, -Lly). After +// normalization, the media box is updated (Llx and Lly are set to 0 and +// Urx and Ury are updated accordingly). +// - Normalize the crop box. +// The crop box of the page is updated based on the previous operations. +// +// After normalization, the page should look the same if openend using a +// PDF viewer. +// NOTE: This function does not normalize annotations, outlines other parts +// that are not part of the basic geometry and page content streams. +func NormalizePage (page *_c .PdfPage )error {_afd ,_ced :=page .GetMediaBox ();if _ced !=nil {return _ced ;};_fgea ,_ced :=page .GetRotate ();if _ced !=nil {_gc .Log .Debug ("\u0045\u0052R\u004f\u0052\u003a\u0020\u0025\u0073\u0020\u002d\u0020\u0069\u0067\u006e\u006f\u0072\u0069\u006e\u0067\u0020\u0061\u006e\u0064\u0020\u0061\u0073\u0073\u0075\u006d\u0069\u006e\u0067\u0020\u006e\u006f\u0020\u0072\u006f\u0074\u0061\u0074\u0069\u006f\u006e\u000a",_ced .Error ()); +};_eedb :=_fgea %360!=0&&_fgea %90==0;_afd .Normalize ();_ebfc ,_fcbb ,_ccf ,_bdc :=_afd .Llx ,_afd .Lly ,_afd .Width (),_afd .Height ();_bec :=_ebfc !=0||_fcbb !=0;if !_eedb &&!_bec {return nil ;};_aeca :=func (_abbb ,_eece ,_dea float64 )_f .BoundingBox {return _f .Path {Points :[]_f .Point {_f .NewPoint (0,0).Rotate (_dea ),_f .NewPoint (_abbb ,0).Rotate (_dea ),_f .NewPoint (0,_eece ).Rotate (_dea ),_f .NewPoint (_abbb ,_eece ).Rotate (_dea )}}.GetBoundingBox (); +};_edf :=_ag .NewContentCreator ();var _daf float64 ;if _eedb {_daf =-float64 (_fgea );_baad :=_aeca (_ccf ,_bdc ,_daf );_edf .Translate ((_baad .Width -_ccf )/2+_ccf /2,(_baad .Height -_bdc )/2+_bdc /2);_edf .RotateDeg (_daf );_edf .Translate (-_ccf /2,-_bdc /2); +_ccf ,_bdc =_baad .Width ,_baad .Height ;};if _bec {_edf .Translate (-_ebfc ,-_fcbb );};_fcg :=_edf .Operations ();_eeab ,_ced :=_eb .MakeStream (_fcg .Bytes (),_eb .NewFlateEncoder ());if _ced !=nil {return _ced ;};_eeg :=_eb .MakeArray (_eeab );_eeg .Append (page .GetContentStreamObjs ()...); +*_afd =_c .PdfRectangle {Urx :_ccf ,Ury :_bdc };if _ebe :=page .CropBox ;_ebe !=nil {_ebe .Normalize ();_gba ,_gdf ,_dcc ,_geee :=_ebe .Llx -_ebfc ,_ebe .Lly -_fcbb ,_ebe .Width (),_ebe .Height ();if _eedb {_gff :=_aeca (_dcc ,_geee ,_daf );_dcc ,_geee =_gff .Width ,_gff .Height ; +};*_ebe =_c .PdfRectangle {Llx :_gba ,Lly :_gdf ,Urx :_gba +_dcc ,Ury :_gdf +_geee };};_gc .Log .Debug ("\u0052\u006f\u0074\u0061\u0074\u0065\u003d\u0025\u0066\u00b0\u0020\u004f\u0070\u0073\u003d%\u0071 \u004d\u0065\u0064\u0069\u0061\u0042\u006f\u0078\u003d\u0025\u002e\u0032\u0066",_daf ,_fcg ,_afd ); +page .Contents =_eeg ;page .Rotate =nil ;return nil ;};func _ccec (_fbe ,_gfc *_c .PdfPageResources )(*_c .PdfPageResources ,error ){if _fbe .XObject ==nil {_fbe .XObject =_gfc .XObject ;}else {_dff ,_ :=_eb .GetDict (_fbe .XObject );if _gfc .XObject !=nil {_ddab ,_ :=_eb .GetDict (_gfc .XObject ); +for _ ,_gee :=range _ddab .Keys (){_fcdc :=_ddab .Get (_gee );_dff .Set (_gee ,_fcdc );};};};_aec ,_agb :=_fbe .GetColorspaces ();if _agb !=nil {return nil ,_agb ;};_dbg ,_agb :=_gfc .GetColorspaces ();if _agb !=nil {return nil ,_agb ;};if _aec ==nil {_fbe .SetColorSpace (_dbg ); +}else {if _dbg !=nil {for _bcac ,_eed :=range _dbg .Colorspaces {if _ ,_bfdf :=_aec .Colorspaces [_bcac ];!_bfdf {_aec .Names =append (_aec .Names ,_bcac );};_fbe .SetColorspaceByName (_eb .PdfObjectName (_bcac ),_eed );};};};if _fbe .ExtGState ==nil {_fbe .ExtGState =_gfc .ExtGState ; +}else {_bee ,_ :=_eb .GetDict (_fbe .ExtGState );if _gfc .ExtGState !=nil {_bacc ,_ :=_eb .GetDict (_gfc .ExtGState );for _ ,_ebfd :=range _bacc .Keys (){_ccae :=_bacc .Get (_ebfd );_bee .Set (_ebfd ,_ccae );};};};if _fbe .Shading ==nil {_fbe .Shading =_gfc .Shading ; +}else {_acec ,_ :=_eb .GetDict (_fbe .Shading );if _gfc .Shading !=nil {_fbc ,_ :=_eb .GetDict (_gfc .Shading );for _ ,_gea :=range _fbc .Keys (){_ec :=_fbc .Get (_gea );_acec .Set (_gea ,_ec );};};};if _fbe .Pattern ==nil {_fbe .Pattern =_gfc .Pattern ; +}else {_eda ,_ :=_eb .GetDict (_fbe .Pattern );if _gfc .Pattern !=nil {_fge ,_ :=_eb .GetDict (_gfc .Pattern );for _ ,_efee :=range _fge .Keys (){_gbc :=_fge .Get (_efee );_eda .Set (_efee ,_gbc );};};};if _fbe .Font ==nil {_fbe .Font =_gfc .Font ;}else {_dedf ,_ :=_eb .GetDict (_fbe .Font ); +if _gfc .Font !=nil {_gbeb ,_ :=_eb .GetDict (_gfc .Font );for _ ,_ffab :=range _gbeb .Keys (){_afg :=_gbeb .Get (_ffab );_dedf .Set (_ffab ,_afg );};};};if _fbe .ProcSet ==nil {_fbe .ProcSet =_gfc .ProcSet ;}else {_edg ,_ :=_eb .GetDict (_fbe .ProcSet ); +if _gfc .ProcSet !=nil {_eeb ,_ :=_eb .GetDict (_gfc .ProcSet );for _ ,_bfc :=range _eeb .Keys (){_gad :=_eeb .Get (_bfc );_edg .Set (_bfc ,_gad );};};};if _fbe .Properties ==nil {_fbe .Properties =_gfc .Properties ;}else {_dec ,_ :=_eb .GetDict (_fbe .Properties ); +if _gfc .Properties !=nil {_cge ,_ :=_eb .GetDict (_gfc .Properties );for _ ,_cab :=range _cge .Keys (){_eff :=_cge .Get (_cab );_dec .Set (_cab ,_eff );};};};return _fbe ,nil ;};func _gbb (_fa string ,_bd *_c .PdfPageResources )([]byte ,error ){_fe :=_ag .NewContentStreamParser (_fa ); +_gef ,_ebb :=_fe .Parse ();if _ebb !=nil {return nil ,_ebb ;};_gce :=&_ag .ContentStreamOperations {};_ba :=map[_eb .PdfObjectName ]bool {};_ed :=map[_eb .PdfObjectName ]bool {};_cfa :=_ag .NewContentStreamProcessor (*_gef );_cfa .AddHandler (_ag .HandlerConditionEnumAllOperands ,"",func (_cd *_ag .ContentStreamOperation ,_fb _ag .GraphicsState ,_ff *_c .PdfPageResources )error {_dd :=_cd .Operand ; +switch _dd {case "\u0043\u0053":if _ab (_fb .ColorspaceStroking ){_geb :=_cd .Params [0].(*_eb .PdfObjectName );if *_geb !="\u0050a\u0074\u0074\u0065\u0072\u006e"{_bf ,_fea :=_ff .GetColorspaceByName (*_geb );if !_fea {return _e .New ("\u0063\u006f\u006c\u006frs\u0070\u0061\u0063\u0065\u0020\u006e\u006f\u0074\u0020\u0064\u0065\u0066\u0069\u006ee\u0064"); +};_dcf ,_fea :=_bf .(*_c .PdfColorspaceSpecialPattern );if !_fea {return _e .New ("\u0074\u0079\u0070\u0065\u0020\u0065\u0072\u0072\u006f\u0072");};if _dcf .UnderlyingCS !=nil {_dcf .UnderlyingCS =_c .NewPdfColorspaceDeviceGray ();};_ebb =_ff .SetColorspaceByName (*_geb ,_dcf ); +if _ebb !=nil {return _ebb ;};};*_gce =append (*_gce ,_cd );return nil ;};_bc :=_ag .ContentStreamOperation {};_bc .Operand =_dd ;_bc .Params =[]_eb .PdfObject {_eb .MakeName ("\u0044\u0065\u0076\u0069\u0063\u0065\u0047\u0072\u0061\u0079")};*_gce =append (*_gce ,&_bc ); +return nil ;case "\u0063\u0073":if _ab (_fb .ColorspaceNonStroking ){_eg :=_cd .Params [0].(*_eb .PdfObjectName );if *_eg !="\u0050a\u0074\u0074\u0065\u0072\u006e"{_df ,_egd :=_ff .GetColorspaceByName (*_eg );if !_egd {return _e .New ("\u0063\u006f\u006c\u006frs\u0070\u0061\u0063\u0065\u0020\u006e\u006f\u0074\u0020\u0064\u0065\u0066\u0069\u006ee\u0064"); +};_af ,_egd :=_df .(*_c .PdfColorspaceSpecialPattern );if !_egd {return _e .New ("\u0074\u0079\u0070\u0065\u0020\u0065\u0072\u0072\u006f\u0072");};if _af .UnderlyingCS !=nil {_af .UnderlyingCS =_c .NewPdfColorspaceDeviceGray ();};if _fdb :=_ff .SetColorspaceByName (*_eg ,_af ); +_fdb !=nil {return _fdb ;};};*_gce =append (*_gce ,_cd );return nil ;};_baa :=_ag .ContentStreamOperation {};_baa .Operand =_dd ;_baa .Params =[]_eb .PdfObject {_eb .MakeName ("\u0044\u0065\u0076\u0069\u0063\u0065\u0047\u0072\u0061\u0079")};*_gce =append (*_gce ,&_baa ); +return nil ;case "\u0053\u0043","\u0053\u0043\u004e":if _ab (_fb .ColorspaceStroking ){_gac :=_ag .ContentStreamOperation {};_gac .Operand =_dd ;_gac .Params =[]_eb .PdfObject {};_abb ,_cfe :=_fb .ColorStroking .(*_c .PdfColorPattern );if !_cfe {return _e .New ("i\u006e\u0076\u0061\u006c\u0069\u0064 \u0073\u0074\u0072\u006f\u006b\u0069\u006e\u0067\u0020c\u006f\u006c\u006fr\u0020t\u0079\u0070\u0065"); +};if _abb .Color !=nil {_faa ,_ee :=_fb .ColorspaceStroking .ColorToRGB (_abb .Color );if _ee !=nil {return _ee ;};_aca :=_faa .(*_c .PdfColorDeviceRGB );_ace :=_aca .ToGray ();_gac .Params =append (_gac .Params ,_eb .MakeFloat (_ace .Val ()));};if _ ,_aee :=_ba [_abb .PatternName ]; +_aee {_gac .Params =append (_gac .Params ,_eb .MakeName (string (_abb .PatternName )));*_gce =append (*_gce ,&_gac );return nil ;};_ba [_abb .PatternName ]=true ;_fbf ,_bfg :=_ff .GetPatternByName (_abb .PatternName );if !_bfg {return _e .New ("\u0075\u006e\u0064\u0065fi\u006e\u0065\u0064\u0020\u0070\u0061\u0074\u0074\u0065\u0072\u006e\u0020\u006e\u0061m\u0065"); +};_efa ,_acd :=_agge (_fbf );if _acd !=nil {return _acd ;};_acd =_ff .SetPatternByName (_abb .PatternName ,_efa .ToPdfObject ());if _acd !=nil {return _acd ;};_gac .Params =append (_gac .Params ,_eb .MakeName (string (_abb .PatternName )));*_gce =append (*_gce ,&_gac ); +}else {_cce ,_ad :=_fb .ColorspaceStroking .ColorToRGB (_fb .ColorStroking );if _ad !=nil {return _ad ;};_bb :=_cce .(*_c .PdfColorDeviceRGB );_abe :=_bb .ToGray ();_cb :=_ag .ContentStreamOperation {};_cb .Operand =_dd ;_cb .Params =[]_eb .PdfObject {_eb .MakeFloat (_abe .Val ())}; +*_gce =append (*_gce ,&_cb );};return nil ;case "\u0073\u0063","\u0073\u0063\u006e":if _ab (_fb .ColorspaceNonStroking ){_gca :=_ag .ContentStreamOperation {};_gca .Operand =_dd ;_gca .Params =[]_eb .PdfObject {};_bg ,_fc :=_fb .ColorNonStroking .(*_c .PdfColorPattern ); +if !_fc {return _e .New ("i\u006e\u0076\u0061\u006c\u0069\u0064 \u0073\u0074\u0072\u006f\u006b\u0069\u006e\u0067\u0020c\u006f\u006c\u006fr\u0020t\u0079\u0070\u0065");};if _bg .Color !=nil {_fcb ,_bca :=_fb .ColorspaceNonStroking .ColorToRGB (_bg .Color ); +if _bca !=nil {return _bca ;};_dac :=_fcb .(*_c .PdfColorDeviceRGB );_bde :=_dac .ToGray ();_gca .Params =append (_gca .Params ,_eb .MakeFloat (_bde .Val ()));};if _ ,_ddf :=_ba [_bg .PatternName ];_ddf {_gca .Params =append (_gca .Params ,_eb .MakeName (string (_bg .PatternName ))); +*_gce =append (*_gce ,&_gca );return nil ;};_ba [_bg .PatternName ]=true ;_dee ,_ca :=_ff .GetPatternByName (_bg .PatternName );if !_ca {return _e .New ("\u0075\u006e\u0064\u0065fi\u006e\u0065\u0064\u0020\u0070\u0061\u0074\u0074\u0065\u0072\u006e\u0020\u006e\u0061m\u0065"); +};_dfa ,_ged :=_agge (_dee );if _ged !=nil {return _ged ;};_ged =_ff .SetPatternByName (_bg .PatternName ,_dfa .ToPdfObject ());if _ged !=nil {return _ged ;};_gca .Params =append (_gca .Params ,_eb .MakeName (string (_bg .PatternName )));*_gce =append (*_gce ,&_gca ); +}else {_bed ,_bfb :=_fb .ColorspaceNonStroking .ColorToRGB (_fb .ColorNonStroking );if _bfb !=nil {return _bfb ;};_fag :=_bed .(*_c .PdfColorDeviceRGB );_ddb :=_fag .ToGray ();_db :=_ag .ContentStreamOperation {};_db .Operand =_dd ;_db .Params =[]_eb .PdfObject {_eb .MakeFloat (_ddb .Val ())}; +*_gce =append (*_gce ,&_db );};return nil ;case "\u0052\u0047","\u004b":_beg ,_cefb :=_fb .ColorspaceStroking .ColorToRGB (_fb .ColorStroking );if _cefb !=nil {return _cefb ;};_fg :=_beg .(*_c .PdfColorDeviceRGB );_gfg :=_fg .ToGray ();_dg :=_ag .ContentStreamOperation {}; +_dg .Operand ="\u0047";_dg .Params =[]_eb .PdfObject {_eb .MakeFloat (_gfg .Val ())};*_gce =append (*_gce ,&_dg );return nil ;case "\u0072\u0067","\u006b":_dfae ,_gbbd :=_fb .ColorspaceNonStroking .ColorToRGB (_fb .ColorNonStroking );if _gbbd !=nil {return _gbbd ; +};_fcd :=_dfae .(*_c .PdfColorDeviceRGB );_gd :=_fcd .ToGray ();_dcdd :=_ag .ContentStreamOperation {};_dcdd .Operand ="\u0067";_dcdd .Params =[]_eb .PdfObject {_eb .MakeFloat (_gd .Val ())};*_gce =append (*_gce ,&_dcdd );return nil ;case "\u0073\u0068":if len (_cd .Params )!=1{return _e .New ("\u0070\u0061\u0072\u0061\u006d\u0073 \u0074\u006f\u0020\u0073\u0068\u0020\u006f\u0070\u0065\u0072\u0061\u0074\u006fr\u0020\u0073\u0068\u006f\u0075\u006c\u0064 \u0062\u0065\u0020\u0031"); +};_agg ,_aea :=_cd .Params [0].(*_eb .PdfObjectName );if !_aea {return _e .New ("\u0073\u0068 \u0070\u0061\u0072\u0061\u006d\u0065\u0074\u0065\u0072\u0020\u0073\u0068\u006f\u0075\u006c\u0064\u0020\u0062\u0065\u0020\u0061\u0020na\u006d\u0065");};if _ ,_ffa :=_ed [*_agg ]; +_ffa {*_gce =append (*_gce ,_cd );return nil ;};_ed [*_agg ]=true ;_fagg ,_fbd :=_ff .GetShadingByName (*_agg );if !_fbd {return _e .New ("\u0073\u0068\u0061\u0064\u0069\u006e\u0067\u0020\u006e\u006f\u0074\u0020\u0064\u0065\u0066i\u006ee\u0064\u0020\u0069\u006e\u0020\u0072\u0065\u0073\u006f\u0075\u0072\u0063\u0065\u0073"); +};_afc ,_bgb :=_add (_fagg );if _bgb !=nil {return _bgb ;};_bgb =_ff .SetShadingByName (*_agg ,_afc .GetContext ().ToPdfObject ());if _bgb !=nil {return _bgb ;};};*_gce =append (*_gce ,_cd );return nil ;});_cfa .AddHandler (_ag .HandlerConditionEnumOperand ,"\u0042\u0049",func (_fdg *_ag .ContentStreamOperation ,_eec _ag .GraphicsState ,_bcag *_c .PdfPageResources )error {if len (_fdg .Params )!=1{return _e .New ("\u0069\u006e\u0076\u0061l\u0069\u0064\u0020\u006e\u0075\u006d\u0062\u0065\u0072\u0020o\u0066 \u0070\u0061\u0072\u0061\u006d\u0065\u0074e\u0072\u0073"); +};_abf ,_abfe :=_fdg .Params [0].(*_ag .ContentStreamInlineImage );if !_abfe {return _e .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0069\u006el\u0069\u006e\u0065\u0020\u0069\u006d\u0061g\u0065\u0020\u0070\u0061\u0072\u0061\u006d\u0065\u0074\u0065\u0072"); +};_bbg ,_ea :=_abf .ToImage (_bcag );if _ea !=nil {return _ea ;};_aeg ,_ea :=_abf .GetColorSpace (_bcag );if _ea !=nil {return _ea ;};_gg ,_ea :=_aeg .ImageToRGB (*_bbg );if _ea !=nil {return _ea ;};_gbe :=_c .NewPdfColorspaceDeviceRGB ();_dbb ,_ea :=_gbe .ImageToGray (_gg ); +if _ea !=nil {return _ea ;};_fgb ,_ea :=_abf .GetEncoder ();if _ea !=nil {return _ea ;};if _gbee ,_ceg :=_fgb .(*_eb .DCTEncoder );_ceg {_gbee .ColorComponents =1;};_gebc ,_ea :=_ag .NewInlineImageFromImage (_dbb ,_fgb );if _ea !=nil {if _ea ==_eb .ErrUnsupportedEncodingParameters {_fgb =_eb .NewFlateEncoder (); +};_gebc ,_ea =_ag .NewInlineImageFromImage (_dbb ,_fgb );if _ea !=nil {return _ea ;};};_bgc :=_ag .ContentStreamOperation {};_bgc .Operand ="\u0042\u0049";_bgc .Params =[]_eb .PdfObject {_gebc };*_gce =append (*_gce ,&_bgc );return nil ;});_abba :=map[string ]bool {}; +_cfa .AddHandler (_ag .HandlerConditionEnumOperand ,"\u0044\u006f",func (_aa *_ag .ContentStreamOperation ,_fed _ag .GraphicsState ,_cbg *_c .PdfPageResources )error {if len (_aa .Params )< 1{return _e .New ("\u0045\u0052\u0052\u004f\u0052\u003a\u0020\u0049\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u006e\u0075\u006d\u0062\u0065r\u0020\u006f\u0066\u0020\u0070\u0061\u0072\u0061\u006ds\u0020\u0066\u006f\u0072\u0020\u0044\u006f\u0020\u006f\u0062\u006a\u0065\u0063t\u003a\u0020\u0052\u0061\u006e\u0067e\u0020\u0063\u0068e\u0063\u006b"); +};_dde :=_aa .Params [0].(*_eb .PdfObjectName );_ ,_age :=_abba [string (*_dde )];if _age {return nil ;};_abba [string (*_dde )]=true ;_ ,_cfd :=_cbg .GetXObjectByName (*_dde );if _cfd ==_c .XObjectTypeImage {_acg ,_ded :=_cbg .GetXObjectImageByName (*_dde ); +if _ded !=nil {return _ded ;};_bbf ,_ded :=_acg .ToImage ();if _ded !=nil {return _ded ;};_dfad ,_ded :=_acg .ColorSpace .ImageToRGB (*_bbf );if _ded !=nil {return _ded ;};_bdd :=_c .NewPdfColorspaceDeviceRGB ();_fab ,_ded :=_bdd .ImageToGray (_dfad ); +if _ded !=nil {return _ded ;};_bac :=_acg .Filter ;if _gcf ,_bfd :=_bac .(*_eb .DCTEncoder );_bfd {_gcf .ColorComponents =1;};_feb ,_ded :=_c .NewXObjectImageFromImage (&_fab ,nil ,_bac );if _ded !=nil {if _ded ==_eb .ErrUnsupportedEncodingParameters {_bac =_eb .NewFlateEncoder (); +};_feb ,_ded =_c .NewXObjectImageFromImage (&_fab ,nil ,_bac );if _ded !=nil {return _ded ;};};_ded =_cbg .SetXObjectImageByName (*_dde ,_feb );if _ded !=nil {return _ded ;};}else if _cfd ==_c .XObjectTypeForm {_efe ,_eaa :=_cbg .GetXObjectFormByName (*_dde ); +if _eaa !=nil {return _eaa ;};_eee ,_eaa :=_efe .GetContentStream ();if _eaa !=nil {return _eaa ;};_ada :=_efe .Resources ;if _ada ==nil {_ada =_cbg ;};_feg ,_eaa :=_gbb (string (_eee ),_ada );if _eaa !=nil {return _eaa ;};_eaa =_efe .SetContentStream (_feg ,nil ); +if _eaa !=nil {return _eaa ;};_eaa =_cbg .SetXObjectFormByName (*_dde ,_efe );if _eaa !=nil {return _eaa ;};};return nil ;});_ebb =_cfa .Process (_bd );if _ebb !=nil {return nil ,_ebb ;};return _gce .Bytes (),nil ;};func _add (_def *_c .PdfShading )(*_c .PdfShading ,error ){_gcff :=_def .ColorSpace ; +if _gcff .GetNumComponents ()==1{return _def ,nil ;}else if _gcff .GetNumComponents ()==3{_ccb :=&_c .PdfFunctionType4 {};_ccb .Domain =[]float64 {0,1,0,1,0,1};_ccb .Range =[]float64 {0,1};_fba :=_a .NewPSProgram ();_fba .Append (_a .MakeReal (0.11));_fba .Append (_a .MakeOperand ("\u006d\u0075\u006c")); +_fba .Append (_a .MakeOperand ("\u0065\u0078\u0063\u0068"));_fba .Append (_a .MakeReal (0.59));_fba .Append (_a .MakeOperand ("\u006d\u0075\u006c"));_fba .Append (_a .MakeOperand ("\u0061\u0064\u0064"));_fba .Append (_a .MakeOperand ("\u0065\u0078\u0063\u0068")); +_fba .Append (_a .MakeReal (0.3));_fba .Append (_a .MakeOperand ("\u006d\u0075\u006c"));_fba .Append (_a .MakeOperand ("\u0061\u0064\u0064"));_ccb .Program =_fba ;_egg :=_c .NewPdfColorspaceDeviceN ();_egg .AlternateSpace =_c .NewPdfColorspaceDeviceGray (); +_egg .ColorantNames =_eb .MakeArray (_eb .MakeName ("\u0052"),_eb .MakeName ("\u0047"),_eb .MakeName ("\u0042"));_egg .TintTransform =_ccb ;_def .ColorSpace =_egg ;return _def ,nil ;}else if _gcff .GetNumComponents ()==4{_fgd :=&_c .PdfFunctionType4 {}; +_fgd .Domain =[]float64 {0,1,0,1,0,1,0,1};_fgd .Range =[]float64 {0,1};_dae :=_a .NewPSProgram ();_dae .Append (_a .MakeOperand ("\u0065\u0078\u0063\u0068"));_dae .Append (_a .MakeReal (0.11));_dae .Append (_a .MakeOperand ("\u006d\u0075\u006c"));_dae .Append (_a .MakeOperand ("\u0061\u0064\u0064")); +_dae .Append (_a .MakeOperand ("\u0065\u0078\u0063\u0068"));_dae .Append (_a .MakeReal (0.59));_dae .Append (_a .MakeOperand ("\u006d\u0075\u006c"));_dae .Append (_a .MakeOperand ("\u0061\u0064\u0064"));_dae .Append (_a .MakeOperand ("\u0065\u0078\u0063\u0068")); +_dae .Append (_a .MakeReal (0.30));_dae .Append (_a .MakeOperand ("\u006d\u0075\u006c"));_dae .Append (_a .MakeOperand ("\u0061\u0064\u0064"));_dae .Append (_a .MakeOperand ("\u0064\u0075\u0070"));_dae .Append (_a .MakeReal (1.0));_dae .Append (_a .MakeOperand ("\u0067\u0065")); +_bfe :=_a .NewPSProgram ();_bfe .Append (_a .MakeOperand ("\u0070\u006f\u0070"));_bfe .Append (_a .MakeReal (1.0));_dae .Append (_bfe );_dae .Append (_a .MakeOperand ("\u0069\u0066"));_fgd .Program =_dae ;_fad :=_c .NewPdfColorspaceDeviceN ();_fad .AlternateSpace =_c .NewPdfColorspaceDeviceGray (); +_fad .ColorantNames =_eb .MakeArray (_eb .MakeName ("\u0043"),_eb .MakeName ("\u004d"),_eb .MakeName ("\u0059"),_eb .MakeName ("\u004b"));_fad .TintTransform =_fgd ;_def .ColorSpace =_fad ;return _def ,nil ;}else {return nil ,_e .New ("\u0075\u006e\u0073\u0075\u0070\u0070\u006fr\u0074\u0065\u0064 \u0070\u0061\u0074t\u0065\u0072n\u0020\u0063\u006f\u006c\u006f\u0072s\u0070ac\u0065\u0020\u0066\u006f\u0072\u0020\u0067\u0072\u0061\u0079\u0073\u0063\u0061\u006c\u0065\u0020\u0063\u006f\u006e\u0076\u0065\u0072\u0073\u0069\u006f\u006e"); +};}; + +// ExtractPageRange extracts a range of pages from inputPath PDF file and saves it as outputPath PDF file. +// keepOptionalContent controls if we need to process OC properties (rarely needed). +func ExtractPageRange (inputPath string ,outputPath string ,pageFrom int ,pageTo int ,keepOptionalContent bool )error {_ge :=_c .NewPdfWriter ();_ac ,_gf ,_cf :=_c .NewPdfReaderFromFile (inputPath ,nil );if _cf !=nil {return _cf ;};defer _gf .Close (); +_ef ,_cf :=_ac .GetNumPages ();if _cf !=nil {return _cf ;};if _ef < pageTo {return _g .Errorf ("n\u0075\u006d\u0050\u0061\u0067\u0065s\u0020\u0028\u0025\u0064\u0029\u0020\u003c\u0020\u0070a\u0067\u0065\u0054o\u0020(\u0025\u0064\u0029",_ef ,pageTo );};if keepOptionalContent {_ce ,_b :=_ac .GetOCProperties (); +if _b !=nil {return _b ;};_ge .SetOCProperties (_ce );};for _dc :=pageFrom ;_dc <=pageTo ;_dc ++{_efc :=_dc ;_cef ,_da :=_ac .GetPage (_efc );if _da !=nil {return _da ;};_da =_ge .AddPage (_cef );if _da !=nil {return _da ;};};_gb ,_cf :=_de .Create (outputPath ); +if _cf !=nil {return _cf ;};defer _gb .Close ();_cf =_ge .Write (_gb );if _cf !=nil {return _cf ;};return nil ;};func _ab (_deg _c .PdfColorspace )bool {_ ,_fd :=_deg .(*_c .PdfColorspaceSpecialPattern );return _fd }; + +// ConvertPageToGrayscale() replaces color objects on the page with grayscale ones. Also references XObject Images and Forms +// to convert those to grayscale. +func ConvertPageToGrayscale (page *_c .PdfPage )error {_ae ,_cc :=page .GetAllContentStreams ();if _cc !=nil {return _cc ;};_be ,_cc :=_gbb (_ae ,page .Resources );if _cc !=nil {return _cc ;};_cc =page .SetContentStreams ([]string {string (_be )},_eb .NewFlateEncoder ()); +if _cc !=nil {return _cc ;};return nil ;}; + +// MergePdf merges several PDF files passed as inputPaths to one PDF file, outputPath. +// isMergingForms controls if it is necessary to merge AcroForms from input files. +// If you are not sure, you can always set isMergingForms to true. +func MergePdf (inputPaths []string ,outputPath string ,isMergingForms bool )error {_cdf :=_c .NewPdfWriter ();var _fdc *_c .PdfAcroForm ;for _eea ,_dcg :=range inputPaths {_cee ,_egb ,_abeb :=_c .NewPdfReaderFromFile (_dcg ,nil );if _abeb !=nil {return _abeb ; +};defer _egb .Close ();_afa ,_abeb :=_cee .GetNumPages ();if _abeb !=nil {return _abeb ;};for _gcab :=range _afa {_fff :=_gcab +1;_cefa ,_acdc :=_cee .GetPage (_fff );if _acdc !=nil {return _acdc ;};_acdc =_cdf .AddPage (_cefa );if _acdc !=nil {return _acdc ; +};};if isMergingForms &&_cee .AcroForm !=nil {if _fdc ==nil {_fdc =_cee .AcroForm ;}else {_fdc ,_abeb =_bbe (_fdc ,_cee .AcroForm ,_eea +1);if _abeb !=nil {return _abeb ;};};};};_aac ,_dda :=_de .Create (outputPath );if _dda !=nil {return _dda ;};defer _aac .Close (); +if _fdc !=nil {_cdf .SetForms (_fdc );};_dda =_cdf .Write (_aac );if _dda !=nil {return _dda ;};return nil ;};func _bbe (_cff ,_ebbe *_c .PdfAcroForm ,_gfa int )(*_c .PdfAcroForm ,error ){if _cff .NeedAppearances ==nil {_cff .NeedAppearances =_ebbe .NeedAppearances ; +};if _cff .SigFlags ==nil {_cff .SigFlags =_ebbe .SigFlags ;};if _cff .CO ==nil {_cff .CO =_ebbe .CO ;};if _cff .DR ==nil {_cff .DR =_ebbe .DR ;}else if _ebbe .DR !=nil {_aaa ,_afb :=_ccec (_cff .DR ,_ebbe .DR );if _afb !=nil {return nil ,_afb ;};_cff .DR =_aaa ; +};if _cff .DA ==nil {_cff .DA =_ebbe .DA ;};if _cff .Q ==nil {_cff .Q =_ebbe .Q ;};if _cff .XFA ==nil {_cff .XFA =_ebbe .XFA ;}else {if _ebbe .XFA !=nil {_gc .Log .Debug ("TO\u0044\u004f\u003a\u0020\u0048\u0061\u006edl\u0065\u0020\u0058\u0046\u0041\u0020\u006d\u0065\u0072\u0067\u0069\u006e\u0067\u0020\u002d \u0043\u0075\u0072\u0072\u0065\u006e\u0074\u006c\u0079\u0020\u006a\u0075\u0073\u0074\u0020\u0075\u0073\u0069\u006e\u0067 \u0066\u0069\u0072\u0073\u0074\u0020o\u006e\u0065\u0020\u0074\u0068\u0061\u0074\u0020\u0069s\u0020e\u006e\u0063\u006f\u0075\u006e\u0074\u0065\u0072\u0065\u0064"); +};};if _cff .Fields ==nil {_cff .Fields =_ebbe .Fields ;}else {_afgc :=_c .NewPdfField ();_afgc .T =_eb .MakeString (_g .Sprintf ("\u0064\u006f\u0063%\u0064",_gfa ));_afgc .Kids =[]*_c .PdfField {};if _ebbe .Fields !=nil {for _ ,_gae :=range *_ebbe .Fields {_gae .Parent =_afgc ; +_afgc .Kids =append (_afgc .Kids ,_gae );};};*_cff .Fields =append (*_cff .Fields ,_afgc );};return _cff ,nil ;};
\ No newline at end of file |
