unipdf/pdfutil/pdfutil.go

36 lines
3.5 KiB
Go
Raw Normal View History

2020-09-14 09:32:45 +00:00
//
// 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/
2024-05-29 17:04:37 +00:00
package pdfutil ;import (_a "github.com/unidoc/unipdf/v3/common";_gf "github.com/unidoc/unipdf/v3/contentstream";_df "github.com/unidoc/unipdf/v3/contentstream/draw";_g "github.com/unidoc/unipdf/v3/core";_c "github.com/unidoc/unipdf/v3/model";);
2020-09-14 09:32:45 +00:00
2020-09-21 01:20:10 +00:00
// NormalizePage performs the following operations on the passed in page:
2024-03-27 22:34:33 +00:00
// - 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.
//
2020-09-14 09:32:45 +00:00
// After normalization, the page should look the same if openend using a
// PDF viewer.
2020-10-19 10:58:10 +00:00
// NOTE: This function does not normalize annotations, outlines other parts
// that are not part of the basic geometry and page content streams.
2024-05-29 17:04:37 +00:00
func NormalizePage (page *_c .PdfPage )error {_de ,_dg :=page .GetMediaBox ();if _dg !=nil {return _dg ;};_dc ,_dg :=page .GetRotate ();if _dg !=nil {_a .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",_dg .Error ());
};_gc :=_dc %360!=0&&_dc %90==0;_de .Normalize ();_ag ,_e ,_ee ,_cg :=_de .Llx ,_de .Lly ,_de .Width (),_de .Height ();_ge :=_ag !=0||_e !=0;if !_gc &&!_ge {return nil ;};_gd :=func (_ce ,_eb ,_f float64 )_df .BoundingBox {return _df .Path {Points :[]_df .Point {_df .NewPoint (0,0).Rotate (_f ),_df .NewPoint (_ce ,0).Rotate (_f ),_df .NewPoint (0,_eb ).Rotate (_f ),_df .NewPoint (_ce ,_eb ).Rotate (_f )}}.GetBoundingBox ();
};_fd :=_gf .NewContentCreator ();var _dfb float64 ;if _gc {_dfb =-float64 (_dc );_deb :=_gd (_ee ,_cg ,_dfb );_fd .Translate ((_deb .Width -_ee )/2+_ee /2,(_deb .Height -_cg )/2+_cg /2);_fd .RotateDeg (_dfb );_fd .Translate (-_ee /2,-_cg /2);_ee ,_cg =_deb .Width ,_deb .Height ;
};if _ge {_fd .Translate (-_ag ,-_e );};_eg :=_fd .Operations ();_fb ,_dg :=_g .MakeStream (_eg .Bytes (),_g .NewFlateEncoder ());if _dg !=nil {return _dg ;};_fbd :=_g .MakeArray (_fb );_fbd .Append (page .GetContentStreamObjs ()...);*_de =_c .PdfRectangle {Urx :_ee ,Ury :_cg };
if _af :=page .CropBox ;_af !=nil {_af .Normalize ();_cgg ,_ae ,_fe ,_fec :=_af .Llx -_ag ,_af .Lly -_e ,_af .Width (),_af .Height ();if _gc {_db :=_gd (_fe ,_fec ,_dfb );_fe ,_fec =_db .Width ,_db .Height ;};*_af =_c .PdfRectangle {Llx :_cgg ,Lly :_ae ,Urx :_cgg +_fe ,Ury :_ae +_fec };
};_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",_dfb ,_eg ,_de );page .Contents =_fbd ;page .Rotate =nil ;
2021-09-23 22:37:42 +00:00
return nil ;};