c# .NET CORE 使用 ITextSharp 将具有透明度的图像添加到现有 PDF [英] c# .NET CORE adding image with transparency to existing PDF using ITextSharp

查看:61
本文介绍了c# .NET CORE 使用 ITextSharp 将具有透明度的图像添加到现有 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将公司徽标添加到现有 pdf(非水印)的每一页上.

My goal is to add company logo to every page of an existing pdf(not watermark).

由于 pdf 文件和 logo 的细节,我只能将 logo 放在 pdf 内容的顶部(而不是底部)并且 logo 必须支持透明度.

Due to pdf file and logo specifics, I can only place the logo on top of the pdf content(not underneath) and the logo has to support transparency.

另一个限制是我必须使用 .NET Core.

One more limitation is I have to use .NET Core.

发布这个答案,因为我找不到明确的解决方案.欢迎提出建议/更正/改进.

Posting this with an answer, because I could not find a clear solution. Suggestions/corrections/improvements are welcome.

希望有人觉得这有用.

推荐答案

支持 .NET Core 的最新 iTextSharp 库是 iText7 但是我不能合法地使用它;既不让我的代码开源,也不购买许可证对我来说是一个选择.因此我使用旧的第三方库:

The newest iTextSharp library to support .NET Core is iText7 however I cannot use it legitemately; neither making my code open source, nor purchasing the licence is an option for me. Therefore I use old, third party library:

Install-Package iTextSharp.LGPLv2.Core

我正在使用的最新版本,在发这篇文章时是 1.3.2

Latest version, the one I'm using, at the time of this post is 1.3.2

需要以下用途

using System;
using System.Drawing.Imaging;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

要实现pdf中的图像透明度,图像必须以正确的格式打开

To acheve image transparency in pdf, image has to be opened in a correct format

var preImage = System.Drawing.Image.FromFile(imagePath);
var image = Image.GetInstance(preImage, ImageFormat.Png);

添加图片时,不要选择要内嵌的图片也很重要

When adding the image, it is also important to not select the image to be inline

canvas.AddImage(image);//do not put .AddImage(image, true);

这是所有代码

var imagePath = "logo.png";
var pdfPath = "edit_this.pdf";

//load pdf file
var pdfBytes = File.ReadAllBytes(pdfPath);
var oldFile = new PdfReader(pdfBytes);

//load image
var preImage = System.Drawing.Image.FromFile(imagePath);
var image = Image.GetInstance(preImage, ImageFormat.Png);
preImage.Dispose();

//optional: if image is wider than the page, scale down the image to fit the page
var sizeWithRotation = oldFile.GetPageSizeWithRotation(1);
if (image.Width > sizeWithRotation.Width)
    image.ScalePercent(sizeWithRotation.Width / image.Width * 100);

//set image position in top left corner
//in pdf files, cooridinates start in the left bottom corner
image.SetAbsolutePosition(0, sizeWithRotation.Height - image.ScaledHeight);

//in production, I use MemoryStream
//I put FileStream here to test the code in console application
using (var newFileStream = new FileStream("with_logo.pdf", FileMode.Create))
{
    //setup PdfStamper
    var stamper = new PdfStamper(oldFile, newFileStream);

    //iterate through the pages in the original file
    for (var i = 1; i <= oldFile.NumberOfPages; i++)
    {
        //get canvas for current page
        var canvas = stamper.GetOverContent(i);
        //add image with pre-set position and size
        canvas.AddImage(image);
    }

    stamper.Close();
}

此代码适用于本地文件.在我的(现实世界)案例中,我收到 pdf 文件作为 Base64 字符串,从本地存储添加徽标,将其转换回 Base64 字符串并在网页上输出.

This code works with local files. In my (real world) case, I receive pdf files as Base64 string, add a logo from local storage, convert it back to Base64 string and output it on a web-page.

我强制(硬编码)以 PNG 格式打开图像,因为我控制徽标的扩展名.如果需要,您可以动态设置图像格式.

I open the image as PNG forcefully(hardcoded) because I control what extension does the logo have. If necessary you can dynamicaly set the image format.

这篇关于c# .NET CORE 使用 ITextSharp 将具有透明度的图像添加到现有 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