【问题标题】:itextsharp adding image to existing pdfitextsharp将图像添加到现有的pdf
【发布时间】:2017-08-03 22:01:15
【问题描述】:

我正在尝试使用 itextsharp 添加图像但没有任何运气 有大量教程用于将图像添加到新的 pdf 文档但不是现有的 pdf,因此 .add menthod 不可用

我正在尝试使用压模写入方法添加图像 我没有收到任何错误,但没有显示图像

PdfReader reader = new PdfReader(pdfIn); //get pdf

        if (File.Exists(pdfOut)) File.Delete(pdfOut); //reset 
        FileStream fs = new FileStream(pdfOut, FileMode.Create);

        PdfStamper stamper = new PdfStamper(reader, fs);
        try
        {
            //  Convert base64string to bytes array
            Byte[] bytes = Convert.FromBase64String(base64decode);
            iTextSharp.text.Image sigimage = iTextSharp.text.Image.GetInstance(bytes);//
            sigimage.SetAbsolutePosition(10, 10);
            sigimage.ScaleToFit(140f, 120f);
            stamper.Writer.Add(sigimage);
        }catch (DocumentException dex){//log exception here
        }catch (IOException ioex){//log exception here
        }



        AcroFields fields = stamper.AcroFields;
        //repeat for each pdf form fill field       
        fields.SetField("agencyName", name.Value);


        stamper.FormFlattening = true; // set to true to lock pdf from being editable
        stamper.Writer.CloseStream = true;
        stamper.Close();
        reader.Close();
        fs.Close();

【问题讨论】:

    标签: c#


    【解决方案1】:

    我认为您尝试以下将其添加到字节

            PdfReader reader = new PdfReader(pdfIn)
            FileStream fs = new FileStream(pdfOut, FileMode.Create);
            var stamper = new PdfStamper(reader, fs);
            var pdfContentByte = stamper.GetOverContent(1);
            iTextSharp.text.Image sigimage = iTextSharp.text.Image.GetInstance(bytes);
            sigimage.SetAbsolutePosition(100, 100);
            pdfContentByte.AddImage(sigimage);
    

    【讨论】:

    • 谢谢谢谢谢谢!那是不使用 pdfContentByte 的问题
    • @Coldtold 你能解释一下,如何插入新页面吗?(通过压模)。所以拿现有的pdf并在新页面中插入一个图像(专门用于图像)!?谢谢!
    【解决方案2】:

    使用以下代码,您可以将图像添加到现有 pdf 文件中的每个页面。 (我将此代码用于桌面应用程序

    string FileLocation = @"C:\\test\\pdfFileName.pdf"; // file path of pdf file
    var uri = new Uri(@"pack://application:,,,/projrct_name;component/View/Icons/funnelGreen.png"); // use image from project/application folder (this image will insert to pdf)
    var resourceStream = Application.GetResourceStream(uri).Stream; 
    PdfReader pdfReader = new PdfReader(FileLocation);
    PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(FileLocation.Replace(".pdf", "(tempFile).pdf"), FileMode.Create));iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(resourceStream), System.Drawing.Imaging.ImageFormat.Png);
    img.SetAbsolutePosition(125, 350); // set the position in the document where you want the watermark to appear.
    img.ScalePercent(35f);// not neccessory, use if you want to adjust image
    img.ScaleToFit(140f, 100f); // not neccessory, use if you want to adjust image 
    PdfContentByte waterMark;
    
    for (int page = 1; page <= pdfReader.NumberOfPages; page++) // for loop will add image to each page. Based on the condition you can add image to single page also 
    {
            waterMark = stamp.GetOverContent(page);
        waterMark.AddImage(img);
    }
    
    stamp.FormFlattening = true;
    stamp.Close();// closing the pdfStamper, the order of closing must be important
    pdfReader.Close();
    
    File.Delete(FileLocation);
    File.Move(FileLocation.Replace(".pdf", "(tempFile).pdf"), FileLocation);
    

    【讨论】:

      猜你喜欢
      • 2016-02-12
      • 1970-01-01
      • 2015-06-23
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 2013-02-10
      • 1970-01-01
      相关资源
      最近更新 更多