Ejemplo n.º 1
0
        private void TransformImage(PdfPageBase page)
        {
            PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\SalesReportChart.png");
            int skewX = 20;
            int skewY = 20;
            float scaleX = 0.2f;
            float scaleY = 0.6f;
            int width = (int)((image.Width + image.Height * Math.Tan(Math.PI * skewX/ 180)) * scaleX);
            int height = (int)((image.Height + image.Width * Math.Tan(Math.PI * skewY/ 180)) * scaleY);
            PdfTemplate template = new PdfTemplate(width, height);
            template.Graphics.ScaleTransform(scaleX, scaleY);
            template.Graphics.SkewTransform(skewX, skewY);
            template.Graphics.DrawImage(image, 0, 0);

            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();
            page.Canvas.TranslateTransform(page.Canvas.ClientSize.Width - 50, 260);
            float offset = (page.Canvas.ClientSize.Width - 100) / 12;
            for (int i = 0; i < 12; i++)
            {
                page.Canvas.TranslateTransform(-offset, 0);
                page.Canvas.SetTransparency(i / 12.0f);
                page.Canvas.DrawTemplate(template, new PointF(0, 0));
            }

            //restor graphics
            page.Canvas.Restore(state);
        }
Ejemplo n.º 2
0
 public void MaakHetPdfDocument(PdfPageBase page, string inhoud, int linksrechtspositie)
 {
     //Draw the text
     page.Canvas.DrawString(inhoud,
         new PdfFont(PdfFontFamily.Helvetica, 10f),
         new PdfSolidBrush(Color.Black), linksrechtspositie
         , 10);
 }
Ejemplo n.º 3
0
        private void DrawImage(PdfPageBase page)
        {
            PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\SalesReportChart.png");
            float width = image.Width * 0.75f;
            float height = image.Height * 0.75f;
            float x = (page.Canvas.ClientSize.Width - width) / 2;

            page.Canvas.DrawImage(image, x, 60, width, height);
        }
        private void DrawPath(PdfPageBase page)
        {
            PointF[] points = new PointF[5];
            for (int i = 0; i < points.Length; i++)
            {
                float x = (float)Math.Cos(i * 2 * Math.PI / 5);
                float y = (float)Math.Sin(i * 2 * Math.PI / 5);
                points[i] = new PointF(x, y);
            }
            PdfPath path = new PdfPath();
            path.AddLine(points[2], points[0]);
            path.AddLine(points[0], points[3]);
            path.AddLine(points[3], points[1]);
            path.AddLine(points[1], points[4]);
            path.AddLine(points[4], points[2]);

            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();
            PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);
            PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue);

            page.Canvas.ScaleTransform(50f, 50f);
            page.Canvas.TranslateTransform(5f, 1.2f);
            page.Canvas.DrawPath(pen, path);

            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Alternate;
            page.Canvas.DrawPath(pen, brush1, path);

            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush1, path);

            PdfLinearGradientBrush brush2 = new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.Red, Color.Blue);
            page.Canvas.TranslateTransform(-4f, 2f);
            path.FillMode = PdfFillMode.Alternate;
            page.Canvas.DrawPath(pen, brush2, path);

            PdfRadialGradientBrush brush3 = new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Red, Color.Blue);
            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush3, path);

            PdfTilingBrush brush4 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
            brush4.Graphics.DrawRectangle(brush2, 0, 0, 4f, 4f);

            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush4, path);

            //restor graphics
            page.Canvas.Restore(state);
        }
Ejemplo n.º 5
0
        protected PdfLayoutResult buildPdfLines(PdfPageBase page, List<LineItem> list, string category, float y)
        {
            PdfFont helv14 = new PdfFont(PdfFontFamily.Helvetica, 14f);
            PdfFont helv12 = new PdfFont(PdfFontFamily.Helvetica, 12f);
            PdfFont helv11 = new PdfFont(PdfFontFamily.Helvetica, 11f);

            PdfTable LinesTable = new PdfTable();
            LinesTable.Style.CellPadding = 1;
            LinesTable.Style.DefaultStyle.Font = helv11;

            List<string> data = new List<string>();
            double subtotal = 0;

            if(category == "Recurring")
                data.Add("Product;Part Number;Monthly Rate;Quantity;Price");
            else
                data.Add("Product;Part Number;Unit Price;Quantity;Price");
            foreach (LineItem line in list)
            {
                data.Add(line.Product.Name + ";" + line.Product.PartNumber + ";$" + line.Product.Price + ";" + line.Quantity + ";$" + line.Total);
                subtotal += line.Total;
            } data.Add(";;; Subtotal: ;$" + subtotal.ToString());

            string[][] dataSource = new string[data.Count][];
            for (int i = 0; i < data.Count; i++)
                dataSource[i] = data[i].Split(';');

            LinesTable.DataSource = dataSource;

            LinesTable.BeginRowLayout += new BeginRowLayoutEventHandler(LinesTable_BeginRowLayout);

            LinesTable.Columns[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
            LinesTable.Columns[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
            LinesTable.Columns[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
            LinesTable.Columns[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Right);

            float width = page.Canvas.ClientSize.Width;
            for (int i = 0; i < LinesTable.Columns.Count; i++)
            {
                if (i == 0)
                    LinesTable.Columns[i].Width = width * .1f * width;
                else
                    LinesTable.Columns[i].Width = width * .045f * width;
            }

            return LinesTable.Draw(page, new PointF(0, y));
        }
Ejemplo n.º 6
0
        private void DrawPage(PdfPageBase page)
        {
            float y = 10;

            //title
            PdfBrush brush1 = PdfBrushes.Black;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
            page.Canvas.DrawString("Category Sales by Year", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
            y = y + font1.MeasureString("Category Sales by Year", format1).Height;
            y = y + 5;

            String[][] data
                = {
                    new String[]{"Category Name", "1994 Sale Amount", "1995 Sale Amount", "1996 Sale Amount"},
                    new String[]{"Beverages", "38,487.20", "102,479.46", "126,901.53"},
                    new String[]{"Condiments", "16,402.95", "51,041.83", "38,602.31"},
                    new String[]{"Confections", "23,812.90", "79,752.25", "63,792.07"},
                    new String[]{"Dairy Products", "30,027.79", "116,495.45", "87,984.05"},
                    new String[]{"Grains/Cereals", "7,313.92", "53,823.48", "34,607.19"},
                    new String[]{"Meat/Poultry", "19,856.86", "77,164.75", "66,000.75"},
                    new String[]{"Produce", "10,694.96", "45,973.69", "43,315.93"},
                    new String[]{"Seafood", "16,247.77", "64,195.51", "50,818.46"}
                };

            PdfTable table = new PdfTable();
            table.Style.CellPadding = 2;
            table.Style.BorderPen = new PdfPen(brush1, 0.75f);
            table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
            table.Style.HeaderSource = PdfHeaderSource.Rows;
            table.Style.HeaderRowCount = 1;
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            table.Style.ShowHeader = true;
            table.DataSource = data;

            PdfLayoutResult result = table.Draw(page, new PointF(0, y));
            y = y + result.Bounds.Height + 5;

            PdfBrush brush2 = PdfBrushes.LightGray;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));
            page.Canvas.DrawString("* All data from NorthWind", font2, brush2, 5, y);
        }
Ejemplo n.º 7
0
        private void AlignText(PdfPageBase page)
        {
            //Draw the text - alignment
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20f);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);

            PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            page.Canvas.DrawString("Left!", font, brush, 0, 20, leftAlignment);
            page.Canvas.DrawString("Left!", font, brush, 0, 50, leftAlignment);

            PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            page.Canvas.DrawString("Right!", font, brush, page.Canvas.ClientSize.Width, 30, rightAlignment);
            page.Canvas.DrawString("Right!", font, brush, page.Canvas.ClientSize.Width, 60, rightAlignment);

            PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, page.Canvas.ClientSize.Width / 2, 40, centerAlignment);

        }
