ITextSharp 使用

ITextSharp是一个在C#平台下操作pdf的一个工具dll

但是,自从4.1.6版本之后的ITextSharp的license变成了AGPL,所以不能商用。所以说version 4.1.6就是目前为止可以商用的最新版本

所以让我们下载4.1.6版本的ITextSharp,然后把它加载到我们的工程之中来。


1. ITextSharp操作文本域

我们经常遇到这种需求:使用一个pdf模板,填充上数据,最后导出一个填充好数据的pdf

ITextSharp可以很好的解决这个问题,

第一步,先用Adobe Acrobat做好模板(具体步骤请自行google)


第二步,使用ITextSharp来填充模板中的文本域

这里,提供一个我自己写的共同方法

private void MakeEnPdf(string pdfTemplate, string tempFilePath, Dictionary<string, string> parameters)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;

            try
            {
                //When the temp file already exists, delete it.
                if (File.Exists(tempFilePath))
                {
                    File.Delete(tempFilePath);
                }

                pdfReader = new PdfReader(pdfTemplate);
                pdfStamper = new PdfStamper(pdfReader, new FileStream(tempFilePath, FileMode.Create));
                // Get the collection of domain.
                AcroFields pdfFormFields = pdfStamper.AcroFields;
                //pdfFormFields.GenerateAppearances = false;
                pdfStamper.FormFlattening = true;
                // Set value for the required domain.
                foreach (KeyValuePair<string, string> parameter in parameters)
                {
                    if (pdfFormFields.Fields[parameter.Key] != null)
                    {
                        pdfFormFields.SetField(parameter.Key, parameter.Value);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                pdfStamper.Close();
                pdfReader.Close();

                pdfStamper = null;
                pdfReader = null;
            }

        }

其中, pdfTemplate是template的路径,tempFilePath是要生成的pdf的路径,parameters是一个词典,

使用这个共同方法可以参考如下代码

 #region Create temporary pdf file without images

            var tempGuid = Guid.NewGuid().ToString();
            var tempFileName = "Product_Data_Sheet_template_2016_Base_" + tempGuid + ".pdf";
            var tempFilePath = Path.Combine(tempUserFolder, tempFileName);
            MakeEnPdf(pdfTemplate, tempFilePath, parameters);

            #endregion


2. ITextSharp添加图片

这个地方,相信大家都可以搜索到一个很好的网址 http://www.mikesdotnetting.com/article/87/itextsharp-working-with-images

基本的操作这里说的比较完全了,但是有一个方面他没有说到,就是: 在pdf的一个rectangle里面放置图片,这个地方我提供一下我的解决思路


首先要得到rectangle的位置信息及它的大小,提供一个类

public class CanvasContainerRectangle
    {
        public CanvasContainerRectangle(float startX, float startY, float rectWidth, float rectHeight)
        {
            this.StartX = startX;
            this.StartY = startY;
            this.RectWidth = rectWidth;
            this.RectHeight = rectHeight;
        }

        public float StartX { get; set; }
        public float StartY { get; set; }

        public float RectWidth { get; set; }
        public float RectHeight { get; set; }

    }

我们在把图片放置在container里面的时候,需要把图片进行缩放,我们需要得到这个缩放的百分比

private float GetPercentage(float width, float height, CanvasContainerRectangle containerRect)
        {
            float percentage = 0;

            if(height > width)
            {
                percentage = containerRect.RectHeight / height;

                if(width * percentage > containerRect.RectWidth)
                {
                    percentage = containerRect.RectWidth / width;
                }
            }
            else
            {
                percentage = containerRect.RectWidth / width;

                if(height * percentage > containerRect.RectHeight)
                {
                    percentage = containerRect.RectHeight / height;
                }
            }

            return percentage;
        }


最后调用方法PutImages把图片加载到pdf之中

public class PdfImage
    {
        public PdfImage(string imageUrl, float fitWidth, float fitHeight, float absolutX, float absoluteY, bool scaleParent)
        {
            this.ImageUrl = imageUrl;
            this.FitWidth = fitWidth;
            this.FitHeight = fitHeight;
            this.AbsoluteX = absolutX;
            this.AbsoluteY = absoluteY;
            this.ScaleParent = scaleParent;
        }

        public PdfImage(string imageUrl, float fitWidth, float fitHeight, float absolutX, float absoluteY, bool scaleParent, byte[] imgBytes)
        {
            this.ImageUrl = imageUrl;
            this.FitWidth = fitWidth;
            this.FitHeight = fitHeight;
            this.AbsoluteX = absolutX;
            this.AbsoluteY = absoluteY;
            this.ScaleParent = scaleParent;
            this.ImgBytes = imgBytes;
        }

        public string ImageUrl { get; set; }
        public float FitWidth { get; set; }
        public float FitHeight { get; set; }
        public float AbsoluteX { get; set; }

        public float AbsoluteY { get; set; }

        public byte[] ImgBytes { get; set; }

        public bool ScaleParent { get; set; }

        public CanvasContainerRectangle ContainerRect { get; set; }

    }
 /// <summary>
        /// Create a pdf with images putting
        /// </summary>
        /// <param name="tempFilePath">The source pdf file</param>
        /// <param name="createdPdfPath">The destination pdf file which will be created</param>
        /// <param name="pdfImages">Images list</param>
        private void PutImages(string tempFilePath, string createdPdfPath, List<PdfImage> pdfImages)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;

            try
            {
                pdfReader = new PdfReader(tempFilePath);
                pdfStamper = new PdfStamper(pdfReader, new FileStream(createdPdfPath, FileMode.Create));

                var pdfContentByte = pdfStamper.GetOverContent(1);

                foreach(var pdfImage in pdfImages)
                {
                    Uri uri = null;
                    Image img = null;

                    var imageUrl = pdfImage.ImageUrl;

                    //If imageUrl is a relative path, get the absolute path firstly
                    if (!imageUrl.StartsWith("http"))
                    {
                        //var absolutePath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(".."), imageUrl);
                        var absolutePath = System.Web.HttpContext.Current.Server.MapPath("..") + imageUrl;
                        uri = new Uri(absolutePath);
                        img = Image.GetInstance(uri);
                    }
                    else
                    {
                        //Get the image first, and then transfer it to ITextSharp image
                        if (pdfImage.ImgBytes != null)
                        {
                            img = Image.GetInstance(new MemoryStream(pdfImage.ImgBytes));
                        }
                    }

                    if(img != null)
                    {

                        if (pdfImage.ScaleParent)
                        {
                            var containerRect = pdfImage.ContainerRect;

                            float percentage = 0.0f;
                            percentage = GetPercentage(img.Width, img.Height, containerRect);
                            img.ScalePercent(percentage * 100);

                            pdfImage.AbsoluteX = (containerRect.RectWidth - img.Width * percentage) / 2 + containerRect.StartX;
                            pdfImage.AbsoluteY = (containerRect.RectHeight - img.Height * percentage) / 2 + containerRect.StartY;

                        }
                        else
                        {
                            img.ScaleToFit(pdfImage.FitWidth, pdfImage.FitHeight);
                        }

                        img.SetAbsolutePosition(pdfImage.AbsoluteX, pdfImage.AbsoluteY);
                        pdfContentByte.AddImage(img);
                    }
                }
                
                pdfStamper.FormFlattening = true;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pdfStamper != null)
                {
                    pdfStamper.Close();
                }

                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
                
                pdfStamper = null;
                pdfReader = null;
            }
        }
 

