unipdf/pdfutil/pdfutil.go
2020-09-28 23:18:17 +00:00

27 lines
2.9 KiB
Go

//
// 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 (_a "github.com/unidoc/unipdf/v3/common";_cd "github.com/unidoc/unipdf/v3/contentstream";_c "github.com/unidoc/unipdf/v3/contentstream/draw";_b "github.com/unidoc/unipdf/v3/model";);
// 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.
func NormalizePage (page *_b .PdfPage )error {_cc ,_fd :=page .GetMediaBox ();if _fd !=nil {return _fd ;};_e ,_fd :=page .GetContentStreams ();if _fd !=nil {return _fd ;};_fdb :=page .Rotate ;_d :=_fdb !=nil &&*_fdb !=0&&*_fdb %90==0;_cc .Normalize ();_df ,_cdf ,_cg ,_be :=_cc .Llx ,_cc .Lly ,_cc .Width (),_cc .Height ();_g :=_df !=0||_cdf !=0;if !_d &&!_g {return nil ;};_fb :=func (_fbe ,_ab ,_cga float64 )_c .BoundingBox {return _c .Path {Points :[]_c .Point {_c .NewPoint (0,0).Rotate (_cga ),_c .NewPoint (_fbe ,0).Rotate (_cga ),_c .NewPoint (0,_ab ).Rotate (_cga ),_c .NewPoint (_fbe ,_ab ).Rotate (_cga )}}.GetBoundingBox ();};_db :=_cd .NewContentCreator ();var _bg float64 ;if _d {_bg =-float64 (*page .Rotate );_gg :=_fb (_cg ,_be ,_bg );_db .Translate ((_gg .Width -_cg )/2+_cg /2,(_gg .Height -_be )/2+_be /2);_db .RotateDeg (_bg );_db .Translate (-_cg /2,-_be /2);_cg ,_be =_gg .Width ,_gg .Height ;};if _g {_db .Translate (-_df ,-_cdf );};_bed :=_db .Operations ().String ();_e =append ([]string {_bed },_e ...);if _fd =page .SetContentStreams (_e ,nil );_fd !=nil {return _fd ;};*_cc =_b .PdfRectangle {Urx :_cg ,Ury :_be };if _ac :=page .CropBox ;_ac !=nil {_ac .Normalize ();_ga ,_fdbd ,_dfa ,_fg :=_ac .Llx -_df ,_ac .Lly -_cdf ,_ac .Width (),_ac .Height ();if _d {_gc :=_fb (_dfa ,_fg ,_bg );_dfa ,_fg =_gc .Width ,_gc .Height ;};*_ac =_b .PdfRectangle {Llx :_ga ,Lly :_fdbd ,Urx :_ga +_dfa ,Ury :_fdbd +_fg };};_a .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",_bg ,_bed ,_cc );page .Rotate =nil ;return nil ;};