【问题标题】:How to place paragraphs in specific place using iTextSharp如何使用 iTextSharp 将段落放置在特定位置
【发布时间】:2017-09-04 22:03:40
【问题描述】:

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

我的代码:

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

【问题讨论】:

    标签: c# winforms position itext


    【解决方案1】:

    假设您知道如何在绝对位置添加图像(请参阅 Joris 的回答),但正在研究如何添加文本,那么您的问题的答案是:使用 ColumnText

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

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

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

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

    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 对象中使用,以便您可以将剩余的文本添加到另一个位置。

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

    单行:

    多行:

    我是否需要长时间搜索这些示例?不,我在Absolute Positioning of text 下的官方网站上找到了它们。

    寻找的人有智慧......

    【讨论】:

    • 如何改变这一行的字体 ColumnText.showTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase("single line"), x, y, rotation); ?
    【解决方案2】:

    “iText in action”一书中对这个概念进行了详尽的解释。可以在网站上找到。

    http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-3

    短代码示例(查看网站以获取其他示例):

    // step 1
    Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
    
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    
    // step 3
    document.open();
    
    // step 4
    // Create and add a Paragraph
    Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
    p.setAlignment(Element.ALIGN_CENTER);
    document.add(p);
    
    // Create and add an Image
    Image img = Image.getInstance(RESOURCE);
    img.setAbsolutePosition(
            (PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
            (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
    document.add(img);
    

    【讨论】:

    • 我认为问题不在于定位图像,而在于将"Date:" + DateTime.Now) 定位在绝对位置。当然:这也是之前回答过很多次的问题。
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多