Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

vb.net - Separating redundant code from pdf generator function

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...