Ejemplo n.º 8
0
        private void TransformText(PdfPageBase page)
        {
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw the text - transform           
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f);
            PdfSolidBrush brush1 = new PdfSolidBrush(Color.Blue);
            PdfSolidBrush brush2 = new PdfSolidBrush(Color.CadetBlue);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);

            page.Canvas.TranslateTransform(page.Canvas.ClientSize.Width / 2, 20);
            page.Canvas.DrawString("Sales Report Chart", font, brush1, 0, 0, format);

            page.Canvas.ScaleTransform(1f, -0.8f);
            page.Canvas.DrawString("Sales Report Chart", font, brush2, 0, -2 * 18 * 1.2f, format);
            //restor graphics
            page.Canvas.Restore(state);
        }
Ejemplo n.º 9
0
        //文档地址 https://www.e-iceblue.cn/spirepdfnet/spire-pdf-for-net-program-guide-content.html


        /// <summary>
        /// Spire插件添加二维码到PDF
        /// </summary>
        /// <param name="sourcePdf">pdf文件路径</param>
        /// <param name="sourceImg">二维码图片路径</param>
        public static void AddQrCodeToPdf(string sourcePdf, string sourceImg)
        {
            //初始化PdfDocument实例,导入PDF文件
            Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();

            //加载现有文档
            doc.LoadFromFile(sourcePdf);
            //添加一个空白页,目的为了删除jar包添加的水印,后面再移除这一页  (没有用)
            //PdfPageBase pb = doc.Pages.Add();
            //获取第二页
            //PdfPageBase page = doc.Pages[1];
            //获取第1页
            PdfPageBase page = doc.Pages[0];
            //加载图片到Image对象
            Image image = Image.FromFile(sourceImg);

            //调整图片大小
            int    width       = image.Width;
            int    height      = image.Height;
            float  scale       = 0.18f; //缩放比例0.18f;
            Size   size        = new Size((int)(width * scale), (int)(height * scale));
            Bitmap scaledImage = new Bitmap(image, size);

            //加载缩放后的图片到PdfImage对象
            Spire.Pdf.Graphics.PdfImage pdfImage = Spire.Pdf.Graphics.PdfImage.FromImage(scaledImage);

            //设置图片位置
            float x = 516f;
            float y = 8f;

            //在指定位置绘入图片
            //page.Canvas.DrawImage(pdfImage, x, y);
            page.Canvas.DrawImage(pdfImage, new PointF(x, y), size);
            //移除第一个页
            //doc.Pages.Remove(pb); //去除第一页水印
            //保存文档
            doc.SaveToFile(@sourcePdf);
            doc.Close();
            //释放图片资源
            image.Dispose();
        }
Ejemplo n.º 10
0
        private void button4_Click(object sender, EventArgs e)
        {
            PdfDocument doc = new PdfDocument();

            // Create one page
            PdfPageBase page = doc.Pages.Add();

            PdfImage pdfImage = PdfImage.FromFile(@"D:\2.jpg");

            //Draw the text
            page.Canvas.DrawImage(pdfImage, new PointF(0, 0));
            //Save pdf file.
            doc.SaveToFile("FromIMG.pdf", FileFormat.PDF);
            var img = doc.SaveAsImage(0, PdfImageType.Bitmap);

            img.Save("1.jpg");
            doc.Close();


            System.Diagnostics.Process.Start("FromIMG.pdf");
        }
Ejemplo n.º 11
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Create one page
            PdfPageBase page = doc.Pages.Add();

            DrawPath(page);
            DrawSpiro(page);
            DrawRectangle(page);
            DrawPie(page);
            DrawEllipse(page);

            //Save the document
            doc.SaveToFile("DrawShape.pdf");
            doc.Close();

            //Launch the Pdf file
            PDFDocumentViewer("DrawShape.pdf");
        }
Ejemplo n.º 12
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            // Create one page
            PdfPageBase page = doc.Pages.Add();

            //Draw the text
            page.Canvas.DrawString("Hello, World!",
                                   new PdfFont(PdfFontFamily.Helvetica, 30f),
                                   new PdfSolidBrush(Color.Black),
                                   10, 10);

            //Save pdf file.
            doc.SaveToFile("HelloWorld.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("HelloWorld.pdf");
        }
Ejemplo n.º 13
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            // Create one page
            PdfPageBase page = doc.Pages.Add();

            DrawText(page);
            AlignText(page);
            AlignTextInRectangle(page);
            TransformText(page);
            RotateText(page);

            //Save doc file.
            doc.SaveToFile("DrawText.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("DrawText.pdf");
        }
Ejemplo n.º 14
0
        private void DrawText(PdfPageBase page)
        {
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw text - brush
            String          text   = "Go! Turn Around! Go! Go! Go!";
            PdfPen          pen    = PdfPens.DeepSkyBlue;
            PdfSolidBrush   brush  = new PdfSolidBrush(Color.White);
            PdfStringFormat format = new PdfStringFormat();
            PdfFont         font   = new PdfFont(PdfFontFamily.Helvetica, 18f, PdfFontStyle.Italic);
            SizeF           size   = font.MeasureString(text, format);
            RectangleF      rctg
                = new RectangleF(page.Canvas.ClientSize.Width / 2 + 10, 180,
                                 size.Width / 3 * 2, size.Height * 2);

            page.Canvas.DrawString(text, font, pen, brush, rctg, format);

            //restor graphics
            page.Canvas.Restore(state);
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            Program mypro = new Program();

            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            // Create one page
            PdfPageBase page = doc.Pages.Add();

            //mypro.DrawSpiro(page);
            //mypro. DrawPath(page);
            mypro.DrawStrightLine(page);

            //Save pdf file.
            doc.SaveToFile("DrawShape11111.pdf");
            doc.Close();

            //Launching the Pdf file.
            mypro.PDFDocumentViewer("DrawShape11111.pdf");
        }
Ejemplo n.º 16
0
        public static void GetElements(string fileName)
        {
            try
            {
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(fileName);
                PdfPageBase page = doc.Pages[0];

                SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                string       text = page.ExtractText(strategy);
                FileStream   fs   = new FileStream(Path.GetDirectoryName(fileName) + "\\result_spire.txt", FileMode.Create);
                StreamWriter sw   = new StreamWriter(fs);
                sw.Write(text);
                sw.Flush();
                sw.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            demo1 mypro = new demo1();

            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            // Create one page
            PdfPageBase page1 = doc.Pages.Add();
            PdfPageBase page2 = doc.Pages.Add();

            mypro.DrawStrightLine(page1);
            mypro.DrawRectangle(page2);

            //Save pdf file.
            doc.SaveToFile("DrawShape233.pdf");
            doc.Close();

            //Launching the Pdf file.
            mypro.PDFDocumentViewer("DrawShape233.pdf");
        }
Ejemplo n.º 18
0
        public bool PrintPdf(PdfSection sec, string name, string[] dataset)
        {
            float y     = 10;
            bool  check = true;

            if (dataset.Length != 1)
            {
                page = sec.Pages.Add();
                try
                {
                    page.Canvas.DrawString(name, font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
                    y += font1.MeasureString("Country List", format1).Height;
                    y += 5;
                    String[][] dataSource = new String[dataset.Length][];
                    for (int i = 0; i < dataset.Length; i++)
                    {
                        dataSource[i] = dataset[i].Split(';');
                    }
                    PdfTable table = new PdfTable();
                    table.Style.CellPadding = 2;
                    table.Style.BorderPen   = new PdfPen(brush1, 0.75f);
                    table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
                    table.Style.HeaderSource             = PdfHeaderSource.Rows;
                    table.Style.HeaderRowCount           = 1;
                    table.Style.ShowHeader = true;
                    table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
                    table.DataSource = dataSource;
                    foreach (PdfColumn column in table.Columns)
                    {
                        column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
                    }
                    table.Draw(page, new PointF(0, y));
                }
                catch
                {
                    check = false;
                }
            }
            return(check);
        }
Ejemplo n.º 19
0
        private void DrawAutomaticField(PdfPageBase page)
        {
            float y = 0;

            //title
            PdfBrush brush1 = PdfBrushes.CadetBlue;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
            page.Canvas.DrawString("Automatic Field List", font1, brush1,
                page.Canvas.ClientSize.Width / 2, y, format1);
            y = y + font1.MeasureString("Automatic Field List", format1).Height;
            y = y + 5;

            String[] fieldList = new String[]
            {
                "DateTimeField",
                "CreationDateField",
                "DocumentAuthorField",
                "SectionNumberField",
                "SectionPageNumberField",
                "SectionPageCountField",
                "PageNumberField",
                "PageCountField",
                "DestinationPageNumberField",
                "CompositeField"
            };
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f));
            PdfStringFormat fieldNameFormat = new PdfStringFormat();
            fieldNameFormat.MeasureTrailingSpaces = true;
            foreach (String fieldName in fieldList)
            {
                //draw field name
                String text = String.Format("{0}: ", fieldName);
                page.Canvas.DrawString(text, font, PdfBrushes.DodgerBlue, 0, y);
                float x = font.MeasureString(text, fieldNameFormat).Width;
                RectangleF bounds = new RectangleF(x, y, 200, font.Height);
                DrawAutomaticField(fieldName, page, bounds);
                y = y + font.Height + 3;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            PdfDocument doc = new PdfDocument();

            // Creates a page
            PdfPageBase page = doc.Pages.Add();

            //Create text
            String text = "Welcome to evaluate Spire.PDF for .NET !";

            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);

            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);

            // Defines a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Calibri", 15f, FontStyle.Regular));

            float x = 50;
            float y = 50;

            // Draw text layer
            page.Canvas.DrawString(text, font, brush, new PointF(x, y), format);

            SizeF size = font.MeasureString("Welcome to  evaluate", format);

            SizeF size2 = font.MeasureString("Spire.PDF for .NET", format);

            // Loads an image
            PdfImage image = PdfImage.FromFile("..\\..\\..\\..\\..\\..\\Data\\MultilayerImage.png");

            // Draw image layer
            page.Canvas.DrawImage(image, new PointF(x + size.Width, y), size2);

            String result = "CreateMultilayerPDF_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
