PDF文件處理利器PDFSharp使用簡介

        最近因工作需要根據需求將一個圖片插入到新建的PDF文件中,翻閱了不少資料,發現有ITestSharpPDFSharp,經過比較發現PDFSharp效率比較不錯,適合網站使用,如果有些需求搞不定考慮兩者同時使用互補。

 

  官網:http://www.pdfsharp.net/wiki/(S(5zdh0aejiwpjz1nhkty33bz5))/Default.aspx?AspxAutoDetectCookieSupport=1

  示例:http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx

 

使用說明

  下載使用release版本的pdfSharp.dll,然後建立如下兩個類:

  

  /// <summary>
    /// The base class with some helper functions.
    /// </summary>
    public class Base
    {
        protected XColor backColor;
        protected XColor backColor2;
        protected XColor shadowColor;
        protected double borderWidth;
        protected XPen borderPen;
        protected static PdfDocument s_document { get; set; }

        protected Base(PdfDocument document)
        {
            s_document = document;
            this.backColor = XColors.Ivory;
            this.backColor2 = XColors.WhiteSmoke;

            this.backColor = XColor.FromArgb(212, 224, 240);
            this.backColor2 = XColor.FromArgb(253, 254, 254);

            this.shadowColor = XColors.Gainsboro;
            this.borderWidth = 4.5;
            this.borderPen = new XPen(XColor.FromArgb(94, 118, 151), this.borderWidth);//new XPen(XColors.SteelBlue, this.borderWidth);
        }

        /// <summary>
        /// Draws the page title and footer.
        /// </summary>
        public void DrawTitle(PdfPage page, XGraphics gfx, string title)
        {
            XRect rect = new XRect(new XPoint(), gfx.PageSize);
            rect.Inflate(-10, -15);
            XFont font = new XFont("Verdana", 14, XFontStyle.Bold);
            gfx.DrawString(title, font, XBrushes.MidnightBlue, rect, XStringFormats.TopCenter);

            rect.Offset(0, 5);
            font = new XFont("Verdana", 8, XFontStyle.Italic);
            XStringFormat format = new XStringFormat();
            format.Alignment = XStringAlignment.Near;
            format.LineAlignment = XLineAlignment.Far;
            gfx.DrawString("Created with " + PdfSharp.ProductVersionInfo.Producer, font, XBrushes.DarkOrchid, rect, format);

            font = new XFont("Verdana", 8);
            format.Alignment = XStringAlignment.Center;
            gfx.DrawString(s_document.PageCount.ToString(), font, XBrushes.DarkOrchid, rect, format);

            s_document.Outlines.Add(title, page, true);
        }

        /// <summary>
        /// Draws a sample box.
        /// </summary>
        public void BeginBox(XGraphics gfx, int number, string title)
        {
            const int dEllipse = 15;
            XRect rect = new XRect(0, 20, 300, 200);
            if (number % 2 == 0)
                rect.X = 300 - 5;
            rect.Y = 40 + ((number - 1) / 2) * (200 - 5);
            rect.Inflate(-10, -10);
            XRect rect2 = rect;
            rect2.Offset(this.borderWidth, this.borderWidth);
            gfx.DrawRoundedRectangle(new XSolidBrush(this.shadowColor), rect2, new XSize(dEllipse + 8, dEllipse + 8));
            XLinearGradientBrush brush = new XLinearGradientBrush(rect, this.backColor, this.backColor2, XLinearGradientMode.Vertical);
            gfx.DrawRoundedRectangle(this.borderPen, brush, rect, new XSize(dEllipse, dEllipse));
            rect.Inflate(-5, -5);

            XFont font = new XFont("Verdana", 12, XFontStyle.Regular);
            gfx.DrawString(title, font, XBrushes.Navy, rect, XStringFormats.TopCenter);

            rect.Inflate(-10, -5);
            rect.Y += 20;
            rect.Height -= 20;
            //gfx.DrawRectangle(XPens.Red, rect);

            this.state = gfx.Save();
            gfx.TranslateTransform(rect.X, rect.Y);
        }

        public void EndBox(XGraphics gfx)
        {
            gfx.Restore(this.state);
        }

        /// <summary>
        /// Gets a five-pointed star with the specified size and center.
        /// </summary>
        protected static XPoint[] GetPentagram(double size, XPoint center)
        {
            XPoint[] points = Pentagram.Clone() as XPoint[];
            for (int idx = 0; idx < 5; idx++)
            {
                points[idx].X = points[idx].X * size + center.X;
                points[idx].Y = points[idx].Y * size + center.Y;
            }
            return points;
        }

        /// <summary>
        /// Gets a normalized five-pointed star.
        /// </summary>
        static XPoint[] Pentagram
        {
            get
            {
                if (pentagram == null)
                {
                    int[] order = new int[] { 0, 3, 1, 4, 2 };
                    pentagram = new XPoint[5];
                    for (int idx = 0; idx < 5; idx++)
                    {
                        double rad = order[idx] * 2 * Math.PI / 5 - Math.PI / 10;
                        pentagram[idx].X = Math.Cos(rad);
                        pentagram[idx].Y = Math.Sin(rad);
                    }
                }
                return pentagram;
            }
        }
        static XPoint[] pentagram;

        XGraphicsState state;
    }
 PDFImage類

 public class PDFSharpImages : Base
    {

        public PDFSharpImages(PdfDocument documnet)
            : base(documnet)
        {
        }

        public void DrawImage(XGraphics gfx,string jpegSamplePath)
        {
            // BeginBox(gfx, number, "DrawImage (original)");
            
            
            XImage image = XImage.FromFile(jpegSamplePath);
            double x = (gfx.PageSize.Width - image.PixelWidth * 72 /  image.HorizontalResolution)/2;
            double y = (gfx.PageSize.Height - image.PixelHeight*72/image.VerticalResolution) / 2;
            gfx.DrawImage(image, x,y);

          //  EndBox(gfx);
        }
}

使用示例

 

class Program
    {
        static void Main(string[] args)
        {
            #region image Sample
            string filename = "HelloWorld.pdf";
            PdfDocument document = new PdfDocument();
            string path = @"H:\開源項目\PDFSharp\PDFTest\PDFTest\bin\Debug\Images\Test (OS2).bmp";
            //// Create an empty page
            //Image image = Image.FromFile(path);
            PDFSharpImages PDFImage = new PDFSharpImages(document);
            //PDFImage.DrawImage(document, image,10, 10);
            PdfPage page = document.AddPage();
            page.Size = PdfSharp.PageSize.A4;
            
            //    ////// Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);
            PDFImage.DrawImage(gfx, path);
            document.Save(filename);
            //// ...and start a viewer.
            Process.Start(filename);
           #endregion
        }
    }

 歡迎推薦一些開源的小系統,要求是能用,好用,方便,我有時間研究研究同時寫出使用心得,爲後來者快速入手帶來幫助。


發佈了32 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章