【问题标题】:How can I restrict the width of a paragraph on a PDF page using iTextSharp?如何使用 iTextSharp 限制 PDF 页面上段落的宽度?
【发布时间】:2015-06-14 01:27:03
【问题描述】:

我想使用 iTextSharp 在 PDF 文档中插入一个段落(将自动换行多行),但我想将段落的宽度限制在页面的左半部分。我看不到 Paragraph 类的“宽度”属性,但肯定有办法做到这一点,stimmt?

更新

假定的答案对我不起作用,因为它使用了 iTextSharp (C#) 中显然不可用的 iText (Java) 内容。具体来说(首先,可能还有更多):

ct.setSimpleColumn(myText, 60, 750, document.getPageSize().getWidth()

虽然*Sharp(大写首字母's')有一个“SetSimpleColumn”,但没有“GetPageSize”。

更新 2

我开始认为,我真正需要做的可能是按照“BestiTextQuestionsOnStackOverflowFull.pdf”中的建议创建一个“无边界表格”

【问题讨论】:

  • 最简单的方法可能是使用没有边框的表格。您需要调整单元格填充和其他一些东西,但这将允许您“Document.Add()”它。如果这对您不起作用,那么正如@OneFineDay 所说,您需要创建自己的矩形来控制它并使用ColumnText
  • 请阅读the documentation,例如The Best iText Questions on StackOverflow,您会发现您最近发布的许多问题都是重复的。例如:这个问题可能与How to fit a String inside a rectangle? 重复。它也可以像定义自定义左边距一样简单。
  • 有趣;我下载了“StackOverflow 上的最佳 iText 问题”示例;完整版有多大?
  • 关于您对问题的更新:iText 中的 getter/setter 主要是 iTextSharp 中的属性。 document.getPageSize().getWidth() 变为 document.PageSize.Width

标签: c# pdf pdf-generation itextsharp


【解决方案1】:

这是一种方法 - 一个无边框的单行表格,其中 WidthPercentage 设置为 50,水平对齐发送到左侧:

using (var ms = new MemoryStream())
{
    using (var doc = new Document(PageSize.A4, 50, 50, 25, 25))                     {
        //Create a writer that's bound to our PDF abstraction and our stream
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {

            //Open the document for writing
            doc.Open();

            var courier9RedFont = FontFactory.GetFont("Courier", 9, BaseColor.RED);
            var importantNotice = new Paragraph("Sit on a potato pan Otis - if you don't agree that that's the best palindrome ever, I will sic Paladin on you, or at least say, 'All down but nine - set 'em up on the other alley, pard'", courier9RedFont);
            importantNotice.Leading = 0;
            importantNotice.MultipliedLeading = 0.9F; // reduce the width between lines in the paragraph with these two settings

            // Add a single-cell, borderless, left-aligned, half-page, table
            PdfPTable table = new PdfPTable(1);
            PdfPCell cellImportantNote = new PdfPCell(importantNotice);
            cellImportantNote.BorderWidth = PdfPCell.NO_BORDER;
            table.WidthPercentage = 50;
            table.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(cellImportantNote);
            doc.Add(table);

            doc.Close();
        }
        var bytes = ms.ToArray();
        String PDFTestOutputFileName = String.Format("iTextSharp_{0}.pdf", DateTime.Now.ToShortTimeString());
        PDFTestOutputFileName = PDFTestOutputFileName.Replace(":", "_");
        var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), PDFTestOutputFileName);
        File.WriteAllBytes(testFile, bytes);
        MessageBox.Show(String.Format("{0} written", PDFTestOutputFileName));
    }
}

【讨论】:

    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 2016-07-29
    • 2012-12-21
    • 2011-02-07
    • 2011-07-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多