Ejemplo n.º 21
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a new Pdf document.
            PdfDocument doc = new PdfDocument();

            //Add a new page to it.
            PdfPageBase page = doc.Pages.Add();

            //Draw a rectangle on the page to define the canvas area for the 3D file.
            Rectangle rt = new Rectangle(0, 80, 200, 200);

            //Initialize a new object of Pdf3DAnnotation, load the .u3d file as 3D annotation.
            Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, @"..\..\..\..\..\..\Data\CreatePdf3DAnnotation.u3d");

            annotation.Activation = new Pdf3DActivation();
            annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen;

            //Define a 3D view mode.
            Pdf3DView View = new Pdf3DView();

            View.Background           = new Pdf3DBackground(new PdfRGBColor(Color.Purple));
            View.ViewNodeName         = "3DAnnotation";
            View.RenderMode           = new Pdf3DRendermode(Pdf3DRenderStyle.Solid);
            View.InternalName         = "3DAnnotation";
            View.LightingScheme       = new Pdf3DLighting();
            View.LightingScheme.Style = Pdf3DLightingStyle.Day;

            //Set the 3D view mode for the annotation.
            annotation.Views.Add(View);

            //Add the annotation to Pdf.
            page.AnnotationsWidget.Add(annotation);

            String result = "CreatePdf3DAnnotation_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
Ejemplo n.º 22
0
        private void button1_Click(object sender, EventArgs e)
        {
            // Creates a pdf document
            PdfDocument doc = new PdfDocument();

            // Creates a new page
            PdfPageBase page = doc.Pages.Add();

            string s1 = "Spire.PDF for .NET is a professional PDF component applied to creating, writing, "
                        + "editing, handling and reading PDF files without any external dependencies within "
                        + ".NET application. Using this .NET PDF library, you can implement rich capabilities "
                        + "to create PDF files from scratch or process existing PDF documents entirely through "
                        + "C#/VB.NET without installing Adobe Acrobat.";
            string s2 = "Many rich features can be supported by the .NET PDF API, such as security setting "
                        + "(including digital signature), PDF text/attachment/image extract, PDF merge/split "
                        + ", metadata update, section, graph/image drawing and inserting, table creation and "
                        + "processing, and importing data etc.Besides, Spire.PDF for .NET can be applied to easily "
                        + "converting Text, Image and HTML to PDF with C#/VB.NET in high quality.";

            // Get width and height of page
            float pageWidth  = page.GetClientSize().Width;
            float pageHeight = page.GetClientSize().Height;

            PdfBrush        brush  = PdfBrushes.Black;
            PdfFont         font   = new PdfFont(PdfFontFamily.TimesRoman, 12f);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify);

            // Draw text
            page.Canvas.DrawString(s1, font, brush, new RectangleF(0, 20, pageWidth / 2 - 8f, pageHeight), format);
            page.Canvas.DrawString(s2, font, brush, new RectangleF(pageWidth / 2 + 8f, 20, pageWidth / 2 - 8f, pageHeight), format);


            String result = "CreateTwoColumnPDF_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
Ejemplo n.º 23
0
        private void button1_Click(object sender, EventArgs e)
        {
            //pdf file
            string input = "..\\..\\..\\..\\..\\..\\Data\\Sample5.pdf";

            string output = "ResetPageSize.pdf";

            //open pdf document
            PdfDocument originalDoc = new PdfDocument(input);

            //set margins
            PdfMargins margins = new PdfMargins(0);

            //create a new pdf document
            using (PdfDocument newDoc = new PdfDocument())
            {
                float scale = 0.8f;
                for (int i = 0; i < originalDoc.Pages.Count; i++)
                {
                    PdfPageBase page = originalDoc.Pages[i];

                    //use same scale to set width and height
                    float width  = page.Size.Width * scale;
                    float height = page.Size.Height * scale;

                    //add new page with expected width and height
                    PdfPageBase newPage = newDoc.Pages.Add(new SizeF(width, height), margins);
                    newPage.Canvas.ScaleTransform(scale, scale);

                    //copy content of original page into new page
                    newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF.Empty);
                }
                //save pdf document
                newDoc.SaveToFile(output);
            }

            //Launch the Pdf file.
            PDFDocumentViewer(output);
        }
Ejemplo n.º 24
0
        private void AlignTextInRectangle(PdfPageBase page)
        {
            //Draw the text - align in rectangle
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
            RectangleF rctg1 = new RectangleF(0, 70, page.Canvas.ClientSize.Width / 2, 100);
            RectangleF rctg2 = new RectangleF(page.Canvas.ClientSize.Width / 2, 70, page.Canvas.ClientSize.Width / 2, 100);
            page.Canvas.DrawRectangle(new PdfSolidBrush(Color.LightBlue), rctg1);
            page.Canvas.DrawRectangle(new PdfSolidBrush(Color.LightSkyBlue), rctg2);

            PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top);
            page.Canvas.DrawString("Left! Left!", font, brush, rctg1, leftAlignment);
            page.Canvas.DrawString("Left! Left!", font, brush, rctg2, leftAlignment);

            PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            page.Canvas.DrawString("Right! Right!", font, brush, rctg1, rightAlignment);
            page.Canvas.DrawString("Right! Right!", font, brush, rctg2, rightAlignment);

            PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Bottom);
            page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, rctg1, centerAlignment);
            page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, rctg2, centerAlignment);
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Spire插件读取PDF中的二维码,Zxing识别二维码
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string QrCodeToPdftwo(string file)
        {
            string str = "";
            //加载PDF文档
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(file);

            List <Image> listImages = new List <Image>();

            for (int i = 0; i < doc.Pages.Count; i++)
            {
                // 实例化一个Spire.Pdf.PdfPageBase对象
                PdfPageBase page = doc.Pages[i];

                // 获取所有pages里面的图片
                Image[] images = page.ExtractImages();
                if (images != null && images.Length > 0)
                {
                    listImages.AddRange(images);
                }
            }

            if (listImages.Count > 0)
            {
                //QRCodeDecoder decoder = new QRCodeDecoder();
                //var image = listImages[0];
                //str = decoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage((Bitmap)image));

                var image = listImages[0];
                ZXing.BarcodeReader reader = new ZXing.BarcodeReader();
                reader.Options.CharacterSet = "UTF-8";
                Bitmap map    = new Bitmap(image);
                Result result = reader.Decode(map);
                return(result == null ? "" : result.Text);
            }

            return(str);
        }
        /// <summary>
        /// 将Pdf文档转换为xps文档
        /// </summary>
        /// <param name="wordDocName">word文档全路径</param>
        /// <param name="xpsDocName">xps文档全路径</param>
        /// <returns></returns>
        public static Boolean ConvertPdfToXPS(string pdfDocName, string xpsDocName)
        {
            Boolean b = false;

            try
            {
                //创建一个PdfDocument类实例,并加载文档
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(pdfDocName);
                PdfPageBase firstPage = doc.Pages.Add();
                doc.Pages.Remove(firstPage);
                //保存文件为XPS
                doc.SaveToFile(xpsDocName, FileFormat.XPS);
                b = true;
            }
            catch (Exception e1)
            {
                b = false;
                //  System.Windows.MessageBox.Show("pdf转换失败:" + e1.Message);
            }
            return(b);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Load Pdf document from disk
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile("../../../../../../Data/PDFTemplate_N.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Create a new Pdf
            PdfDocument newPdf = new PdfDocument();

            //Remove all the margins
            newPdf.PageSettings.Margins.All = 0;

            //Set the page size of new Pdf
            newPdf.PageSettings.Width  = page.Size.Width;
            newPdf.PageSettings.Height = page.Size.Height / 2;

            //Add a new page
            PdfPageBase newPage = newPdf.Pages.Add();

            PdfTextLayout format = new PdfTextLayout();

            format.Break  = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;

            //Draw the page in the new page
            page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);

            //Save the Pdf document
            string output = "SplitAPageIntoMultipage_out.pdf";

            newPdf.SaveToFile(output);

            //Launch the document
            PDFDocumentViewer(output);
        }
