Placing Image in PDF

Hi,

I have used different methods to place an image on a PDF page (for example Add Image to PDF using C#|Aspose.PDF for .NET and Set Image Size|Aspose.PDF for .NET and also with the AddImage method of the page).
All work BUT the image is always placed “behind” other elements on the page (for example a rectangle…).

How can I define z-order of image?

Best Regards
Yahia

One example source code (with AddImage):

        using (System.IO.MemoryStream vMStream = new System.IO.MemoryStream(Properties.Resources.NotoSans_Bold))
        {
            Font = Aspose.Pdf.Text.FontRepository.OpenFont(vMStream, Aspose.Pdf.Text.FontTypes.OTF);
        };

        using (var vPDFDoc = new Aspose.Pdf.Document())
        {

            vPDFDoc.DisableFontLicenseVerifications = true;

            vPDFDoc.PageLayout = Aspose.Pdf.PageLayout.SinglePage;

            var vPDFPage = vPDFDoc.Pages.Add();
            var vPageSize = new Aspose.Pdf.PageSize(Aspose.Pdf.PageSize.A3.Width, Aspose.Pdf.PageSize.A3.Height) { IsLandscape = true };
            vPDFPage.SetPageSize(vPageSize.Width, vPageSize.Height);

            vPDFPage.PageInfo.Margin = new Aspose.Pdf.MarginInfo(0, 0, 0, 0);

            var vPageFullRECT = vPDFPage.GetPageRect(true);
            var vPageGrossRECT = new Aspose.Pdf.Rectangle((vPageFullRECT.Width * 15) / 1000, (vPageFullRECT.Height * 15) / 1000, (vPageFullRECT.Width * 985) / 1000, (vPageFullRECT.Height * 985) / 1000);

            var vCurrY = vPageGrossRECT.URY;

            var vX = 0.0;

            var vW = vPDFPage.Rect.Width;
            var vH = (40.0 / 297.0) * vPDFPage.Rect.Height;
            var vY = vCurrY - vH;


            Aspose.Pdf.Drawing.Graph vG = new Aspose.Pdf.Drawing.Graph((float)vPageFullRECT.Width, (float)vPageFullRECT.Height) { IsInLineParagraph = true, IsChangePosition = false };

            var vRect2Draw = new Aspose.Pdf.Drawing.Rectangle((float)vX, (float)vY, (float)vW, (float)vH);
            vRect2Draw.GraphInfo.Color = Aspose.Pdf.Color.Black;
            vRect2Draw.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(208.0 / 255.0, 2.0 / 255.0, 27.0 / 255.0);
            vRect2Draw.GraphInfo.LineWidth = 1;

            string vText = "TOP-HIT";

            var vCurrX = vX;
            var vFontSize = 100;

            var vTextFontSize = 100;
            vCurrY = vY + 130;

            vX = vX + 1.5;
            vY = vY + 1.5;
            vW = vW - 3;
            vH = vH - 3;

            Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment(vText);
            textFragment.IsInLineParagraph = true;

            textFragment.Position.XIndent = 0;
            textFragment.Position.YIndent = vY / 2;
            textFragment.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
            textFragment.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
            textFragment.TextState.Font = Font;
            textFragment.TextState.FontSize = (float)vTextFontSize;
            textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(248.0 / 255.0, 231.0 / 255.0, 28.0 / 255.0);
            textFragment.TextState.DrawTextRectangleBorder = true;

            textFragment.TextState.FormattingOptions = new Aspose.Pdf.Text.TextFormattingOptions();
            textFragment.TextState.FormattingOptions.WrapMode = Aspose.Pdf.Text.TextFormattingOptions.WordWrapMode.ByWords;
            textFragment.TextState.FormattingOptions.LineSpacing = Aspose.Pdf.Text.TextFormattingOptions.LineSpacingMode.FullSize;
            textFragment.ZIndex = 100;

            vG.Shapes.Add(vRect2Draw);
            vG.Shapes[0].Text = textFragment;
            vPDFPage.Paragraphs.Add(vG);

            vPDFPage.Paragraphs.Add(textFragment);

            using (var vImageStream = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(vF)))
            {
                vPDFPage.AddImage(vImageStream, new Aspose.Pdf.Rectangle(0, vPageFullRECT.Height - 200, 200, vPageFullRECT.Height));

                vPDFDoc.Save("test.pdf");
            };
        };

@it-yeq

We tried using image stamp and have set the z-index of Graph to -1 to render the image above it but we were not able to achieve the expected results. Therefore, an investigation ticket as PDFNET-50299 has been logged in our issue management system. We will further look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Any update on this?

@it-yeq

We are afraid that the earlier logged ticket has not been reviewed yet. As it was logged recently in our issue tracking system, it is pending for investigation. We will surely analyze and resolve it on first come first serve basis and let you know once we have some updates in this regard. Please give us some time.

We are sorry for the inconvenience.

any news on this?

@it-yeq

We are afraid that no updates are available regarding ticket resolution at the moment. As soon as we complete its analysis, we will be able to share some news about its resolution. We will let you know once we make some progress towards ticket resolution. Please give us some time.

We are sorry for the inconvenience.

Any update regarding the placing of images / z-order?

@it-yeq

Regretfully, the earlier logged ticket could not get resolved due to other pending issues in the queue which were logged prior to it. However, we will surely inform you as soon as we have some significant updates regarding ticket resolution. We appreciate your patience and comprehension in this regard.

