Skip to content

Commit 09a0ef9

Browse files
committed
Refactor SetMetadata method
1 parent b45a5c3 commit 09a0ef9

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

Libraries/Itext/Sources/Internal/Writer.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,33 +178,31 @@ private void SetMetadata(Metadata src, PdfDocument dest)
178178
.SetKeywords(src.Keywords)
179179
.SetCreator(src.Creator);
180180

181-
var pl = src.Options.ToPageLayout();
182-
if (pl != ViewerOption.None)
181+
if (src.Options == ViewerOption.None) return;
182+
183+
var catalog = dest.GetCatalog() ?? throw new ArgumentNullException("PdfCatalog");
184+
185+
var layout = src.Options.ToPageLayout();
186+
if (layout != ViewerOption.None)
183187
{
184-
PdfName pageLayout = new(pl.ToName());
185-
_ = dest.GetCatalog().SetPageLayout(pageLayout);
188+
_ = catalog.SetPageLayout(new(layout.ToName()));
186189

187-
var viewerPreferences = dest.GetCatalog().GetViewerPreferences();
188-
if (viewerPreferences is null)
190+
var vp = catalog.GetViewerPreferences();
191+
if (vp is null)
189192
{
190-
// Init ViewerPreferences
191-
viewerPreferences = new PdfViewerPreferences();
192-
dest.GetCatalog().SetViewerPreferences(viewerPreferences);
193+
vp = new PdfViewerPreferences();
194+
catalog.SetViewerPreferences(vp);
193195
}
194196

195197
// Add Direction to ViewerPreferences
196-
if (pl == ViewerOption.TwoColumnRight || pl == ViewerOption.TwoPageRight)
197-
{
198-
viewerPreferences.SetDirection(PdfViewerPreferences.PdfViewerPreferencesConstants.RIGHT_TO_LEFT);
199-
}
200-
else
201-
{
202-
viewerPreferences.SetDirection(PdfViewerPreferences.PdfViewerPreferencesConstants.LEFT_TO_RIGHT);
203-
}
198+
var direction = layout == ViewerOption.TwoColumnRight || layout == ViewerOption.TwoPageRight ?
199+
PdfViewerPreferences.PdfViewerPreferencesConstants.RIGHT_TO_LEFT :
200+
PdfViewerPreferences.PdfViewerPreferencesConstants.LEFT_TO_RIGHT ;
201+
vp.SetDirection(direction);
204202
}
205203

206-
var pm = src.Options.ToPageMode();
207-
if (pm != ViewerOption.None) _ = dest.GetCatalog().SetPageMode(new(pm.ToName()));
204+
var mode = src.Options.ToPageMode();
205+
if (mode != ViewerOption.None) _ = catalog.SetPageMode(new(mode.ToName()));
208206
}
209207

210208
/* ----------------------------------------------------------------- */

0 commit comments

Comments
 (0)