private void AddPage(Document pdfDocument, string filePath)
        {
            var imageInfo = GetImageInfoEx(filePath);
            var page      = pdfDocument.Pages.Add();

            // Resize page, or we have default A4 Size
            page.SetPageSize(imageInfo.PageSize.Width, imageInfo.PageSize.Height);

            // add image to Images collection of Page Resources
            page.Resources.Images.Add(imageInfo.Stream);
            // using GSave operator: this operator saves current graphics state
            page.Contents.Add(new Operator.GSave());

            // create Rectangle and Matrix objects
            var rectangle = new Rectangle(0, 0, imageInfo.PageSize.Width, imageInfo.PageSize.Height);
            var matrix    = new Matrix(new[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

            // using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
            page.Contents.Add(new Operator.ConcatenateMatrix(matrix));
            var ximage = page.Resources.Images[page.Resources.Images.Count];

            // using Do operator: this operator draws image
            page.Contents.Add(new Operator.Do(ximage.Name));
            // using GRestore operator: this operator restores graphics state
            page.Contents.Add(new Operator.GRestore());
            page.FreeMemory();
            Log.Verbose("Added page to pdf document {documentName}", pdfDocument.FileName);
        }
Example #2
0
        static void Main(string[] args)
        {
            var doc  = new Document();
            var page = doc.Pages.Add();

            page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello World!"));

            int lowerLeftX  = 40;
            int lowerLeftY  = 830;
            int upperRightX = 120;
            int upperRightY = 790;

            var logoFile = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "logo.jpg");

            FileStream imageStream = new FileStream(logoFile, FileMode.Open, FileAccess.Read);

            page.Resources.Images.Add(imageStream);
            page.Contents.Add(new Aspose.Pdf.Operators.GSave());
            Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
            Aspose.Pdf.Matrix    matrix    = new Aspose.Pdf.Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
            page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
            XImage ximage = page.Resources.Images[page.Resources.Images.Count];

            page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
            page.Contents.Add(new Aspose.Pdf.Operators.GRestore());


            var pdfFile = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "new.pdf");


            doc.Save(pdfFile);


            Console.WriteLine("all done..");
        }