在pdf中的每个图像的正下方或上方添加文本

编程入门 行业动态 更新时间:2024-10-23 23:23:31
本文介绍了在pdf中的每个图像的正下方或上方添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 private static void InsertTextToPdf(string sourceFileName, string newFileName) { using (Stream pdfStream = new FileStream(sourceFileName, FileMode.Open)) using (Stream newpdfStream = new FileStream(newFileName, FileMode.Create, FileAccess.ReadWrite)) { PdfReader pdfReader = new PdfReader(pdfStream); PdfStamper pdfStamper = new PdfStamper(pdfReader, newpdfStream); PdfContentByte pdfContentByte = pdfStamper.GetOverContent(1); BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); pdfContentByte.SetColorFill(BaseColor.BLUE); pdfContentByte.SetFontAndSize(baseFont, 8); pdfContentByte.BeginText(); pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Kevin Cheng - A Hong Kong actor", 400, 600, 0); pdfContentByte.EndText(); pdfStamper.Close(); } }

此代码可以在PDF中添加文本,但我想在PDF的每个图像的下面添加相同的文本。

This code is working fine to add text in a PDF , but i want to add the same text on below of every image in PDF.

我尝试将图像提取的代码与文本添加合并,但不能多次看到相同的文本,同时能够看到多个图像提取。我在提取图像的同一循环中添加了文本。

I tried with merging the code of image extraction with text add but not able to see the same text multiple time , while able to see multiple image extraction. I added the text in same loop where extracting the image .

public static Dictionary<string, System.Drawing.Image> ExtractImages(string filename, string sourceFileName, string newFileName) { var images = new Dictionary<string, System.Drawing.Image>(); Stream newpdfStream = new FileStream(newFileName, FileMode.Create, FileAccess.ReadWrite); using (var reader = new PdfReader(filename)) { // PdfReader pdfReader = new PdfReader(reader); PdfStamper pdfStamper = new PdfStamper(pdfReader, newpdfStream); PdfContentByte pdfContentByte = pdfStamper.GetOverContent(1); var parser = new PdfReaderContentParser(reader); ImageRenderListener listener = null; for (var i = 1; i <= reader.NumberOfPages; i++) { parser.ProcessContent(i, (listener = new ImageRenderListener())); var index = 1; if (listener.Images.Count > 0) { foreach (var pair in listener.Images) { images.Add(string.Format("{0}_Page_{1}_Image_{2}{3}", System.IO.Path.GetFileNameWithoutExtension(filename), i.ToString("D4"), index.ToString("D4"), pair.Value), pair.Key); index++; { BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); pdfContentByte.SetColorFill(BaseColor.BLUE); pdfContentByte.SetFontAndSize(baseFont, 8); pdfContentByte.BeginText(); pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Kevin Cheng - A Hong Kong actor", 400 + index * 10, 600, 0); pdfContentByte.EndText(); } } } } pdfStamper.Close(); return images; } }

推荐答案

IRenderListener implementation ImageRenderListener 您尝试使用收集字典< System.Drawing中的图像。图像,字符串> 将图像映射到与其原始类型匹配的文件扩展名。特别是它完全忽略了图像坐标,因此不能用于在pdf 中的每个图像的下方或上方添加文本。

The IRenderListener implementation ImageRenderListener you try to use collects the images in a Dictionary<System.Drawing.Image, string> mapping images to the file extension matching their original type. In particular it completely ignores the image coordinates and, therefore, is not usable for your task to add text just below or above every image in pdf.

相反,你需要一个 IRenderListener 实现,它计算图像顶部位置的坐标;你实际上可以更进一步,甚至可以在该听众中添加文本,例如这个实现:

Instead you need an IRenderListener implementation which calculates the coordinates of the position atop an image; you can actually go a step further and even do the text adding in that listener, e.g. this implementation:

public class ImageEntitlingRenderListener : IRenderListener { BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); PdfStamper pdfStamper = null; int page = 0; public ImageEntitlingRenderListener(PdfStamper pdfStamper, int page) { this.pdfStamper = pdfStamper; this.page = page; } public void RenderImage(ImageRenderInfo renderInfo) { Matrix ctm = renderInfo.GetImageCTM(); float xCenter = ctm[Matrix.I31] + 0.5F * ctm[Matrix.I11]; float yTop = ctm[Matrix.I32] + ctm[Matrix.I22]; PdfContentByte pdfContentByte = pdfStamper.GetOverContent(page); pdfContentByte.SetColorFill(BaseColor.BLUE); pdfContentByte.SetFontAndSize(baseFont, 8); pdfContentByte.BeginText(); pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "rahul", xCenter, yTop, 0); pdfContentByte.EndText(); } public void BeginTextBlock() { } public void EndTextBlock() { } public void RenderText(TextRenderInfo renderInfo) { } }

此渲染侦听器在PDF中的每个位图图像上添加rahul。

This render listener adds "rahul" atop each bitmap image in your PDF.

注意:我通过引入一些假设来保持代码简单。特别是我假设PDF中的图像是直立呈现的,并且不涉及页面旋转。通用解决方案必须考虑整个 ctm 以及计算 xCenter 时的页面轮换, yTop 和文本轮换(此处始终 0 )。

Beware: I kept the code simple by introducing some assumptions. In particular I assumed the images in the PDF to be rendered upright and no page rotation to be involved. A generic solution would have to consider the whole ctm and the page rotation when calculating xCenter, yTop, and the text rotation (here always 0).

您可以像这样使用该渲染侦听器:

You can use that render listener like this:

private static void InsertTextToPdf(string sourceFileName, string newFileName) { using (Stream pdfStream = new FileStream(sourceFileName, FileMode.Open)) using (Stream newpdfStream = new FileStream(newFileName, FileMode.Create, FileAccess.ReadWrite)) { PdfReader pdfReader = new PdfReader(pdfStream); PdfStamper pdfStamper = new PdfStamper(pdfReader, newpdfStream); var parser = new PdfReaderContentParser(pdfReader); for (var i = 1; i <= pdfReader.NumberOfPages; i++) { parser.ProcessContent(i, (new ImageEntitlingRenderListener(pdfStamper, i))); } pdfStamper.Close(); pdfReader.Close(); } }

更多推荐

在pdf中的每个图像的正下方或上方添加文本

本文发布于:2023-06-07 21:56:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   文本   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!