146 lines
4.8 KiB
C#
146 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using iTextSharp.text;
|
|
using iTextSharp.text.pdf;
|
|
using System.IO;
|
|
using Microsoft.Win32;
|
|
|
|
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)
|
|
{
|
|
RegisterFont(fontName);
|
|
return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize, fontStyle, fontColor);
|
|
}
|
|
public static void RegisterFont(string fontName)
|
|
{
|
|
if (!iTextSharp.text.FontFactory.IsRegistered(fontName))
|
|
{
|
|
foreach (string name in FontKey.GetValueNames())
|
|
{
|
|
if (name.StartsWith(fontName))
|
|
{
|
|
string fontFile = (string)FontKey.GetValue(name);
|
|
iTextSharp.text.FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
private static RegistryKey _FontKey = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows NT").OpenSubKey("CurrentVersion").OpenSubKey("Fonts");
|
|
public static RegistryKey FontKey
|
|
{ get { return _FontKey; } }
|
|
private static string _FontFolder = (String)Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("Shell Folders").GetValue("Fonts");
|
|
public static string FontFolder
|
|
{ get { return _FontFolder; } }
|
|
}
|
|
}
|