Separating redundant code from pdf generator function

谁说胖子不能爱 提交于 2020-01-10 05:29:50

问题


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

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?


回答1:


You're adding content "the hard way". If I were you, I'd write a separate class/factory/method that creates either a Phrase or a Paragraph with the content. For instance:

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

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).

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.



来源:https://stackoverflow.com/questions/13523099/separating-redundant-code-from-pdf-generator-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!