Ejemplo n.º 28
0
        /// <summary>
        /// https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html
        /// </summary>
        public void pdf()
        {
            PdfDocument      doc             = new PdfDocument();
            PdfPageBase      page            = doc.Pages.Add();
            PdfGraphicsState state           = page.Canvas.Save();
            PdfFont          font            = new PdfFont(PdfFontFamily.Helvetica, 10f);
            PdfSolidBrush    brush           = new PdfSolidBrush(Color.Blue);
            PdfStringFormat  centerAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

            float x = page.Canvas.ClientSize.Width / 2;
            float y = 380;

            page.Canvas.TranslateTransform(x, y);
            for (int i = 0; i < 12; i++)
            {
                page.Canvas.RotateTransform(30);
                page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, 20, 0, centerAlignment);
            }
            page.Canvas.Restore(state);
            doc.SaveToFile("DrawText.pdf");
            doc.Close();
        }
Ejemplo n.º 29
0
        private PdfLayoutResult DrawPDFTable(string title, float y, PdfPageBase page, string dataName)
        {
            //Draw Title
            PdfBrush        brush  = PdfBrushes.Black;
            PdfTrueTypeFont font   = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
            string          title1 = title;

            page.Canvas.DrawString(title1, font, brush, page.Canvas.ClientSize.Width / 2, y, format);
            y = y + font.MeasureString(title1, format).Height;
            y = y + 10;

            //Create PDF table and define table style
            PdfTable table = new PdfTable();

            table.Style.CellPadding = 3;
            table.Style.BorderPen   = new PdfPen(brush, 0.75f);
            table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
            table.Style.DefaultStyle.Font            = new PdfTrueTypeFont(new Font("Arial", 10f));
            table.Style.DefaultStyle.StringFormat    = format;
            table.Style.AlternateStyle = new PdfCellStyle();
            table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow;
            table.Style.AlternateStyle.Font            = new PdfTrueTypeFont(new Font("Arial", 10f));
            table.Style.AlternateStyle.StringFormat    = format;
            table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
            table.Style.HeaderStyle.Font            = new PdfTrueTypeFont(new Font("Arial", 14f, FontStyle.Bold));
            table.Style.HeaderStyle.StringFormat    = format;
            table.Style.ShowHeader = true;

            //Fill data in table
            table.DataSource = GetData(dataName);

            //Draw the table on Pdf page
            PdfLayoutResult result = table.Draw(page, new PointF(0, y));

            return(result);
        }
        private static void AddFileLinkAnnotation(PdfPageBase page, float y)
        {
            //Define a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f));

            //Set the string format
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);

            //Text string
            String prompt = "Launch a File: ";

            //Draw text string on page canvas
            page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y);

            //Use MeasureString to get the width of string
            float x = font.MeasureString(prompt, format).Width;

            //String of file name
            String label = "Sample.pdf";

            //Use MeasureString to get the SizeF of string
            SizeF size = font.MeasureString(label);

            //Create a rectangle
            RectangleF bounds = new RectangleF(x, y, size.Width, size.Height);

            //Draw label string
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y);

            //Create PdfFileLinkAnnotation on the rectangle and link file "Sample.pdf"
            PdfFileLinkAnnotation annotation = new PdfFileLinkAnnotation(bounds, @"..\..\..\..\..\..\Data\Sample.pdf");

            //Set color for annotation
            annotation.Color = Color.Blue;

            //Add annotation to the page
            (page as PdfNewPage).Annotations.Add(annotation);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Load a Pdf document from disk
            PdfDocument document = new PdfDocument();

            document.LoadFromFile("../../../../../../Data/PDFTemplate_N.pdf");

            //Get the first page
            PdfPageBase page = document.Pages[0];

            //Set the font and brush
            PdfTrueTypeFont font  = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular), true);
            PdfSolidBrush   brush = new PdfSolidBrush(Color.Blue);

            //Time text
            String timeString = DateTime.Now.ToString("MM/dd/yy hh:mm:ss tt ");

            //Create a template and rectangle, draw the string
            PdfTemplate template = new PdfTemplate(140, 15);
            RectangleF  rect     = new RectangleF(new PointF(page.ActualSize.Width - template.Width - 10, page.ActualSize.Height - template.Height - 10), template.Size);

            template.Graphics.DrawString(timeString, font, brush, new PointF(0, 0));

            //Create stamp annoation
            PdfRubberStampAnnotation stamp       = new PdfRubberStampAnnotation(rect);
            PdfAppearance            apprearance = new PdfAppearance(stamp);

            apprearance.Normal = template;
            stamp.Appearance   = apprearance;
            page.AnnotationsWidget.Add(stamp);

            //Sabe the document
            string output = "AddDateTimeStamp_result.pdf";

            document.SaveToFile(output, FileFormat.PDF);

            PDFDocumentViewer(output);
        }
