iTextSharp z-index

 ̄綄美尐妖づ 提交于 2019-12-06 08:47:59
mkl

The explanation you posted as an answer (BTW, more apropos would have been to edit your question to contain that data) explains the issue.

There are two principal types of objects visible on a PDF page:

  1. the PDF page content;
  2. annotations associated with the page.

The annotations are always displayed above the page content if they are displayed at all.

In your case you add the image to the PDF page content (using OverContent or UnderContent only changes where in relation to other PDF page content material your additions appear). The stamp, on the other hand, most likely is realized by means of an annotation. Thus, the stamp annotation always is above your additions.

If you want to have your additions appear above the stamp, you either have to add your additions as some kind of annotation, too, or you have to flatten the stamp annotation into the page content before adding your stuff.

Which of these varients is better, depends on the requirements you have. Are there any requirements forcing the stamp to remain a stamp annotation? Are there any requirements forcing your additions to remain part of the content? Please elaborate your requirements. As content and annotations have some different properties when displayed or printed, please state all requirements.

And furthermore, please supply sample documents.

So like I said the original pdf have a stamp saved inside it, if I open the pdf with acrobat reader I can move the stamp.

So here my code to write some strokes :

        using (var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
        using (var intputStream = new FileStream(pathPdf, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            PdfReader reader = new PdfReader(intputStream);
            using (var pdfStamper = new PdfStamper(reader, outputStream))
            {
                foreach (var page in pages)
                {                                                
                    if (page != null && page.ExportedImages.HasItems())
                    {                                                                                                                                            
                        PdfContentByte pdfContent = pdfStamper.GetOverContent(page.PageIndex);
                        Rectangle pageSize = reader.GetPageSizeWithRotation(page.PageIndex);                            
                        PdfLayer pdfLayer = new PdfLayer(string.Format(ANNOTATIONNAMEWITHPAGENAME, page.PageIndex), pdfContent.PdfWriter);

                        foreach (ExporterEditPageInfoImage exportedInfo in page.ExportedImages)
                        {
                            Image image = PngImage.GetImage(exportedInfo.Path);                                                                
                            image.Layer = pdfLayer;

                            if (quality == PublishQuality.Normal || quality == PublishQuality.Medium || quality == PublishQuality.High)
                            {
                                float width = (float)Math.Ceiling((image.Width / image.DpiX) * 72);
                                float height = (float)Math.Ceiling((image.Height / image.DpiY) * 72);
                                image.ScaleAbsolute(width, height);

                                float x = (float)(exportedInfo.HorizontalTile * (page.TileSize * (72 / 96d)));
                                float y = (float)Math.Max(0, (pageSize.Height - ((exportedInfo.VerticalTile + 1) * (page.TileSize * (72 / 96d)))));
                                image.SetAbsolutePosition(x, y);                                    
                            }
                            else
                                throw new NotSupportedException();

                            pdfContent.AddImage(image);                                
                            GC.Collect();
                            GC.WaitForPendingFinalizers();
                        }
                    }
                }

                pdfStamper.Close();
            }
        }

So my strokes are saved good in the pdf the problem the stamp is always on top of everything and I think is normal so can I do a workaround for this ?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!