如何使用iTextSharp将段落放置在特定位置 [英] How to place paragraphs in specific place using iTextSharp

查看:801
本文介绍了如何使用iTextSharp将段落放置在特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文本放在pdf上的特定位置?我做了一些搜索,但没有发现任何不好的东西.我有document.Add(new Paragraph("Date:" + DateTime.Now));,我想将其放置在pdf文件的特定区域上.

How do I place text at a specific location on the pdf? I did a little bit of searching but didn't find anything too good. I have document.Add(new Paragraph("Date:" + DateTime.Now)); and I wanted to place that on a specific area on the pdf file.

我的代码:

   private void savePDF_Click(object sender, EventArgs e)
    {
        FileStream fileStream = new FileStream(nameTxtB.Text + "Repair.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
        Document document = new Document();
        document.Open();
        iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(PageSize.LETTER);
        PdfWriter pdfWriter = PdfWriter.GetInstance(document, fileStream);

        iTextSharp.text.Image r3tsLogo = iTextSharp.text.Image.GetInstance("rt3slogo.PNG"); //creates r3ts logo
        iTextSharp.text.Image r3Info = iTextSharp.text.Image.GetInstance("R3 Information.PNG"); //creates r3 information text below r3ts logo

        r3tsLogo.SetAbsolutePosition(document.PageSize.Width - 375 - 0f, document.PageSize.Height - 130 - 0f); 
        r3Info.SetAbsolutePosition(document.PageSize.Width - 365 - 0f, document.PageSize.Height - 170 - 0f); //higher the number in height the lower the place of text on paper
                                   //less  number will result in text more to right in width

        //increase size of picture
        r3tsLogo.ScalePercent(120); 
        r3Info.ScalePercent(65);

//---------------adds all images to pdf file --------------------------------- 
        document.Add(r3tsLogo);
        document.Add(r3Info);
        document.Add(new Paragraph("Date:" + DateTime.Now));




        document.Close(); 
    }

推荐答案

假定您知道如何在绝对位置添加图像(请参阅Joris的答案),但要查看如何添加文本,然后是问题的答案是:使用ColumnText.

Assuming that you know how to add images at an absolute position (see Joris' answer), but looking at how to add text, then the answer to your question is: use ColumnText.

如果只需要添加不需要换行的一行,则可以使用ShowTextAligned()方法:

If you only need to add a single line that doesn't need to be wrapped, you can use the ShowTextAligned() method:

ColumnText.showTextAligned(writer.DirectContent,
     Element.ALIGN_CENTER, new Phrase("single line"), x, y, rotation);

在此代码行中,xy是文本中间的坐标(其他可能的对齐值是ALIGN_LEFTALIGN_RIGHT). rotation参数定义以度为单位的旋转.请注意,文本"single line"不会被换行.如果您要添加的文本太长,则可以通过这种方式添加从页面上掉下来"的文本.

In this line of code, x and y are the coordinates for the middle of the text (other possible alignment values are ALIGN_LEFT and ALIGN_RIGHT). The rotation parameter defines a rotation in degrees. Note that the text "single line" won't be wrapped. You can add text that "falls off the page" this way if the text you're adding is too long.

如果要在特定矩形内添加文本,则需要使用Rectangle对象定义列:

If you want to add text inside a specific rectangle, then you need to define the column using a Rectangle object:

ColumnText ct = new ColumnText(writer.DirectContent);
ct.setSimpleColumn(new Rectangle(0, 0, 523, 50));
ct.addElement(new Paragraph("This could be a very long sentence that needs to be wrapped"));
ct.go();

如果您提供的文本超出了矩形的大小,则不会渲染该文本.但是,它仍然可以在ct对象中使用,以便您可以将其余文本添加到另一个位置.

If you provide more text than fits the rectangle, that text will not be rendered. However, it will still be available in the ct object so that you can add that remaining text at another position.

所有这些都已经被问过并回答过:

All of this has been asked and answered before:

单行:

  • http://stackoverflow.com/questions/16370428/how-to-write-in-a-specific-location-the-zapfdingbatslist-in-a-pdf-document-using
  • http://stackoverflow.com/questions/17998306/rotating-text-using-center-in-itext

多行:

  • http://stackoverflow.com/questions/33609447
  • http://stackoverflow.com/questions/31152874/how-to-add-text-in-pdfcontentbyte-rectangle-using-itextsharp
  • http://stackoverflow.com/questions/15414923/rotate-paragraphs-or-cells-some-arbitrary-number-of-degrees-itext

我是否需要长时间搜索这些示例?不,我在文本的绝对位置下的官方网站上找到了它们

Did I have to search long for these examples? No, I found them on the official web site under Absolute Positioning of text.

有智慧的人...

这篇关于如何使用iTextSharp将段落放置在特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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