Ejemplo n.º 32
0
        private void button1_Click(object sender, EventArgs e)
        {
            PdfDocument pdf = new PdfDocument();

            pdf.LoadFromFile(@"..\..\..\..\..\..\Data\DrawingTemplate.pdf");
            //Create one page
            PdfPageBase page = pdf.Pages[0];

            //Save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw rectangles
            int          x      = 200;
            int          y      = 300;
            int          width  = 200;
            int          height = 100;
            PdfPen       pen    = new PdfPen(Color.Black, 1f);
            PdfBrush     brush  = new PdfSolidBrush(Color.Red);
            PdfBlendMode mode   = new PdfBlendMode();

            page.Canvas.SetTransparency(0.5f, 0.5f, mode);
            page.Canvas.DrawRectangle(pen, brush, new Rectangle(new Point(x, y), new Size(width, height)));

            x = x + width / 2;
            y = y - height / 2;
            page.Canvas.SetTransparency(0.2f, 0.2f, mode);
            page.Canvas.DrawRectangle(pen, brush, new Rectangle(new Point(x, y), new Size(width, height)));

            //Restor graphics
            page.Canvas.Restore(state);

            String result = "SetRectangleTransparency_out.pdf";

            //Save the document
            pdf.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
Ejemplo n.º 33
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document and load file from disk
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(@"../../../../../../Data/PDFTemplate_N.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Define Pdf pen
            PdfPen pen = new PdfPen(Color.Gray);

            //Save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Rotate page canvas
            page.Canvas.RotateTransform(-20);

            PdfStringFormat format = new PdfStringFormat();

            format.CharacterSpacing = 5f;

            //Draw the string on page
            page.Canvas.DrawString("E-ICEBLUE", new PdfFont(PdfFontFamily.Helvetica, 45f), pen, 0, 500f, format);

            //Restore graphics
            page.Canvas.Restore(state);

            //Save the Pdf file
            string output = "FillStrokeText_out.pdf";

            doc.SaveToFile(output);
            doc.Close();

            //Launch the Pdf file
            PDFDocumentViewer(output);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Load file from disk
            doc.LoadFromFile(@"..\..\..\..\..\..\Data\LinkAnnotation.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Get the annotation collection
            PdfAnnotationCollection annotations = page.AnnotationsWidget;

            //Verify whether widgetCollection is not null or not
            if (annotations.Count > 0)
            {
                //traverse the PdfAnnotationCollection
                foreach (PdfAnnotation pdfAnnotation in annotations)
                {
                    //if it is PdfTextWebLinkAnnotationWidget
                    if (pdfAnnotation is PdfTextWebLinkAnnotationWidget)
                    {
                        //Get the link annotation
                        PdfTextWebLinkAnnotationWidget annotation = pdfAnnotation as PdfTextWebLinkAnnotationWidget;

                        //Change the url
                        annotation.Url = "http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html";
                    }
                }
            }
            String result = "ExtractAndUpdateLink_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Pdf file
            String input = @"..\..\..\..\..\..\Data\SampleB_2.pdf";

            //Open pdf document
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(input);
            PdfNewDocument newDoc = new PdfNewDocument();

            //Set Pdf_A1B
            newDoc.Conformance = PdfConformanceLevel.Pdf_A1B;
            foreach (PdfPageBase page in doc.Pages)
            {
                SizeF       size = page.Size;
                PdfPageBase p    = newDoc.Pages.Add(size, new Spire.Pdf.Graphics.PdfMargins(0));
                page.CreateTemplate().Draw(p, 0, 0);
            }

            //Load files and add in attachments
            byte[]        data    = File.ReadAllBytes(@"..\..\..\..\..\..\Data\SampleB_1.png");
            PdfAttachment attach1 = new PdfAttachment("attachment1.png", data);

            byte[]        data2   = File.ReadAllBytes(@"..\..\..\..\..\..\Data\SampleB_1.pdf");
            PdfAttachment attach2 = new PdfAttachment("attachment2.pdf", data2);

            newDoc.Attachments.Add(attach1);
            newDoc.Attachments.Add(attach2);

            string output = "ToPDFAWithAttachments-result.pdf";

            newDoc.Save(output);
            newDoc.Close();

            //Launch the reuslt file
            PDFDocumentViewer(output);
        }
Ejemplo n.º 36
0
        private void button1_Click(object sender, EventArgs e)
        {
            PdfDocument pdf = new PdfDocument();

            pdf.LoadFromFile(@"..\..\..\..\..\..\Data\DrawingTemplate.pdf");
            //Create one page
            PdfPageBase page = pdf.Pages[0];

            //Save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw line
            //Set location and size
            float x      = 95;
            float y      = 95;
            float width  = 400;
            float height = 500;

            //Create pens
            PdfPen pen  = new PdfPen(Color.Black, 0.1f);
            PdfPen pen1 = new PdfPen(Color.Red, 0.1f);

            //Draw a rectangle
            page.Canvas.DrawRectangle(pen, x, y, width, height);
            //Draw two crossed lines
            page.Canvas.DrawLine(pen1, x, y, x + width, y + height);
            page.Canvas.DrawLine(pen1, x + width, y, x, y + height);

            //Restore graphics
            page.Canvas.Restore(state);

            String result = "DrawLine_out.pdf";

            //Save the document
            pdf.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
Ejemplo n.º 37
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            doc.DocumentInformation.Author = "Spire.Pdf";

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            for (int i = 1; i < 4; i++)
            {
                //create section
                PdfSection section = doc.Sections.Add();
                section.PageSettings.Size    = PdfPageSize.A4;
                section.PageSettings.Margins = margin;

                for (int j = 0; j < i; j++)
                {
                    // Create one page
                    PdfPageBase page = section.Pages.Add();
                    DrawAutomaticField(page);
                }
            }

            //Save pdf file.
            doc.SaveToFile("AutomaticField.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("AutomaticField.pdf");
        }
        public ActionResult OverlayDocuments(string InsideBrowser)
        {
            string dataPath1, dataPath2;

            dataPath1 = ResolveApplicationDataPath("BorderTemplate.pdf");
            dataPath2 = ResolveApplicationDataPath("SourceTemplate.pdf");

            Stream            stream1 = new FileStream(dataPath2, FileMode.Open, FileAccess.Read);
            FileStream        file    = new FileStream(dataPath1, FileMode.Open, FileAccess.Read, FileShare.Read);
            PdfLoadedDocument ldDoc1  = new PdfLoadedDocument(file);
            PdfLoadedDocument ldDoc2  = new PdfLoadedDocument(stream1);
            PdfDocument       doc     = new PdfDocument();

            for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i)
            {
                PdfPage     page = doc.Pages.Add();
                PdfGraphics g    = page.Graphics;

                PdfPageBase lpage    = ldDoc2.Pages[i];
                PdfTemplate template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());

                lpage    = ldDoc1.Pages[0];
                template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
            }

            if (InsideBrowser == "Browser")
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
            }
            else
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Add a section
            PdfSection section = doc.Sections.Add();

            //Load a image
            PdfImage image = PdfImage.FromFile(@"../../../../../../Data/scenery.jpg");

            //Check whether the width of the image file is greater than default page width or not
            if (image.PhysicalDimension.Width > section.PageSettings.Size.Width)
            {
                //Set the orientation as landscape
                section.PageSettings.Orientation = PdfPageOrientation.Landscape;
            }

            else
            {
                section.PageSettings.Orientation = PdfPageOrientation.Portrait;
            }

            //Add a new page with orientation Landscape
            PdfPageBase page = section.Pages.Add();

            //Draw the image on the page
            page.Canvas.DrawImage(image, new PointF(0, 0));

            string output = "SetPageOrientation-result.pdf";

            //Save to file
            doc.SaveToFile(output);


            //Launch the reuslt file
            PDFDocumentViewer(output);
        }
Ejemplo n.º 40
0
        private void button1_Click(object sender, EventArgs e)
        {
            //pdf file
            string input = "..\\..\\..\\..\\..\\..\\..\\Data\\Sample5.pdf";

            //open a pdf document
            PdfDocument document = new PdfDocument(input);

            //get the first page
            PdfPageBase page = document.Pages[0];

            //create a rubber stamp annotation
            PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(0, 0), new SizeF(60, 60)));

            //create an instance of PdfAppearance
            PdfAppearance loApprearance = new PdfAppearance(loStamp);

            PdfImage image = PdfImage.FromFile("..\\..\\..\\..\\..\\..\\..\\Data\\image stamp.jpg");

            PdfTemplate template = new PdfTemplate(160, 160);

            //draw a pdf image into pdf template
            template.Graphics.DrawImage(image, 0, 0);

            loApprearance.Normal = template;
            loStamp.Appearance   = loApprearance;

            //add the rubber stamp annotation into pdf
            page.AnnotationsWidget.Add(loStamp);

            string output = "AddImageStamp.pdf";

            //save pdf document
            document.SaveToFile(output);

            //Launching the Pdf file
            PDFDocumentViewer(output);
        }
Ejemplo n.º 41
0
        private void RotateText(PdfPageBase page)
        {
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw the text - transform           
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);

            PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            float x = page.Canvas.ClientSize.Width / 2;
            float y = 380;

            page.Canvas.TranslateTransform(x, y);
            for (int i = 0; i < 12; i++)
            {
                page.Canvas.RotateTransform(30);
                page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, 20, 0, centerAlignment);
            }

            //restor graphics
            page.Canvas.Restore(state);
        }
