Main
I would suggest to extract the two case branches' code into separate methods. After that your top-level functionality could be simplified like this:
using var originalPdfStream = new MemoryStream(buffer);
using var modifiedPdfStream = new MemoryStream();
var pdfReader = new PdfReader(originalPdfStream);
var pdfDocument = new PdfDocument(pdfReader, new PdfWriter(modifiedPdfStream));
EditFirstPage(pdfDocument);
EditSecondPage(pdfDocument);
pdfDocument.Close();
byte[] modifiedPdfInBytes = modifiedPdfStream.ToArray();
- I've renamed your variables to better express the intent (like
stream >> originalPdfStream)
- I've used C# 8's using declaration to reduce code indentation
- If you are using older C# version then you can only use here using statement
First page
void EditFirstPage(PdfDocument pdfDocument)
{
var canvasPage = new PdfCanvas(pdfDocument.GetFirstPage());
canvasPage.BeginText()
.SetFontAndSize(PdfFontFactory.CreateFont(Common.Font), Page1.FontSize)
.MoveText(Page1.Version.X, Page1.Version.Y)
.SetFillColor(Common.FillColor)
.ShowText(Common.VersionNumber)
.EndText();
}
- I've moved all the hard-coded values into a dedicated class called
Page1
- Those constants that are shared between page 1 and 2 have been moved to a class called
Common
static class Common
{
public const string Font = StandardFonts.HELVETICA_BOLD;
public static readonly Color FillColor = ColorConstants.WHITE;
public const string VersionNumber = "4.2021";
}
static class Page1
{
public const int FontSize = 34;
public static class Version
{
public const int X = 735, Y = 248;
}
}
Second Page
void EditSecondPage(PdfDocument pdfDocument)
{
var canvasPage = new PdfCanvas(pdfDocument.GetPage(2));
canvasPage.BeginText()
.SetFontAndSize(PdfFontFactory.CreateFont(Common.Font), Page2.FontSize)
.MoveText(Page2.Version.X, Page2.Version.Y)
.SetFillColor(Common.FillColor)
.ShowText(Common.VersionNumber)
.EndText();
var rectangle = new Rectangle(Page2.Disclaimer.X, Page2.Disclaimer.Y, Page2.Disclaimer.Width, Page2.Disclaimer.Height);
var canvas = new Canvas(canvasPage, rectangle);
var paragraph = new Paragraph()
.Add(Page2.Paragraph.Disclaimer)
.SetFont(PdfFontFactory.CreateFont(Page2.Paragraph.Font))
.SetFontColor(Page2.Paragraph.FontColor)
.SetFontSize(Page2.Paragraph.FontSize)
.SetTextAlignment(TextAlignment.LEFT);
canvas.Add(paragraph);
canvas.Close();
ImageData imageData = ImageDataFactory.Create(Page2.MapImage.FileName);
var imageRectangle = new Rectangle(Page2.MapImage.X, Page2.MapImage.Y, Page2.MapImage.Width, Page2.MapImage.Height);
canvasPage.AddImageFittedIntoRectangle(imageData, imageRectangle, true);
}
- I've renamed some variables and moved the hard-coded values into a dedicated class
static class Page2
{
public const int FontSize = 21;
public static class Version
{
public const int X = 760, Y = 573;
}
public static class Disclaimer
{
public const float X = 75, Y = 26;
public const float Width = 883, Height = 81;
}
public static class MapImage
{
public const string FileName = "PathtoImage.png";
public const float X = 111.318f, Y = 130.791f;
public const float Width = 755.454f, Height = 432.094f;
}
public static class Paragraph
{
public const string Disclaimer = "Text";
public const string Font = StandardFonts.HELVETICA;
public static readonly Color FontColor = ColorConstants.BLACK;
public const int FontSize = 12;
}
}
The code which adds the version number to a specific page is redundant. You can define a helper method to that like this:
void AddVersionNumber(PdfCanvas canvasPage, int fontSize, double x, double y)
=> canvasPage.BeginText()
.SetFontAndSize(PdfFontFactory.CreateFont(Common.Font), fontSize)
.MoveText(x, y)
.SetFillColor(Common.FillColor)
.ShowText(Common.VersionNumber)
.EndText();