从pdf生成器函数中分离冗余代码 [英] Separating redundant code from pdf generator function

查看:111
本文介绍了从pdf生成器函数中分离冗余代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用itextsharp库的vb.net应用程序。我正在运行的是以下代码变得非常多余,在我看来,这不是一个干净的做事方式。但我似乎无法弄清楚如何将它分成一个单独的函数,我将简单地传递给字符串,x_Cord,y_Cord,倾斜并让它a)当传入它时,它作为一个数组进入或者b)为需要它的每一行做它...然后函数将返回contentBytes的必要信息......下面与我最终的冗余非常类似。

This is a vb.net app that uses the itextsharp library. What I am running in to is the following code is becoming awfully redundant which in my opinion is not a clean way to do things. But I can not seem to figure out how to separate it in to a separate function that in which I would simply pass in the string, x_Cord, y_Cord, tilt and have it either a) when passing it in it goes in as an array or b) does it for each line that needs it... The function would then return the necessary information for the contentBytes... Below is very similar to what I am ending up with being heavily redundant.

Dim cb As PdfContentByte = writer.DirectContent

为了清楚起见,我将上述内容简单地用于显示cb的声明。

I included the above simply to show what cb is declared as for clarity.

cb.BeginText()
cb.SetFontAndSize(Californian, 36)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "CERTIFICATE OF COMPLETION", 396, 397.91, 0)
cb.SetFontAndSize(Bold_Times, 22)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, name, 396, 322.35, 0)
cb.SetFontAndSize(Bold_Times, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _hours + " Hours", 297.05, 285.44, 0)
cb.SetFontAndSize(Bold_Times, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _dates, 494.95, 285.44, 0)
cb.SetFontAndSize(Bold_Times, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _class1, 396, 250.34, 0)
If Not String.IsNullOrWhiteSpace(_class2) Then
    cb.SetFontAndSize(Bold_Times, 16)
    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _class2, 396, 235.34, 0)
End If
cb.SetFontAndSize(Copper, 16)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _conf_num + _prefix + " Annual Conference " + _dates, 396, 193.89, 0)
cb.SetFontAndSize(Bold_Times, 13)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Some Name", 396, 175.69, 0)
cb.SetFontAndSize(Bold_Times, 10)
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Some Company Manager", 396, 162.64, 0)
cb.EndText()

关于将其变为自己的函数的任何想法?

Any ideas on making this in to an function of its own?

推荐答案

<你正在艰难地添加内容。如果我是你,我会写一个单独的类/工厂/方法创建短语段落与内容。例如:

protected Font f1 = new Font(Californian, 36);
protected Font f2 = new Font(Bold_times, 16);

public Phrase getCustomPhrase(String name, int hours, ...) {
  Phrase p = new Phrase();
  p.add(new Chunk("...", f1));
  p.add(new Chunk(name, f2);
  ...
  return p;
}

然后我会使用ColumnText添加短语段落在正确的位置。如果是短语,我会使用 ColumnText.showTextAligned()方法。在段落的情况下,我会使用这种结构:

Then I would use ColumnText to add the Phrase or Paragraph at the correct position. In the case of a Phrase, I'd use the ColumnText.showTextAligned() method. In the case of Paragraph, I'd use this construction:

ColumnText ct = new ColumnText(writer.DirectContent);
ct.setSimpleColumn(rectangle);
ct.addElement(getCustomParagraph(name, hours, ...));
ct.go();

前者(使用短语)是最好的,如果你只需要写一行不需要包装,朝向你想要的任何方向。

The former (using a Phrase) is best if you only need to write one line that doesn't need to be wrapped, oriented in any direction you want.

后者(使用 复合模式中的段落最好是要在特定矩形内添加文本(由左下角和上角的坐标定义)右上角。

The latter (using a Paragraph in composite mode) is best if you want to add text inside a specific rectangle (defined by the coordinates of the lower-left corner and the upper-right corner).

您采用的方法有效,但......它涉及几乎手动编写PDF语法。这更难,因此更容易出错。你已经发现了,否则你不会问这个问题; - )

The approach you've taken works, but... it involves writing PDF syntax almost "manually". That's more difficult and therefore more error-prone. You already discovered that, otherwise you wouldn't ask the question ;-)

这是一个很好的问题;我会赞成它。

It's a good question; I'll upvote it.

这篇关于从pdf生成器函数中分离冗余代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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