Ejemplo n.º 42
0
        private void DrawPage(PdfPageBase page)
        {
            float pageWidth = page.Canvas.ClientSize.Width;
            float y = 0;

            //page header
            PdfPen pen1 = new PdfPen(Color.LightGray, 1f);
            PdfBrush brush1 = new PdfSolidBrush(Color.LightGray);
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic));
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right);
            String text = "Demo of Spire.Pdf";
            page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1);
            SizeF size = font1.MeasureString(text, format1);
            y = y + size.Height + 1;
            page.Canvas.DrawLine(pen1, 0, y, pageWidth, y);

            //title
            y = y + 5;
            PdfBrush brush2 = new PdfSolidBrush(Color.Black);
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
            PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center);
            format2.CharacterSpacing = 1f;
            text = "Summary of Science";
            page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);
            size = font2.MeasureString(text, format2);
            y = y + size.Height + 6;

            //icon
            PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Wikipedia_Science.png");
            page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y));
            float imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2;
            float imageBottom = image.PhysicalDimension.Height + y;

            //refenrence content
            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 9f));
            PdfStringFormat format3 = new PdfStringFormat();
            format3.ParagraphIndent = font3.Size * 2;
            format3.MeasureTrailingSpaces = true;
            format3.LineSpacing = font3.Size * 1.5f;
            String text1 = "(All text and picture from ";
            String text2 = "Wikipedia";
            String text3 = ", the free encyclopedia)";
            page.Canvas.DrawString(text1, font3, brush2, 0, y, format3);

            size = font3.MeasureString(text1, format3);
            float x1 = size.Width;
            format3.ParagraphIndent = 0;
            PdfTrueTypeFont font4 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Underline));
            PdfBrush brush3 = PdfBrushes.Blue;
            page.Canvas.DrawString(text2, font4, brush3, x1, y, format3);
            size = font4.MeasureString(text2, format3);
            x1 = x1 + size.Width;

            page.Canvas.DrawString(text3, font3, brush2, x1, y, format3);
            y = y + size.Height;

            //content
            PdfStringFormat format4 = new PdfStringFormat();
            text = System.IO.File.ReadAllText(@"..\..\..\..\..\..\Data\Summary_of_Science.txt");
            PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial", 10f));
            format4.LineSpacing = font5.Size * 1.5f;
            PdfStringLayouter textLayouter = new PdfStringLayouter();
            float imageLeftBlockHeight = imageBottom - y;
            PdfStringLayoutResult result
                = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight));
            if (result.ActualSize.Height < imageBottom - y)
            {
                imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight;
                result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight));
            }
            foreach (LineInfo line in result.Lines)
            {
                page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4);
                y = y + result.LineHeight;
            }
            PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2);
            PdfTextLayout textLayout = new PdfTextLayout();
            textLayout.Break = PdfLayoutBreakType.FitPage;
            textLayout.Layout = PdfLayoutType.Paginate;
            RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
            textWidget.StringFormat = format4;
            textWidget.Draw(page, bounds, textLayout);
        }
Ejemplo n.º 43
0
        private float DrawFormField(XPathNavigator fieldNode, PdfForm form, PdfPageBase page, float y, int fieldIndex)
        {
            float width = page.Canvas.ClientSize.Width;
            float padding = 2;

            //measure field label
            String label = fieldNode.GetAttribute("label", "");
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            float labelMaxWidth = width * 0.4f - 2 * padding;
            SizeF labelSize = font1.MeasureString(label, labelMaxWidth, format);

            //measure field height
            float fieldHeight = MeasureFieldHeight(fieldNode);

            float height = labelSize.Height > fieldHeight ? labelSize.Height : fieldHeight;
            height = height + 2;

            //draw background
            PdfBrush brush = PdfBrushes.SteelBlue;
            if (fieldIndex % 2 == 1)
            {
                brush = PdfBrushes.LightGreen;
            }
            page.Canvas.DrawRectangle(brush, 0, y, width, height);

            //draw field label
            PdfBrush brush1 = PdfBrushes.LightYellow;
            RectangleF labelBounds = new RectangleF(padding, y, labelMaxWidth, height);
            page.Canvas.DrawString(label, font1, brush1, labelBounds, format);

            //daw field
            float fieldMaxWidth = width * 0.57f - 2 * padding;
            float fieldX = labelBounds.Right + 2 * padding;
            float fieldY = y + (height - fieldHeight) / 2;
            String fieldType = fieldNode.GetAttribute("type", "");
            String fieldId = fieldNode.GetAttribute("id", "");
            bool required = "true" == fieldNode.GetAttribute("required", "");
            switch (fieldType)
            {
                case "text":
                case "password":
                    PdfTextBoxField textField = new PdfTextBoxField(page, fieldId);
                    textField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                    textField.BorderWidth = 0.75f;
                    textField.BorderStyle = PdfBorderStyle.Solid;
                    textField.Required = required;
                    if ("password" == fieldType)
                    {
                        textField.Password = true;
                    }
                    if ("true" == fieldNode.GetAttribute("multiple", ""))
                    {
                        textField.Multiline = true;
                        textField.Scrollable = true;
                    }
                    form.Fields.Add(textField);
                    break;
                case "checkbox":
                    PdfCheckBoxField checkboxField = new PdfCheckBoxField(page, fieldId);
                    float checkboxWidth = fieldHeight - 2 * padding;
                    float checkboxHeight = checkboxWidth;
                    checkboxField.Bounds = new RectangleF(fieldX, fieldY + padding, checkboxWidth, checkboxHeight);
                    checkboxField.BorderWidth = 0.75f;
                    checkboxField.Style = PdfCheckBoxStyle.Cross;
                    checkboxField.Required = required;
                    form.Fields.Add(checkboxField);
                    break;

                case "list":
                    XPathNodeIterator itemNodes = fieldNode.Select("item");
                    if ("true" == fieldNode.GetAttribute("multiple", ""))
                    {
                        PdfListBoxField listBoxField = new PdfListBoxField(page, fieldId);
                        listBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                        listBoxField.BorderWidth = 0.75f;
                        listBoxField.MultiSelect = true;
                        listBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
                        listBoxField.Required = required;
                        //add items into list box.
                        foreach (XPathNavigator itemNode in itemNodes)
                        {
                            String text = itemNode.SelectSingleNode("text()").Value;
                            listBoxField.Items.Add(new PdfListFieldItem(text, text));
                        }
                        listBoxField.SelectedIndex = 0;
                        form.Fields.Add(listBoxField);

                        break;
                    }
                    if (itemNodes != null && itemNodes.Count <= 3)
                    {
                        PdfRadioButtonListField radioButtonListFile
                            = new PdfRadioButtonListField(page, fieldId);
                        radioButtonListFile.Required = required;
                        //add items into radio button list.
                        float fieldItemHeight = fieldHeight / itemNodes.Count;
                        float radioButtonWidth = fieldItemHeight - 2 * padding;
                        float radioButtonHeight = radioButtonWidth;
                        foreach (XPathNavigator itemNode in itemNodes)
                        {
                            String text = itemNode.SelectSingleNode("text()").Value;
                            PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem(text);
                            fieldItem.BorderWidth = 0.75f;
                            fieldItem.Bounds = new RectangleF(fieldX, fieldY + padding, radioButtonWidth, radioButtonHeight);
                            radioButtonListFile.Items.Add(fieldItem);

                            float fieldItemLabelX = fieldX + radioButtonWidth + padding;
                            SizeF fieldItemLabelSize = font1.MeasureString(text);
                            float fieldItemLabelY = fieldY + (fieldItemHeight - fieldItemLabelSize.Height) / 2;
                            page.Canvas.DrawString(text, font1, brush1, fieldItemLabelX, fieldItemLabelY);

                            fieldY = fieldY + fieldItemHeight;
                        }
                        form.Fields.Add(radioButtonListFile);

                        break;
                    }

                    //combo box
                    PdfComboBoxField comboBoxField = new PdfComboBoxField(page, fieldId);
                    comboBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
                    comboBoxField.BorderWidth = 0.75f;
                    comboBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
                    comboBoxField.Required = required;
                    //add items into combo box.
                    foreach (XPathNavigator itemNode in itemNodes)
                    {
                        String text = itemNode.SelectSingleNode("text()").Value;
                        comboBoxField.Items.Add(new PdfListFieldItem(text, text));
                    }
                    form.Fields.Add(comboBoxField);
                    break;

            }

            if (required)
            {
                //draw *
                float flagX = width * 0.97f + padding;
                PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
                SizeF size = font3.MeasureString("*");
                float flagY = y + (height - size.Height) / 2;
                page.Canvas.DrawString("*", font3, PdfBrushes.Red, flagX, flagY);
            }

            return y + height;
        }
