直接在pdf中的每张图片下方或上方添加文字privatestaticvoidInsertTextToPdf(stringsourceFileName,stringnewFileName){using(StreampdfStream=newFileStream(sourceFileName,FileMode.Open))使用(StreamnewpdfStream=newFileStream(newFileName,FileMode.Create,FileAccess.ReadWrite)){PdfReaderpdfReader=newPdfReader(pdfStream);PdfStamperpdfStamper=newPdfStamper(pdfReader,newpdfStream);PdfContentBytepdfContentByte=pdfStamper.GetOverContent(1);BaseFontbaseFont=BaseFont.CreateFont(BaseFont.TIMES_ROMAN,BaseFont.CP1250,BaseFont.NOT_EMBEDDED);;pdfContentByte.BeginText();pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER,"郑嘉颖-香港演员",400,600,0);pdfContentByte.EndText();pdfStamper.Close();这段代码可以很好地在PDF中添加文本,但我想在PDF中的每个图像下方添加相同的文本。我尝试将用于图像提取的代码与文本添加合并,但是在能够看到多个图像提取的同时无法多次看到相同的文本。我在获取图像的同一个循环中添加了文本。publicstaticDictionaryExtractImages(stringfilename,stringsourceFileName,stringnewFileName){varimages=newDictionary();StreamnewpdfStream=newFileStream(newFileName,FileMode.Create,FileAccess.ReadWrite);使用(varreader=newPdfReader(filename)){//PdfReaderpdfReader=newPdfReader(reader);PdfStamperpdfStamper=newPdfStamper(pdfReader,newpdfStream);PdfContentBytepdfContentByte=pdfStamper.GetOverContent(1);varparser=newPdfReaderContentParser(阅读器);ImageRenderListener监听器=null;for(vari=1;i0){foreach(varpairinlistener.Images){images.Add(string.Format("{0}_Page_{1}_Image_{2}{3}",System.IO.Path.GetFileNameWithoutExtension(文件名),i.ToString("D4"),index.ToString("D4"),pair.Value),pair.Key);索引++;{BaseFontbaseFont=BaseFont.CreateFont(BaseFont.TIMES_ROMAN,BaseFont.CP1250,BaseFont.NOT_EMBEDDED);pdfContentByte.SetColorFill(BaseColor.BLUE);pdfContentByte.SetFontAndSize(bas易字体,8);pdfContentByte.BeginText();pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER,"郑嘉颖-香港演员",400+index*10,600,0);pdfContentByte.EndText();}}}}pdfStamper.Close();返回图像;您尝试使用的IRenderListener实现ImageRenderListener收集图像字典字典将图像映射到与其原始类型匹配的文件扩展名特别是它完全忽略图像坐标,因此不能在pdf中使用在每个图像下方或上方添加文本。相反,您需要一个IRenderListener实现来计算图像顶部的位置坐标;您实际上可以更进一步,甚至可以在该侦听器中添加文本,例如此实现:PdfStamperpdfStamper=null;整页=0;publicImageEntitlingRenderListener(PdfStamperpdfStamper,intpage){this.pdfStamper=pdfStamper;这个。页面=页面;){矩阵ctm=renderInfo.GetImageCTM();floatxCenter=ctm[Matrix.I31]+0.5F*ctm[Matrix.I11];floatyTop=ctm[Matrix.I32]+ctm[Matrix.I22];PdfContentBytepdfContentByte=pdfStamper.GetOverContent(页面);pdfContentByte.SetColorFill(BaseColor.BLUE);pdfContentByte.SetFontAndSize(baseFont,8);pdfContentByte.BeginText();pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER,"rahul",xCenter,0)yTop;pdfContentByte.EndText();}publicvoidBeginTextBlock(){}publicvoidEndTextBlock(){}publicvoidRenderText(TextRenderInforenderInfo){}}此渲染侦听器在PDF中的每个位图图像上添加“rahul”注意:我通过引入一些假设来保持代码简单。特别是,我假设PDF中的图像是竖直呈现的,并且不涉及页面旋转。通用解决方案在计算xCenter、yTop和文本旋转时必须考虑整个ctm和页面旋转(这里始终为0)。可以这样使用渲染监听器:以上是C#学习教程:在pdf中每张图片的正下方或上方添加文字如果对大家有用,需要了解更多C#学习教程,希望大家分享完整内容将更加关注它——privatestaticvoidInsertTextToPdf(stringsourceFileName,stringnewFileName){using(StreampdfStream=newFileStream(sourceFileName,FileMode.Open))using(StreamnewpdfStream=newFileStream(newFileName,FileMode.Create,FileAccess.ReadWrite)){PdfReaderpdfReader=newPdfReader(pdfStream);PdfStamperpdfStamper=newPdfStamper(pdfReader,newpdfStream);varparser=newPdfReaderContentParser(pdfReader);for(vari=1;i本文收集自网络,不代表立场,如涉及侵权,请点右联系管理员删除。如需转载请注明出处:
