Tag: itextsharp

使用itextsharp从PDF中提取图像

我试图使用itextsharp从pdf中提取所有图像,但似乎无法克服这一个障碍。 System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS);行出现错误System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS); 给出“参数无效”错误。 我认为它适用于图像是位图而不是任何其他格式的图像。 我有以下代码 – 抱歉长度; private void Form1_Load(object sender, EventArgs e) { FileStream fs = File.OpenRead(@”reader.pdf”); byte[] data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); List ImgList = new List(); iTextSharp.text.pdf.RandomAccessFileOrArray RAFObj = null; iTextSharp.text.pdf.PdfReader PDFReaderObj = null; iTextSharp.text.pdf.PdfObject PDFObj = null; iTextSharp.text.pdf.PdfStream PDFStremObj = null; try { […]

如何使用iTextSharp从PDF中正确提取下标/上标?

iTextSharp很好地从PDF文档中提取纯文本,但是我遇到了下标/上标文本的问题,这在技术文档中很常见。 TextChunk.SameLine()需要两个块具有相同的垂直定位才能“在”同一行上,而上标或下标文本不是这种情况。 例如,在本文档的第11页上,在“燃烧效率”下: http://www.mass.gov/courts/docs/lawlib/300-399cmr/310cmr7.pdf 预期案文: monoxide (CO) in flue gas in accordance with the following formula: CE = [CO2 /(CO + CO2)] 结果文字: monoxide (CO) in flue gas in accordance with the following formula: CE = [CO /(CO + CO )] 2 2 我将SameLine()移动到LocationTextExtractionStrategy并为它读取的私有TextChunk属性创建了公共getter。 这允许我在我自己的子类中动态调整容差,如下所示: public class SubSuperStrategy : LocationTextExtractionStrategy { public int SameLineOrientationTolerance { […]

使用itextsharp突出显示pdf中的单词,而不是在浏览器中显示突出显示的单词

使用itextsharp在浏览器中不显示突出显示的单词。 土砖 浏览器 码 List MatchesFound = strategy.GetTextLocations(splitText[i].Trim(), StringComparison.CurrentCultureIgnoreCase); foreach (Rectangle rect in MatchesFound) { float[] quad = { rect.Left – 3.0f, rect.Bottom, rect.Right, rect.Bottom, rect.Left – 3.0f, rect.Top + 1.0f, rect.Right, rect.Top + 1.0f }; //Create our hightlight PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad); //Set the color highlight.Color = BaseColor.YELLOW; //Add the […]

如何减少许多单元格的PdfPTable的内存消耗

我正在使用ITextSharp创建一个PDF,它由一个PdfTable组成。 不幸的是,对于特定的数据集,由于创建了大量的PdfPCell,我得到了Out of memory Exception(我已经分析了内存使用情况 – 我已经有近1/2万个单元!) 在这种情况下有没有办法减少内存使用量? 我试过在各个点(每行之后)和完全压缩后进行刷新 PdfWriter基于FileStream 代码看起来非常像这样: Document document = Document(); FileStream stream = new FileStream(fileName,FileMode.Create); pdfWriter = PdfWriter.GetInstance(document, stream); document.Open(); PdfPTable table = new PdfPTable(nbColumnToDisplay); foreach (GridViewRow row in gridView.Rows) { j = 0; for (int i = 0; i < gridView.HeaderRow.Cells.Count; i++) { PdfPCell cell = new PdfPCell( new […]

HTML to List使用XMLWorker

有人可以提供一个使用iTextSharp(C#)中的XMLWorkerHelper将HTML解析为元素列表的示例。 文档中给出的JAVA版本是: XMLWorkerHelper.getInstance().parseXHtml(new ElementHandler() { public void add(final Writable w) { if (w instanceof WritableElement) { List elements = ((WritableElement)w).elements(); // write class names of elements to file } } }, HTMLParsingToList.class.getResourceAsStream(“/html/walden.html”));

使用iTextSharp创建PDF时输入页码

我正在使用ASP MVC,我使用iTextSharp在我的应用程序中生成PDF。 但现在我有一个问题:我打印列表,当存在多个页面时,我想显示页码(例如: Page 1 to 4 )。 我找到了一些例子,但我认为它比我需要做的更复杂(比如exameple )。 编辑:我发现这个例子2 。 我可以计算页数,但我不能打印页数。 我做了什么: public ActionResult downloadListaISCC(DateTime? DataFimFiltro) { //Code to generate list to PDF //My document Document doc1 = new Document(); doc1.SetPageSize(iTextSharp.text.PageSize.A4); doc1.SetMargins(0f, 0f, 0f, 0f); doc1.NewPage(); MemoryStream pdfStream = new MemoryStream(); PdfWriter pdfWriter = PdfWriter.GetInstance(doc1, pdfStream); //Code to create table doc1.Add(table); //table list […]

使用iTextsharp为PDF添加页眉和页脚

如何在pdf中为每个页面添加页眉和页脚。 Headed将只包含一个文本页脚将包含pdf的文本和分页(页:1/4) 这怎么可能 ? 我尝试添加以下行,但标题不会显示在pdf中。 document.AddHeader(“Header”, “Header Text”); 这是我用于生成PDF的代码: protected void GeneratePDF_Click(object sender, EventArgs e) { DataTable dt = getData(); Response.ContentType = “application/pdf”; Response.AddHeader(“content-disposition”, “attachment;filename=Locations.pdf”); Response.Cache.SetCacheability(HttpCacheability.NoCache); Document document = new Document(); PdfWriter.GetInstance(document, Response.OutputStream); document.Open(); iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.COURIER , 8); PdfPTable table = new PdfPTable(dt.Columns.Count); PdfPRow row = null; float[] widths = new float[] { […]

iTextSharp – 如何在页面上获取单词的位置

我正在使用iTextSharp和reader.GetPageContent方法从PDF中提取文本。 我需要找到文档中找到的每个单词的矩形/位置。 有没有办法使用iTextSharp获取PDF中单词的矩形/位置?

Itextsharp:在一页上调整2个元素

所以,我在使用C#(.NET 4.0 + WinForms)和iTextSharp 5.1.2时遇到了这个问题。 我有一些扫描图像存储在数据库中,需要与这些图像一起构建。 有些文件只有一页,其他有几百页。 这工作正常使用: foreach (var page in pages) { Image pageImage = Image.GetInstance(page.Image); pageImage.ScaleToFit(document.PageSize.Width,document.PageSize.Height); pageImage.Alignment = Image.ALIGN_TOP | Image.ALIGN_CENTER; document.Add(pageImage); document.NewPage(); //… } 问题是: 我需要在最后一页的底部添加一个小表。 我试试: foreach (var page in pages) { Image pageImage = Image.GetInstance(page.Image); pageImage.ScaleToFit(document.PageSize.Width,document.PageSize.Height); pageImage.Alignment = Image.ALIGN_TOP | Image.ALIGN_CENTER; document.Add(pageImage); document.NewPage(); //… } Table t = new […]

如何在合并时删除空格

我有一些代码需要3个不同的PDF字节数组并合并它们。 这段代码效果很好。 问题(有些人)认为每个PDF都被认为是一个完整的页面(如果打印),即使它上面只有4英寸的内容,因此垂直留下7英寸的空白区域。 然后将中间文档放入其中,并且可以在其末尾处具有或不具有垂直白色空间。 然后页脚也会放在自己的页面上。 这是代码: byte[] Bytes = rv.LocalReport.Render(“PDF”, null, out MimeType, out Encoding, out Extension, out StreamIDs, out Warnings); List MergeSets = // This is filled prior to this code // Append any other pages to this primary letter if (MergeSets.Count > 0) { MemoryStream ms = new MemoryStream(); Document document = new […]