Ejemplo n.º 44
0
        void DrawAutomaticField(String fieldName, PdfPageBase page, RectangleF bounds)
        {
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfBrush brush = PdfBrushes.OrangeRed;
            PdfStringFormat format
                = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

            if ("DateTimeField" == fieldName)
            {
                PdfDateTimeField field = new PdfDateTimeField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                field.Draw(page.Canvas);
            }

            if ("CreationDateField" == fieldName)
            {
                PdfCreationDateField field = new PdfCreationDateField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                field.Draw(page.Canvas);
            }

            if ("DocumentAuthorField" == fieldName)
            {
                PdfDocumentAuthorField field = new PdfDocumentAuthorField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }


            if ("SectionNumberField" == fieldName)
            {
                PdfSectionNumberField field = new PdfSectionNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("SectionPageNumberField" == fieldName)
            {
                PdfSectionPageNumberField field = new PdfSectionPageNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("SectionPageCountField" == fieldName)
            {
                PdfSectionPageCountField field = new PdfSectionPageCountField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("PageNumberField" == fieldName)
            {
                PdfPageNumberField field = new PdfPageNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("PageCountField" == fieldName)
            {
                PdfPageCountField field = new PdfPageCountField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("DestinationPageNumberField" == fieldName)
            {
                PdfDestinationPageNumberField field = new PdfDestinationPageNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Page = page as PdfNewPage;
                field.Draw(page.Canvas);
            }

            if ("CompositeField" == fieldName)
            {
                PdfSectionPageNumberField field1 = new PdfSectionPageNumberField();
                field1.NumberStyle = PdfNumberStyle.LowerRoman;
                PdfSectionPageCountField field2 = new PdfSectionPageCountField();
                PdfCompositeField fields = new PdfCompositeField();
                fields.Font = font;
                fields.Brush = brush;
                fields.StringFormat = format;
                fields.Bounds = bounds;
                fields.AutomaticFields = new PdfAutomaticField[] { field1, field2 };
                fields.Text = "section page {0} of {1}";
                fields.Draw(page.Canvas);
            }
        }
Ejemplo n.º 45
0
        private PdfLayoutResult DrawPart(PdfPageBase page, DataTable parts, int index, String title, float y)
        {
            //draw title
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
            DataRow row = parts.Rows[index];
            page.Canvas.DrawString(title, font1, PdfBrushes.Black, 0, y);
            y = y + font1.MeasureString(title).Height + 1;

            //draw table
            Object[][] data = new Object[2][];
            data[0] = new String[parts.Columns.Count];
            for (int i = 0; i < parts.Columns.Count; i++)
            {
                data[0][i] = parts.Columns[i].ColumnName;
            }
            data[1] = row.ItemArray;

            PdfTable table = new PdfTable();
            table.Style.CellPadding = 2;
            table.Style.BorderPen = new PdfPen(PdfBrushes.Black, 0.75f);
            table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.GreenYellow;
            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 9f));
            table.Style.HeaderSource = PdfHeaderSource.Rows;
            table.Style.HeaderRowCount = 1;
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.ForestGreen;
            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold));
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            table.Style.ShowHeader = true;
            table.DataSource = data;

            float width
                = page.Canvas.ClientSize.Width
                    - (table.Columns.Count + 1) * table.Style.BorderPen.Width;
            for (int i = 0; i < table.Columns.Count; i++)
            {
                table.Columns[i].Width = i == 1 ? width * 0.35f : width * 0.13f;
            }

            PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
            tableLayout.Break = PdfLayoutBreakType.FitPage;
            tableLayout.Layout = PdfLayoutType.Paginate;

            return table.Draw(page, new PointF(0, y), tableLayout);
        }
Ejemplo n.º 46
0
        private void TransformText(PdfPageBase page)
        {
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw the text - transform           
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f);
            PdfSolidBrush brush1 = new PdfSolidBrush(Color.DeepSkyBlue);
            PdfSolidBrush brush2 = new PdfSolidBrush(Color.CadetBlue);

            page.Canvas.TranslateTransform(20, 200);
            page.Canvas.ScaleTransform(1f, 0.6f);
            page.Canvas.SkewTransform(-10, 0);
            page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush1, 0, 0);

            page.Canvas.SkewTransform(10, 0);
            page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, 0);

            page.Canvas.ScaleTransform(1f, -1f);
            page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, -2 * 18);
            //restor graphics
            page.Canvas.Restore(state);
        }
Ejemplo n.º 47
0
 private float DrawFormSection(String label, PdfPageBase page, float y)
 {
     PdfBrush brush1 = PdfBrushes.LightYellow;
     PdfBrush brush2 = PdfBrushes.DarkSlateGray;
     PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold));
     PdfStringFormat format = new PdfStringFormat();
     float height = font.MeasureString(label).Height;
     page.Canvas.DrawRectangle(brush2, 0, y, page.Canvas.ClientSize.Width, height + 2);
     page.Canvas.DrawString(label, font, brush1, 2, y + 1);
     y = y + height + 2;
     PdfPen pen = new PdfPen(PdfBrushes.LightSkyBlue, 0.75f);
     page.Canvas.DrawLine(pen, 0, y, page.Canvas.ClientSize.Width, y);
     return y + 0.75f;
 }
Ejemplo n.º 48
0
        private float DrawPageTitle(PdfPageBase page, float y)
        {
            PdfBrush brush1 = PdfBrushes.MidnightBlue;
            PdfBrush brush2 = PdfBrushes.Red;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold));
            String title = "Your Account Information(* = Required)";
            SizeF size = font1.MeasureString(title);
            float x = (page.Canvas.ClientSize.Width - size.Width) / 2;
            page.Canvas.DrawString("Your Account Information(", font1, brush1, x, y);
            size = font1.MeasureString("Your Account Information(");
            x = x + size.Width;
            page.Canvas.DrawString("* = Required", font1, brush2, x, y);
            size = font1.MeasureString("* = Required");
            x = x + size.Width;
            page.Canvas.DrawString(")", font1, brush1, x, y);
            y = y + size.Height;

            y = y + 3;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic));
            String p = "Your information is not public, shared in anyway, or displayed on this site.";
            page.Canvas.DrawString(p, font2, brush1, 0, y);

            return y + font2.Height;
        }
Ejemplo n.º 49
0
        private PdfLayoutResult DrawOrderItems(PdfPageBase page, DataTable orderItems, String title, float y)
        {
            //draw title
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold));
            page.Canvas.DrawString(title, font1, PdfBrushes.Black, 0, y);
            y = y + font1.MeasureString(title).Height + 1;

            PdfTable table = new PdfTable();
            table.Style.CellPadding = 2;
            table.Style.BorderPen = new PdfPen(PdfBrushes.Black, 0.75f);
            table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.MediumTurquoise;
            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f));
            table.Style.AlternateStyle = new PdfCellStyle();
            table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.PaleTurquoise;
            table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f));
            table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Teal;
            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Bold));
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            table.Style.ShowHeader = true;
            table.DataSource = orderItems;
            for (int i = 2; i < table.Columns.Count; i++)
            {
                table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
            }

            PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
            tableLayout.Break = PdfLayoutBreakType.FitPage;
            tableLayout.Layout = PdfLayoutType.Paginate;

            return table.Draw(page, new PointF(0, y), tableLayout);
        }
