Files
SourceCode/PROMS/Volian.Print.Library/pdf.cs
T

111 lines
3.9 KiB
C#

using System;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using Volian.Base.Library;
namespace Volian.Print.Library
{
public class pdf
{
public Document MyDocument { get; set; }
public PdfWriter MyWriter { get; set; }
public string MyFileName { get; set; }
public PdfContentByte ContentByte => MyWriter.DirectContent;
public pdf(string myFileName)
{
MyFileName = myFileName;
MyDocument = new Document(PageSize.LETTER);
try
{
MyWriter = PdfWriter.GetInstance(MyDocument, new FileStream(myFileName, FileMode.Create));
MyWriter.SetAtLeastPdfVersion(PdfWriter.VERSION_1_7);
}
catch (Exception ex)
{
Console.WriteLine("{0} - {1}\r\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace);
}
}
public pdf(string myFileName, Rectangle mySize)
{
MyFileName = myFileName;
MyDocument = new Document(mySize);
try
{
MyWriter = PdfWriter.GetInstance(MyDocument, new FileStream(myFileName, FileMode.Create));
MyWriter.SetAtLeastPdfVersion(PdfWriter.VERSION_1_7);
}
catch (Exception ex)
{
Console.WriteLine("{0} - {1}\r\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace);
}
}
public void Close() => MyDocument.Close();
private readonly Color _WatermarkColor = Color.BLUE;
private readonly float _WatermarkOpacity = .10F;
public void WatermarkPageNumber(int pageNumber)
{
Rectangle pageSize = ContentByte.PdfWriter.PageSize; // Get page size
BaseFont myBasefont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, BaseFont.EMBEDDED);
string strPage = pageNumber.ToString();
float h = myBasefont.GetAscentPoint(strPage, 10);
float w = myBasefont.GetWidthPoint(strPage, 10);
float fontSizeH = 10 * pageSize.Height / h;
float fontSizeW = 10 * pageSize.Width / w;
float myFontSize = fontSizeH < fontSizeW ? fontSizeH : fontSizeW;
ContentByte.SaveState();
ContentByte.BeginText();
ContentByte.SetPatternFill(PatternPainter);
ContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
ContentByte.SetLineWidth(.5F);
ContentByte.SetFontAndSize(myBasefont, myFontSize);// Set font and size
ContentByte.SetColorStroke(_WatermarkColor);
PdfGState gs2 = new PdfGState
{
StrokeOpacity = _WatermarkOpacity
};
ContentByte.SetGState(gs2);
ContentByte.ShowTextAlignedKerned(PdfContentByte.ALIGN_CENTER, strPage, pageSize.Width / 2, 0, 0);// Draw the text
ContentByte.EndText();
ContentByte.FillStroke(); // Controlled by TextRenderMode above
ContentByte.RestoreState();
}
private PdfPatternPainter _PatternPainter;
public PdfPatternPainter PatternPainter
{
get
{
if (_PatternPainter == null)
_PatternPainter = SetSquareDotPattern(2);
return _PatternPainter;
}
}
public PdfPatternPainter SetSquareDotPattern(float radius)
{
PdfPatternPainter myPatternPainter;
myPatternPainter = ContentByte.CreatePattern(radius * 4, radius * 2, radius * 4, radius * 2);
PdfGState gState = new PdfGState
{
FillOpacity = _WatermarkOpacity
};
myPatternPainter.SetGState(gState);
myPatternPainter.SetColorFill(_WatermarkColor);
myPatternPainter.Rectangle(0, 0, radius, radius);
myPatternPainter.Rectangle(radius * 2, radius, radius, radius);
myPatternPainter.Fill();
return myPatternPainter;
}
public static iTextSharp.text.Font GetFont(string fontName, float fontSize, int fontStyle, Color fontColor)
{
// B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont(fontName);
return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize, fontStyle, fontColor);
}
}
}