- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我构建了一种方法,试图查看给定 pdf 中所有嵌入图像的分辨率是否至少为 300 PPI(适合打印)。它所做的是循环浏览页面上的每个图像,并将其宽度和高度与艺术框的宽度和高度进行比较。如果每页只有一张图片,它会成功运行,但当有多张图片时,艺术框大小会包含所有图片,从而导致数字丢失。
我希望有人可能知道如何获取绘制图像的矩形大小,以便我可以正确比较,或者是否有更简单的方法来获取图像对象的 PPI(因为它会是呈现在其矩形中,而不是原始形式)。
这是上述方法的代码
private static bool AreImages300PPI(PdfDictionary pg)
{
var res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
var xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
if (xobj == null) return true;
foreach (PdfName name in xobj.Keys)
{
PdfObject obj = xobj.Get(name);
if (!obj.IsIndirect()) continue;
var tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
var type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
var width = float.Parse(tg.Get(PdfName.WIDTH).ToString());
var height = float.Parse(tg.Get(PdfName.HEIGHT).ToString());
var artbox = (PdfArray) pg.Get(PdfName.ARTBOX);
var pdfRect = new PdfRectangle(float.Parse(artbox[0].ToString()), float.Parse(artbox[1].ToString()),
float.Parse(artbox[2].ToString()), float.Parse(artbox[3].ToString()));
if (PdfName.IMAGE.Equals(type) && (width < pdfRect.Width*300/72 || height < pdfRect.Height*300/72)
|| ((PdfName.FORM.Equals(type) || PdfName.GROUP.Equals(type)) && !AreImages300PPI(tg)))
{
return false;
}
}
return true;
}
作为引用,这里是调用它的方法:
internal static List<string> GetLowResWarnings(string MergedPDFPath)
{
var returnlist = new List<string>();
using (PdfReader pdf = new PdfReader(MergedPDFPath))
{
for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
{
var pg = pdf.GetPageN(pageNumber);
if (!AreImages300PPI(pg))
returnlist.Add(pageNumber.ToString());
}
}
return returnlist;
}
感谢您提供的任何帮助。
最佳答案
我可以给你一条完全不同的道路吗?您正在查看全局文件中的图像,但看不到它们在页面中的使用方式。
iTextSharp 有一个名为 iTextSharp.text.pdf.parser.PdfReaderContentParser
的类它可以运行 PdfReader
并告诉您有关它的事情。您可以通过实现 iTextSharp.text.pdf.parser.IRenderListener
来订阅信息界面。对于它遇到的每个图像,您的类的 RenderImage
方法将被调用 iTextSharp.text.pdf.parser.ImageRenderInfo
目的。从这个对象中,您可以获得实际图像以及当前变换矩阵,后者将告诉您图像是如何放置到文档中的。
使用此信息,您可以创建这样的类:
public class MyImageRenderListener : iTextSharp.text.pdf.parser.IRenderListener {
//For each page keep a list of various image info
public Dictionary<int, List<ImageScaleInfo>> Pages = new Dictionary<int, List<ImageScaleInfo>>();
//Need to manually change the page when using this
public int CurrentPage { get; set; }
//Pass through the current page units
public Single CurrentPageUnits { get; set; }
//Not used, just interface contracts
public void BeginTextBlock() { }
public void EndTextBlock() { }
public void RenderText(iTextSharp.text.pdf.parser.TextRenderInfo renderInfo) { }
//Called for each image
public void RenderImage(iTextSharp.text.pdf.parser.ImageRenderInfo renderInfo) {
//Get the basic image info
var img = renderInfo.GetImage().GetDrawingImage();
var imgWidth = img.Width;
var imgHeight = img.Height;
img.Dispose();
//Get the current transformation matrix
var ctm = renderInfo.GetImageCTM();
var ctmWidth = ctm[iTextSharp.text.pdf.parser.Matrix.I11];
var ctmHeight = ctm[iTextSharp.text.pdf.parser.Matrix.I22];
//Create new key for our page number if it doesn't exist already
if (!this.Pages.ContainsKey(CurrentPage)) {
this.Pages.Add(CurrentPage, new List<ImageScaleInfo>());
}
//Add our image info to this page
this.Pages[CurrentPage].Add(new ImageScaleInfo(imgWidth, imgHeight, ctmWidth, ctmHeight, this.CurrentPageUnits));
}
}
它使用这个辅助类来存储我们的信息:
public class ImageScaleInfo {
//The page's unit space, almost always 72
public Single PageUnits { get; set; }
//The image's actual dimensions
public System.Drawing.SizeF ImgSize { get; set; }
//How the image is placed into the page
public System.Drawing.SizeF CtmSize { get; set; }
//Automatically calculate how the image is scaled
public Single ImgWidthScale { get { return ImgSize.Width / CtmSize.Width; } }
public Single ImgHeightScale { get { return ImgSize.Height / CtmSize.Height; } }
//Helper constructor
public ImageScaleInfo(Single imgWidth, Single imgHeight, Single ctmWidth, Single ctmHeight, Single pageUnits) {
this.ImgSize = new System.Drawing.SizeF(imgWidth, imgHeight);
this.CtmSize = new System.Drawing.SizeF(ctmWidth, ctmHeight);
this.PageUnits = pageUnits;
}
}
那么使用它真的很简单:
//Create an instance of our helper class
var imgList = new MyImageRenderListener();
//Parse the PDF and inspect each image
using (var reader = new PdfReader(testFile)) {
var proc = new iTextSharp.text.pdf.parser.PdfReaderContentParser(reader);
for (var i = 1; i <= reader.NumberOfPages; i++) {
//Get the page object itself
var p = reader.GetPageN(i);
//Get the page units. Per spec, page units are expressed as multiples of 1/72 of an inch with a default of 72.
var pageUnits = (p.Contains(PdfName.USERUNIT) ? p.GetAsNumber(PdfName.USERUNIT).FloatValue : 72);
//Set the page number so we can find it later
imgList.CurrentPage = i;
imgList.CurrentPageUnits = pageUnits;
//Process the page
proc.ProcessContent(i, imgList);
}
}
//Dump out some information
foreach (var p in imgList.Pages) {
foreach (var i in p.Value) {
Console.WriteLine(String.Format("Image PPI is {0}x{1}", i.ImgWidthScale * i.PageUnits, i.ImgHeightScale * i.PageUnits));
}
}
编辑
根据@BrunoLowagie 下面的评论,我更新了上面的内容以删除“magic 72”并实际尝试查询文档以查看它是否已被覆盖。不太可能发生,但一两年后有人会发现一些晦涩难懂的 PDF 并提示这段代码不能正常工作所以安全总比抱歉好。
关于c# - 如何使用 ITextSharp 获取 PDF 中嵌入图像的分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23815950/
当段落长度对于 ColumnText 的宽度来说太长时,如何减少换行符的高度? 我已经尝试了以下方法,因为我看到了其他回答这个问题的问题: p.Leading = 0 但这并没有产生任何影响。 我还尝
是否可以使用 iTextSharp 将句子中的单个单词加粗?我正在处理来自 xml 的大段文本,并且我试图将几个单独的单词加粗,而不必将字符串分成单独的短语。 例如: document.Add(new
不间断空格如何用于在 PdfPTable 单元格中包含多行内容。 iTextSharp 正在用空格字符分解单词。 场景是我想要在表头中显示多行内容,例如在第一行可能显示“Text1 &”,在第二行显示
我正在从 iTextSharp 创建 PDF 以供打印。我有可变长度的文本,我希望始终以最大字体大小填充固定高度的表格单元格,而不会换行。如何做到这一点? 最佳答案 首先,您需要能够测量所选字体的文本
我想使用 iTextSharp 从 pdf 文件中检索文本。但是,我无法像在 itextsharp(itext) 的 JAVA 库中那样使用 PDFTextExtractor。我需要 readPDFO
我们想在发送之前在我们的 pdf 顶部添加一个带有用户电子邮件和名称的水印。我已经编写了执行此操作的代码,并且运行良好。我想检查这是否是最好的方法。我们希望在 pdf 的顶部将水印分成两行。 ,我使用
有没有办法使用 iTextSharp 更改 PDF 中第二页的页边距? 我现在有: Document document = new Document(PageSize.A4, 144f, 72f, 1
这其实是引用Question实际上已关闭 我正在使用 ItextSharp 5.2.1。 我想使用 PdfContentByte 使我的标题文本带有下划线。请为我提供解决方案。 最佳答案 privat
我正在使用来自 nuGet (5.5.8) 的最新 iTextSharp 库来解析 pdf 文件中的一些文本。我面临的问题是 GetTextFromPage 方法不仅从它应该返回的页面中返回文本,它还
如何在保持 itextsharp 旋转的同时缩放 pdf 页面? 我有以下内容,但我失去了轮换: public static void ScaleToLetter(string inPDF,
我必须在 pdf 中插入图像。也就是说,无论我在哪里看到文本“签名”,我都必须在那里插入签名图像。我可以通过说 absolute positions 来做到。但是,我正在寻找如何在 pdf 中找到“签
我希望使用 itextSharp 将 html 转换为 pdf。 我希望在我的 pdf 中有一个特定的样式。 我希望所有 pdf 文件都遵循特定的 CSS 类。但我不知道我必须添加那个编译器 khno
我在 ASP.NET 代码中使用 iTextSharp DLL。我正在将数据提取到数据集中并将数据集添加到 PDF 表中。 如果我的数据集有更多 100 行,那么 100 行将添加到 PDF 表中,并
如何使用 iIextSharp 为 PDF 文档设置默认字体和字体大小,以便在整个 PDF 中使用它。 最佳答案 遇到与俄语和罗马尼亚字母相同的问题(itextsharp 5.5.6.0,.net 3
我使用 PdfContentByte 在 pdf 中显示文本,因为我现在也使用 SetTextMatrix mathod 来放置该文本,当我的文本很大时它不会显示在 pdf 中显示我可以包装文本显示我
我现在正在使用 iTextSharp (5.4.5) 几个星期。这周,我在文档中的元素顺序方面遇到了一些奇怪的事情。 我正在处理包含主题和图像(图表)的 pdf 报告。 文档的格式是这样的: 自然保护
我尝试了几种方法来做到这一点,但仍然无法做到。看来 iTextSharp 需要 2 次通过情况,以便图像出现在文本顶部。所以我尝试使用内存流来执行此操作,但我不断收到错误。 Public Fu
我在 iText/iTextSharp(iTextSharp 5.3.3 通过 NuGet)中遇到了一个非常奇怪的 XFA 表单问题。我正在尝试填写静态 XFA 样式的表单,但我的更改没有生效。 我有
当我使用 itextsharp 提取文本时,我将获得文本的 x 和 y 坐标。如果我根据 xy 位置将文本从 pdf 转换为 html,则通过使用这 2 个坐标,文本位置 chnages 。得到我使用
有人可以提供示例或链接到使用 itextsharp 5.4.4 签署现有 pdf 的示例吗?理想情况下保持 pdf/pdf 的一致性?谢谢。 编辑:我理解这个问题看起来好像我没有使用谷歌等。但是,新版
我是一名优秀的程序员,十分优秀!