Ejemplo n.º 50
0
        private void DrawText(PdfPageBase page)
        {
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw text - brush
            String text = "Go! Turn Around! Go! Go! Go!";
            PdfPen pen = PdfPens.DeepSkyBlue;
            PdfSolidBrush brush = new PdfSolidBrush(Color.White);
            PdfStringFormat format = new PdfStringFormat();
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f, PdfFontStyle.Italic);
            SizeF size = font.MeasureString(text, format);
            RectangleF rctg
                = new RectangleF(page.Canvas.ClientSize.Width / 2 + 10, 180,
                size.Width / 3 * 2, size.Height * 2);
            page.Canvas.DrawString(text, font, pen, brush, rctg, format);

            //restor graphics
            page.Canvas.Restore(state);
        }
Ejemplo n.º 51
0
        private PdfLayoutResult DrawTable(PdfPageBase page, float y)
        {
            PdfBrush brush1 = PdfBrushes.Black;

            //create data table
            PdfTable table = new PdfTable();
            table.Style.CellPadding = 2;
            table.Style.BorderPen = new PdfPen(brush1, 0.75f);
            table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
            table.Style.AlternateStyle = new PdfCellStyle();
            table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow;
            table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
            table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            table.Style.ShowHeader = true;

            using (OleDbConnection conn = new OleDbConnection())
            {
                conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb";
                OleDbCommand command = new OleDbCommand();
                command.CommandText
                    = " select Description, OnHand, OnOrder, Cost, ListPrice from parts ";
                command.Connection = conn;
                using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command))
                {
                    DataTable dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);
                    table.DataSourceType = PdfTableDataSourceType.TableDirect;
                    table.DataSource = dataTable;
                }
            }
            float width
                = page.Canvas.ClientSize.Width
                    - (table.Columns.Count + 1) * table.Style.BorderPen.Width;
            for (int i = 0; i < table.Columns.Count; i++)
            {
                if (i == 0)
                {
                    table.Columns[i].Width = width * 0.40f * width;
                    table.Columns[i].StringFormat
                        = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
                }
                else
                {
                    table.Columns[i].Width = width * 0.15f * width;
                    table.Columns[i].StringFormat
                        = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
                }
            }

            PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
            tableLayout.Break = PdfLayoutBreakType.FitElement;
            tableLayout.Layout = PdfLayoutType.Paginate;

            PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout);
            y = result.Bounds.Bottom + 3;

            PdfBrush brush2 = PdfBrushes.Gray;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));
            result.Page.Canvas.DrawString(String.Format("* {0} parts in the list.", table.Rows.Count),
                font2, brush2, 5, y);

            return result;
        }
Ejemplo n.º 52
0
        private void DrawSpiro(PdfPageBase page)
        {
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();

            //Draw shap - spiro
            PdfPen pen = PdfPens.DeepSkyBlue;

            int nPoints = 1000;
            double r1 = 30;
            double r2 = 25;
            double p = 35;
            double x1 = r1 + r2 - p;
            double y1 = 0;
            double x2 = 0;
            double y2 = 0;

            page.Canvas.TranslateTransform(100, 100);

            for (int i = 0; i < nPoints; i++)
            {
                double t = i * Math.PI / 90;
                x2 = (r1 + r2) * Math.Cos(t) - p * Math.Cos((r1 + r2) * t / r2);
                y2 = (r1 + r2) * Math.Sin(t) - p * Math.Sin((r1 + r2) * t / r2);
                page.Canvas.DrawLine(pen, (float)x1, (float)y1, (float)x2, (float)y2);
                x1 = x2;
                y1 = y2;
            }

            //restor graphics
            page.Canvas.Restore(state);
        }
Ejemplo n.º 53
0
        private PdfLayoutResult DrawVendor(PdfPageBase page, DataTable vendors, int index, String title, float y)
        {
            //draw title
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
            DataRow row = vendors.Rows[index];
            page.Canvas.DrawString(title, font1, PdfBrushes.Black, 0, y);
            y = y + font1.MeasureString(title).Height + 1;

            //draw table
            Object[][] data = new Object[vendors.Columns.Count][];
            for (int i = 0; i < vendors.Columns.Count; i++)
            {
                data[i] = new Object[2];
                data[i][0] = vendors.Columns[i].ColumnName;
                data[i][1] = vendors.Rows[index].ItemArray[i];
            }

            PdfGrid grid = new PdfGrid();
            grid.Style.CellPadding = new PdfPaddings(2, 2, 1, 1);
            grid.DataSource = data;

            float width
                = page.Canvas.ClientSize.Width
                    - (grid.Columns.Count + 1) * 0.75f;
            grid.Columns[0].Width = width * 0.20f;
            grid.Columns[1].Width = width * 0.80f;

            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f));
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Style.Font = font2;
                grid.Rows[i].Cells[0].Style.BackgroundBrush = PdfBrushes.CadetBlue;
                grid.Rows[i].Cells[1].Style.BackgroundBrush = PdfBrushes.SkyBlue;
            }

            PdfGridLayoutFormat layout = new PdfGridLayoutFormat();
            layout.Break = PdfLayoutBreakType.FitPage;
            layout.Layout = PdfLayoutType.Paginate;

            return grid.Draw(page, new PointF(0, y), layout);
        }
Ejemplo n.º 54
0
        private void DrawPageHeaderAndFooter(PdfPageBase page, PdfMargins margin, bool isCover)
        {
            page.Canvas.SetTransparency(0.5f);
            PdfImage headerImage
                = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Header.png");
            PdfImage footerImage
                = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Footer.png");
            page.Canvas.DrawImage(headerImage, new PointF(0, 0));
            page.Canvas.DrawImage(footerImage, new PointF(0, page.Canvas.ClientSize.Height - footerImage.PhysicalDimension.Height));
            if (isCover)
            {
                page.Canvas.SetTransparency(1);
                return;
            }

            PdfBrush brush = PdfBrushes.Black;
            PdfPen pen = new PdfPen(brush, 0.75f);
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
            format.MeasureTrailingSpaces = true;
            float space = font.Height * 0.75f;
            float x = margin.Left;
            float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
            float y = margin.Top - space;
            page.Canvas.DrawLine(pen, x, y, x + width, y);
            y = y - 1 - font.Height;
            page.Canvas.DrawString("Demo of Spire.Pdf", font, brush, x + width, y, format);
            page.Canvas.SetTransparency(1);
        }
Ejemplo n.º 55
0
        private void TekenHelePyramideOpPapier(PdfPageBase page, int[,] pyramide, int basislengte, int papierhoogte)
        {
            PdfSolidBrush blackBrush = new PdfSolidBrush(Color.Black);
            PdfFont helv = new PdfFont(PdfFontFamily.Helvetica, 15f);
            Random rnd = new Random();
            int xOffset = 50;
            // draw the rectangles
            for (int rij = 0; rij <= basislengte - 1; rij++)
            {
                for (int aantal = 0; aantal <= (basislengte - 1 - rij); aantal++)
                {
                    int x = aantal * 50 + 25 * rij+xOffset;
                    int y = papierhoogte - rij * 25;
                    int rechthook = 50;

                    RectangleF rectangleFLine = new RectangleF(x - 6, y - 6, rechthook + 3, 25);
                    page.Canvas.DrawRectangle(blackBrush, rectangleFLine);

                    RectangleF rectangleF = new RectangleF(x - 4, y - 4, rechthook - 1, 22);
                    PdfLinearGradientBrush gradBrush = new PdfLinearGradientBrush(rectangleF, new PdfRGBColor(200, 200, 200), new PdfRGBColor(200, 200, 200), PdfLinearGradientMode.Horizontal);
                    page.Canvas.DrawRectangle(gradBrush, rectangleF);

                }
            }

            // draw all the numbers on the paper
            for (int rij = 0; rij <= basislengte - 1; rij++)
            {
                for (int aantal = 0; aantal <= (basislengte - rij - 1); aantal++)
                {
                    Console.Write(pyramide[rij, aantal].ToString() + " ");
                    int x = aantal * 50 + 25 * rij + 5 +xOffset;
                    int y = papierhoogte - rij * 25;

                    if (DezeCelTekenen(basislengte, rnd, rij, aantal))
                    {
                        page.Canvas.DrawString(pyramide[rij, aantal].ToString(),
                            helv,
                            blackBrush, x,
                            y);
                    }

                }
                Console.WriteLine();

            }
        }