我们在使用的时候,首先要把要加载的图片,做成PdfImage格式,然后调用PutImages方法,把这些图片加载到pdf之中

注意,我这里是把图片加载到rectangle的正中间位置,如果要到其他的位置,可以自己改一下PutImages函数








  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iTextSharp 是一个用于生成和处理 PDF 文件的开源库,它是 C# 版本的 iText 库的一个端口。以下是 iTextSharp使用手册: ### 下载和安装 iTextSharp 可以从 NuGet 包管理器中下载,也可以从官方网站下载。 ### 创建 PDF 文件 要使用 iTextSharp 创建 PDF 文件,需要先创建一个 Document 对象,该对象表示最终生成的 PDF 文件。然后可以向该对象添加内容,例如文本、图片、表格等。最后需要将 Document 对象写入到文件或流中。 ```csharp // 创建一个 Document 对象 Document document = new Document(); // 创建一个 PdfWriter 对象,将 Document 对象写入到文件或流中 PdfWriter.GetInstance(document, new FileStream("example.pdf", FileMode.Create)); // 打开 Document 对象 document.Open(); // 添加文本内容 document.Add(new Paragraph("Hello, world!")); // 关闭 Document 对象 document.Close(); ``` ### 添加文本内容 iTextSharp 允许在 PDF 文件中添加各种文本内容,例如段落、标题、列表等。可以使用 Paragraph、Chapter 和 Section 等类来实现。 ```csharp // 创建一个 Paragraph 对象 Paragraph paragraph = new Paragraph("This is a paragraph."); // 添加 Paragraph 对象到 Document 对象中 document.Add(paragraph); ``` ### 添加图片内容 iTextSharp 允许在 PDF 文件中添加图片内容,可以使用 Image 类来实现。 ```csharp // 创建一个 Image 对象 Image image = Image.GetInstance("example.jpg"); // 添加 Image 对象到 Document 对象中 document.Add(image); ``` ### 添加表格内容 iTextSharp 允许在 PDF 文件中添加表格内容,可以使用 PdfPTable 类来实现。 ```csharp // 创建一个 PdfPTable 对象 PdfPTable table = new PdfPTable(3); // 添加表头 table.AddCell("Header 1"); table.AddCell("Header 2"); table.AddCell("Header 3"); // 添加表格内容 table.AddCell("1.1"); table.AddCell("1.2"); table.AddCell("1.3"); table.AddCell("2.1"); table.AddCell("2.2"); table.AddCell("2.3"); // 添加 PdfPTable 对象到 Document 对象中 document.Add(table); ``` ### 设置样式和格式 iTextSharp 允许设置各种样式和格式,例如字体、颜色、边框等。可以使用 Font、Color 和 PdfPCell 等类来实现。 ```csharp // 创建一个 Font 对象 Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD); // 创建一个 Paragraph 对象,并设置样式 Paragraph paragraph = new Paragraph("This is a paragraph.", font); // 创建一个 PdfPCell 对象,并设置样式和内容 PdfPCell cell = new PdfPCell(new Phrase("Cell content", font)); cell.BackgroundColor = new Color(192, 192, 192); cell.BorderWidth = 2; // 添加 PdfPCell 对象到 PdfPTable 对象中 PdfPTable table = new PdfPTable(1); table.AddCell(cell); document.Add(table); ``` ### 更多功能 iTextSharp 还支持许多其他功能,例如水印、加密、书签、页面设置等。可以查看官方文档来了解更多细节。 以上是 iTextSharp 的简单使用手册,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值