We apologize for your inconvenience.

any news regarding this issue?

@it-yeq

We are afraid that the earlier logged ticket is not resolved yet. Your concerns have already been recorded and we will surely notify you in this forum thread as soon as we have some updates about ticket resolution or its fix ETA. Your patience is highly appreciated in this regard.

We apologize for the inconvenience.

any news yet?

@it-yeq

We are afraid that no updates are available at the moment regarding ticket resolution. However, we will surely inform you once we make any progress in this regard. We apologize for the inconvenience.

Hi,

Is it possible to get this somehow fixed within the next 4 weeks? Is there something like a paid fix?

Thanks in advance.

Best Regards
Yahia

@it-yeq

We do offer paid support option where issues are resolved on urgent basis and have highest priority. We will soon let you know how soon the resolution can be made in case you use this option.

@it-yeq

We are investigating the earlier logged ticket. We would like to get more information for analysis.
In your example, there are two identical inscriptions and it is unclear which one was earlier and which one was added. It is better to use different labels. Can you please share output files generated at your end for each of the methods used?

@asad.ali
I am not sure what you mean by “identical inscriptions”?

@it-yeq

If you check the output generated by using complete example of yours, you can see that there are two images inside it. One is added by using ImageStamp and other is drawn using Graph functionality: image.png (4.8 KB)

We request can you please share two output files i.e. one generated using Graph only in your environment and other using Image Stamp?

@it-yeq

Before adding an image, you need to invoke pdfDocument.ProcessParagraphs() method to apply changes:

using (var vPDFDoc = new Aspose.Pdf.Document())
{
    vPDFDoc.DisableFontLicenseVerifications = true;
    vPDFDoc.PageLayout = Aspose.Pdf.PageLayout.SinglePage;

    var vPDFPage = vPDFDoc.Pages.Add();
    var vPageSize = new Aspose.Pdf.PageSize(Aspose.Pdf.PageSize.A3.Width, Aspose.Pdf.PageSize.A3.Height) { IsLandscape = true };
    vPDFPage.SetPageSize(vPageSize.Width, vPageSize.Height);
    vPDFPage.PageInfo.Margin = new Aspose.Pdf.MarginInfo(0, 0, 0, 0);

    var vPageFullRECT = vPDFPage.GetPageRect(true);
    var vPageGrossRECT = new Aspose.Pdf.Rectangle((vPageFullRECT.Width * 15) / 1000, (vPageFullRECT.Height * 15) / 1000, (vPageFullRECT.Width * 985) / 1000, (vPageFullRECT.Height * 985) / 1000);
    var vCurrY = vPageGrossRECT.URY;

    var vX = 0.0;
    var vW = vPDFPage.Rect.Width;
    var vH = (40.0 / 297.0) * vPDFPage.Rect.Height;
    var vY = vCurrY - vH;

    Aspose.Pdf.Drawing.Graph vG = new Aspose.Pdf.Drawing.Graph((float)vPageFullRECT.Width, (float)vPageFullRECT.Height) { IsInLineParagraph = true, IsChangePosition = false };
    vG.ZIndex = -1;

    var vRect2Draw = new Aspose.Pdf.Drawing.Rectangle((float)vX, (float)vY, (float)vW, (float)vH);
    vRect2Draw.GraphInfo.Color = Aspose.Pdf.Color.Black;
    vRect2Draw.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(208.0 / 255.0, 2.0 / 255.0, 27.0 / 255.0);
    vRect2Draw.GraphInfo.LineWidth = 1;

    string vText = "TOP-HIT";

    var vCurrX = vX;
    var vFontSize = 100;
    var vTextFontSize = 100;
    vCurrY = vY + 130;
    vX = vX + 1.5;
    vY = vY + 1.5;
    vW = vW - 3;
    vH = vH - 3;

    Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment(vText);
    textFragment.IsInLineParagraph = true;
    textFragment.Position.XIndent = 0;
    textFragment.Position.YIndent = vY / 2;
    textFragment.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
    textFragment.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
    textFragment.TextState.Font = FontRepository.FindFont("Arial");
    textFragment.TextState.FontSize = (float)vTextFontSize;
    textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(248.0 / 255.0, 231.0 / 255.0, 28.0 / 255.0);
    textFragment.TextState.DrawTextRectangleBorder = true;
    textFragment.TextState.FormattingOptions = new Aspose.Pdf.Text.TextFormattingOptions();
    textFragment.TextState.FormattingOptions.WrapMode = Aspose.Pdf.Text.TextFormattingOptions.WordWrapMode.ByWords;
    textFragment.TextState.FormattingOptions.LineSpacing = Aspose.Pdf.Text.TextFormattingOptions.LineSpacingMode.FullSize;
    textFragment.ZIndex = 100;

    vG.Shapes.Add(vRect2Draw);
    vG.Shapes[0].Text = textFragment;
    vPDFPage.Paragraphs.Add(vG);
    vPDFPage.Paragraphs.Add(textFragment);

    using (var vImageStream = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(dataDir + "aspose.png")))
    {
        ImageStamp stamp = new ImageStamp(vImageStream);
        stamp.Background = false;
        stamp.XIndent = 200;
        stamp.YIndent = vPageFullRECT.Height - 100;
        stamp.Height = 100;
        stamp.Width = 200;

        vPDFPage.AddStamp(stamp);
        vPDFDoc.Save(dataDir + "test.pdf");
    }
}