Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...