如何使用iTextSharp限制PDF页面上段落的宽度? [英] How can I restrict the width of a paragraph on a PDF page using iTextSharp?

查看:191
本文介绍了如何使用iTextSharp限制PDF页面上段落的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to insert a paragraph (which will word-wrap over several lines) into a PDF document using iTextSharp, but I want to restrict the width of the paragraph to the left half of the page. I see no "width" property for the Paragraph class, but surely there is a way to do this, stimmt?

假定的答案对我不起作用,因为它使用了iTextSharp(C#)中显然不可用的iText(Java)东西.具体来说(可能会有更多):

The supposed answer doesn't work for me because it uses iText (Java) stuff apparently unavailable in iTextSharp (C#). Specifically (to begin with, there might be more):

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

尽管* Sharp(大写的首字母s)有一个"SetSimpleColumn",但没有"GetPageSize".

Although there is a "SetSimpleColumn" for *Sharp (uppercased initial 's'), there is no "GetPageSize".

我开始认为我真正需要做的就是按照建议创建一个无边界表",如"BestiTextQuestionsOnStackOverflowFull.pdf"所述.

I'm beginning to think that what I really need to do may be to create a 'borderless table' as suggested, and as articulated in "BestiTextQuestionsOnStackOverflowFull.pdf"

推荐答案

这是一种解决方法-无边距的单行表,其WidthPercentage设置为50,水平对齐方式发送到Left:

This is one way to do it - a borderless, single-row table with WidthPercentage set to 50 and Horizontal Alignment sent to Left:

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));
    }
}

这篇关于如何使用iTextSharp限制PDF页面上段落的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