예제 #1
0
    /// <summary>
    /// Generates PDF file with two pages.
    /// </summary>
    public static void GenerateRgbPdf()
    {
        var dpi = 300f;

        var unitFactory = new UnitFactory(dpi);

        using (var pdfWriter = new PdfWriter("../../../../_Output/PDFAndEPSOutput.pdf"))
            using (var graphics = pdfWriter.GetGraphics())
                using (var bitmap = new Bitmap("../../../../_Input/Chicago.jpg"))
                {
                    // Front side 3.5"×2.5" size

                    pdfWriter.AddPage(unitFactory.Inch(3.5f), unitFactory.Inch(2.0f), dpi, dpi);

                    {
                        // Rectangle
                        var pen = new Pen(RgbColor.Red, unitFactory.Point(1));

                        graphics.DrawRectangle(pen, unitFactory.Inch(0.125f), unitFactory.Inch(0.125f),
                                               unitFactory.Inch(3.5f - 0.125f * 2), unitFactory.Inch(2 - 0.125f * 2));

                        // Image
                        graphics.DrawImage(bitmap, new System.Drawing.RectangleF(
                                               unitFactory.Inch(0.25f), unitFactory.Inch(0.75f),
                                               unitFactory.Inch(0.25f + 0.5f * (float)bitmap.Height / (float)bitmap.Width), unitFactory.Inch(1.25f)));

                        // Text
                        var font = graphics.CreateFont("Arial", 32f);
                        var text = new PlainText("Front Side", font, new SolidBrush(RgbColor.Navy))
                        {
                            Alignment = TextAlignment.Left,
                            Position  = new System.Drawing.PointF(unitFactory.Inch(1.125f), unitFactory.Inch(1.125f))
                        };

                        graphics.DrawText(text);
                    }

                    // Back side	3.5"×2.5" size

                    pdfWriter.AddPage(unitFactory.Inch(3.5f), unitFactory.Inch(2.0f), dpi, dpi);

                    {
                        // Pen 0.5pt width
                        var pen = new Pen(RgbColor.Blue, unitFactory.Point(0.5f));

                        graphics.DrawRectangle(pen, unitFactory.Inch(0.125f), unitFactory.Inch(0.125f),
                                               unitFactory.Inch(3.5f - 0.125f * 2), unitFactory.Inch(2 - 0.125f * 2));

                        // Artistic (bridge) text
                        var font       = graphics.CreateFont("Times New Roman", "Italic", 24f);
                        var bridgeText = new BridgeText("Back Side", font, new SolidBrush(RgbColor.Green))
                        {
                            Bend   = 0.2f,
                            Center = new System.Drawing.PointF(unitFactory.Inch(3.5f / 2), unitFactory.Inch(1f))
                        };
                        graphics.DrawText(bridgeText);
                    }
                }
    }
