Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

c# - The stamps (pictures) overlap in the PDF

I wrote the following code to stamp a PDF.

imagefile = stamps to be added

filename = PDF's file path

My problem is that the stamps added to each page are overlapping. I want to bring these stamps together. How can I do it? Thanks.

 private void damga(string resimdosyas?, string dosyayolu)
    {
       


        byte[] bytes = File.ReadAllBytes(dosyayolu);

        PdfContentByte waterMark;

        using (MemoryStream stream = new MemoryStream())
        {
            PdfReader reader = new PdfReader(bytes);
            using (PdfStamper stamper = new PdfStamper(reader, stream))
            {
                int sayfa = reader.NumberOfPages;
                for (int i = 1; i <= sayfa; i++)
                {
                    waterMark = stamper.GetUnderContent(i);
                    string[] files = Directory.GetFiles(resimdosyas?);
                    foreach (string file in files)
                    {
                        var resim = iTextSharp.text.Image.GetInstance("C://Users//tufanc//source//repos//PdfStamp//PdfStamp//stampimage//" + Path.GetFileName(file));
                        
                       
                        resim.SetAbsolutePosition(0, 100);
                        waterMark.AddImage(resim);
                    }
                }
            }
            bytes = stream.ToArray();
        }
        File.WriteAllBytes(dosyayolu, bytes);



    }
question from:https://stackoverflow.com/questions/65896537/the-stamps-pictures-overlap-in-the-pdf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  private void damga(string resimdosyas?, string dosyayolu)
    {


        byte[] bytes = File.ReadAllBytes(dosyayolu);

        PdfContentByte waterMark;

        using (MemoryStream stream = new MemoryStream())
        {
            PdfReader reader = new PdfReader(bytes);
            using (PdfStamper stamper = new PdfStamper(reader, stream))
            {

                int sayfa = reader.NumberOfPages;
                for (int i = 1; i <= sayfa; i++)
                {
                    waterMark = stamper.GetUnderContent(i);
                    string[] files = Directory.GetFiles(resimdosyas?);
                    foreach (string file in files)
                    {
                        int imzasayisi = files.Length;
                        for (int imza = 1; imza <= imzasayisi; imza++)
                        {
                            

                        var resim = iTextSharp.text.Image.GetInstance("C://Users//tufanc//source//repos//PdfStamp//PdfStamp//stampimage//" + Path.GetFileName(file)); 

                       
                            int imzax = 1;
                            int imzay = 100;

                            imzax = imzax * imza;
                            imzay = imzay * imza;

                            resim.SetAbsolutePosition(imzax, imzay);
                            
                            waterMark.AddImage(resim);
                        }
                    }
                }
            }
            bytes = stream.ToArray();
        }
        File.WriteAllBytes(dosyayolu, bytes);



    }

I changed like this, but I set the for loop incorrectly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...