130 lines
4.1 KiB
C#
130 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using iTextSharp.text;
|
|
using iTextSharp.text.pdf;
|
|
using System.IO;
|
|
using Microsoft.Win32;
|
|
using Volian.Base.Library;
|
|
|
|
namespace Volian.Print.Library
|
|
{
|
|
public class pdf
|
|
{
|
|
private Document _MyDocument;
|
|
public Document MyDocument
|
|
{
|
|
get { return _MyDocument; }
|
|
set { _MyDocument = value; }
|
|
}
|
|
private PdfWriter _MyWriter;
|
|
public PdfWriter MyWriter
|
|
{
|
|
get { return _MyWriter; }
|
|
set { _MyWriter = value; }
|
|
}
|
|
private string _MyFileName;
|
|
public string MyFileName
|
|
{
|
|
get { return _MyFileName; }
|
|
set { _MyFileName = value; }
|
|
}
|
|
public PdfContentByte ContentByte
|
|
{
|
|
get { return _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 Color _WatermarkColor = Color.BLUE;
|
|
private 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();
|
|
gs2.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();
|
|
gState.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);
|
|
}
|
|
}
|
|
}
|