예제 #2
0
    /// <summary>
    /// Creates business card in PDF format
    /// </summary>
    private static void CreateBusinessCard()
    {
        using (var writer = new PdfWriter("../../../../_Output/CreateBusinessCard.pdf"))
        {
            int width  = 350;
            int height = 200;

            using (var graphics = writer.GetGraphics())
            {
                // Front side
                writer.AddPage(width, height, RgbColor.White);

                var blueBrush = new SolidBrush(RgbColor.DeepSkyBlue);

                // Draw circle
                graphics.DrawEllipse(new Pen(RgbColor.DeepSkyBlue, 2f), 15f, 20f, 30f, 30f);

                // Draw text
                var font = graphics.CreateFont("Arial", 18f);
                var text = new PlainText("Front side", font, new SolidBrush(RgbColor.Gray),
                                         95f, 41f, TextAlignment.Center);
                graphics.DrawText(text);

                font = graphics.CreateFont("Arial", 16f);
                text = new PlainText(@"John Doe
<span style=""color:gray;font-size:16pt"">General Manager</span>", font, blueBrush, 335f, 100f, TextAlignment.Right);

                graphics.DrawText(text);

                graphics.FillRectangle(blueBrush, 0, height - 50, width, 50);

                font = graphics.CreateFont("Arial", 12f);
                text = new PlainText(@"123.456.7890
[email protected]", font, new SolidBrush(RgbColor.White), 15f, 170f, TextAlignment.Left);

                graphics.DrawText(text);

                text = new PlainText(@"335 Cloverfield Blvd
Charlington, NY 10123", font, new SolidBrush(RgbColor.White), 200f, 170f, TextAlignment.Left);

                graphics.DrawText(text);

                // Back side
                writer.AddPage(width, height, RgbColor.DeepSkyBlue);

                graphics.DrawEllipse(new Pen(RgbColor.White, 3f), 65f, 72f, 55f, 55f);

                font = graphics.CreateFont("Arial", 36);

                text = new PlainText("Back side", font, new SolidBrush(RgbColor.White), 140f, 112f, TextAlignment.Left);
                graphics.DrawText(text);
            }
        }
    }
예제 #3
0
    /// <summary>
    /// Saves raster and vector graphics to PDF format
    /// </summary>
    private static void WriteRasterAndVectorGraphicsToPdf()
    {
        using (var writer = new PdfWriter("../../../../_Output/WriteRasterAndVectorGraphicsToPdf.pdf"))
        {
            // Reduce output file size
            writer.Compression = CompressionType.Jpeg;
            writer.Quality     = 80;

            writer.AddPage(800, 650, RgbColor.White);

            using (var graphics = writer.GetGraphics())
            {
                // Draw bitmap
                using (var bitmap = new Bitmap("../../../../_Input/Chicago.jpg"))
                {
                    graphics.DrawImage(bitmap, 100f, 100f);
                }

                // Draw rectangle
                graphics.DrawRectangle(new Pen(RgbColor.Gray, 4f), 50f, 50f, 700f, 550f);

                // Draw text
                var font = graphics.CreateFont("Arial", 56f);
                var text = new PlainText("Confidential", font, new SolidBrush(RgbColor.OrangeRed),
                                         400f, 340f, TextAlignment.Center);
                graphics.DrawText(text);
            }
        }
    }
예제 #4
0
    /// <summary>
    /// Fills a rectangle with a PANTONE color
    /// </summary>
    private static void DrawOnPdfWithSpotColor()
    {
        using (var pdfWriter = new PdfWriter(@"../../../../_Output/VectorSpot.pdf"))
            using (var gr = pdfWriter.GetGraphics())
            {
                pdfWriter.AddPage(500, 500, 72, 72);

                gr.FillRectangle(new SolidBrush(spotColor), 10, 10, 400, 400);

                pdfWriter.Close();
            }
    }
예제 #5
0
        /// <summary>
        /// Unisce gli stream pdf contenuti nella lista passata in un unico stream pdf
        /// </summary>
        /// <param name="files">lista stream pdf</param>
        /// <returns></returns>
        public static byte[] MergePDFs(System.Collections.Generic.List <byte[]> files)
        {
            MemoryStream ms = null;

            byte[]    result = null;
            ArrayList master = null;

            ms     = new MemoryStream();
            master = new ArrayList();
            int      f        = 0;
            Document document = null;
            PdfCopy  writer   = null;

            while (f < files.Count)
            {
                PdfReader reader = new PdfReader(files[f]);
                reader.ConsolidateNamedDestinations();
                int n = reader.NumberOfPages;
                if (f == 0)
                {
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    writer   = new PdfCopy(document, ms);
                    document.Open();
                }
                for (int i = 0; i < n;)
                {
                    ++i;
                    if (writer != null)
                    {
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        writer.AddPage(page);
                    }
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null && writer != null)
                {
                    writer.CopyAcroForm(reader);
                }
                f++;
            }
            if (document != null)
            {
                document.Close();
            }
            result = ms.ToArray();
            ms.Close();
            return(result);
        }
예제 #6
0
    private static void MergeLayersPdf()
    {
        using (var psdReader = new PsdReader("../../../../_Input/Seal.psd"))
        {
            // The same approach can be used for EPS

            using (var pdfWriter = new PdfWriter("../../../../_Output/CurvedText.pdf"))
            {
                pdfWriter.AddPage(psdReader.Width, psdReader.Height, psdReader.DpiX, psdReader.DpiY, RgbColor.White);

                using (var graphics = pdfWriter.GetGraphics())
                {
                    MergeLayers(psdReader, graphics, GetLayerText);
                }
            }
        }
    }
예제 #7
0
        /// <summary>
        /// Demonstrates how to edit shape stroke color with DrawPath event
        /// </summary>
        private static void RecolorRedStrokes(string inPdf, string outPdf)
        {
            using (var reader = new PdfReader(inPdf))
                using (var gc = reader.Frames[0].GetContent())
                    using (var writer = new PdfWriter(outPdf))
                        using (var gr = writer.GetGraphics())
                        {
                            writer.AddPage(gc.Width, gc.Height, gc.DpiX, gc.DpiY);

                            gr.BeforeDrawPath += (sender, e) =>
                            {
                                if (e.Pen != null)
                                {
                                    e.Pen = new Pen(RgbColor.DarkMagenta, e.Pen.Width);
                                }
                            };

                            gr.DrawContainer(gc, 0, 0);
                        }
        }
예제 #8
0
    /// <summary>
    /// Converts the specified process color into a spot color saving graphics container to the PDF format
    /// </summary>
    private static void ReplaceProcessColor()
    {
        using (var reader = new PdfReader(@"../../../../_Input/Seal.pdf"))
            using (var gc = reader.Frames[0].GetContent())
                using (var writer = new PdfWriter(@"../../../../_Output/Process2SpotColor.pdf"))
                    using (var gr = writer.GetGraphics())
                    {
                        writer.AddPage(gc.Width, gc.Height, gc.DpiX, gc.DpiY);

                        gr.BeforeDrawPath += (sender, e) =>
                        {
                            if (e.Brush != null && e.Brush is SolidBrush && (e.Brush as SolidBrush).Color.IsBlack())
                            {
                                e.Brush = new SolidBrush(spotColor);
                            }
                        };

                        gr.DrawContainer(gc, 0, 0);
                    }
    }
예제 #9
0
 /// <summary>
 /// Creates a new empty PDF
 /// </summary>
 public PdfFile()
 {
     _writer = new PdfWriter();
     _writer.AddPage(_pageWidthPt, _pageHeightPt);
     COMDisposable.Subscribe(this, typeof(ComInterfaces._PdfFile));
 }
예제 #10
0
 /// <summary>
 /// Adds a new page.
 /// </summary>
 public void AddPage()
 {
     EnsureNotDisposed();
     _writer.AddPage(_pageWidthPt, _pageHeightPt);
     _contentHeightPt = 0;
 }
예제 #11
0
        public static void Main(string[] args)
        {
            var fileName = "out.pdf";

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (var fs = File.OpenWrite(fileName))
                using (var writer = new PdfWriter(fs))
                {
                    var bleed    = new Spacing(3, Unit.Millimeters);
                    var mediaBox = Sizes.A4.Expand(bleed).AsRectangle();
                    var trimBox  = mediaBox.Contract(bleed);

                    writer
                    // Set document info
                    .SetDocumentInfo(info =>
                    {
                        info.Author = "Gerard Gunnewijk";
                        info.Title  = "Example 1";
                    })
                    // Test placement using rectangle
                    .AddPage(page =>
                    {
                        page.MediaBox = mediaBox;
                        page.TrimBox  = trimBox;

                        using (var barrenStream = File.OpenRead("Pexels_com/arid-barren-desert-1975514.jpg"))
                            using (var barrenImage = SixLabors.ImageSharp.Image.Load(barrenStream))
                            {
                                var scale = (double)barrenImage.Width / barrenImage.Height;

                                page.AddImage(barrenImage, new Rectangle(0, 0, scale * 303, 303, Unit.Millimeters));
                            }

                        using (var eyeStream = File.OpenRead("Pexels_com/adult-blue-blue-eyes-865711.jpg"))
                        {
                            var scale = 3456d / 5184;

                            var width  = 100;
                            var height = 100 * scale;

                            var offSet = 6;
                            page.AddImage(eyeStream, new Rectangle(offSet, offSet, width + offSet, height + offSet, Unit.Millimeters));
                        }
                    })
                    // Test shape graphics
                    .AddPage(page =>
                    {
                        page.AddShapes(ctx =>
                        {
                            ctx.DefaultState(g =>
                            {
                                g.LineWidth = 1;
                                g.Fill      = null;
                                g.Stroke    = null;
                                g.Dash      = new Dash()
                                {
                                    Array = Array.Empty <double>(),
                                    Phase = 0
                                };
                                g.MiterLimit = 10;
                                g.LineCap    = LineCapStyle.ButtCap;
                                g.LineJoin   = LineJoinStyle.MiterJoin;
                            });

                            ctx.NewPath(g => { g.Fill = PredefinedColors.Red; g.Stroke = PredefinedColors.Black; g.LineWidth = 5; })
                            .Move(100, 100)
                            .LineTo(200, 100)
                            .LineTo(200, 200)
                            .LineTo(100, 200);
                            ctx.NewPath(g => { g.Fill = PredefinedColors.Blue; g.Stroke = null; })
                            .Move(50, 50)
                            .LineTo(150, 50)
                            .LineTo(150, 150)
                            .LineTo(50, 150)
                            .Close();
                            ctx.NewPath(g => { g.Fill = null; g.Stroke = PredefinedColors.Yellow; g.LineWidth = 3; g.Dash = new Dash()
                                               {
                                                   Array = new[] { 5d }
                                               }; })
                            .Move(150, 150)
                            .LineTo(250, 150)
                            .LineTo(250, 250)
                            .LineTo(150, 250)
                            .Close();
                        });
                    })
                    // Test placement using matrix
                    .AddPage(page =>
                    {
                        page.MediaBox = mediaBox;
                        page.TrimBox  = trimBox;

                        using (var forestStream = File.OpenRead("Pexels_com/android-wallpaper-art-backlit-1114897.jpg"))
                            using (var forestImage = SixLabors.ImageSharp.Image.Load(forestStream))
                            {
                                var scale = (double)forestImage.Width / forestImage.Height;

                                var matrix = Matrix.CreateScaleMatrix(new Value(scale * 303, Unit.Millimeters), new Value(303, Unit.Millimeters))
                                             .Translate(new Value(-100, Unit.Millimeters), new Value(0, Unit.Millimeters));

                                page.AddImage(forestImage, matrix);
                            }
                    });

                    using (var blurStream = File.OpenRead("Pexels_com/4k-wallpaper-blur-bokeh-1484253.jpg"))
                        using (var blurImage = SixLabors.ImageSharp.Image.Load(blurStream))
                        {
                            var reusedImage = writer.AddImage(blurImage);

                            for (int i = 0; i < 4; i++)
                            {
                                writer.AddPage(page =>
                                {
                                    page.MediaBox = mediaBox;
                                    page.TrimBox  = trimBox;

                                    var scale = (double)blurImage.Width / blurImage.Height;

                                    page.AddImage(reusedImage, new Rectangle(0, 0, scale * 303, 303, Unit.Millimeters));
                                });
                            }
                        }
                }
        }