2012-11-23 2 views
2

이것은 itextsharp 라이브러리를 사용하는 vb.net 응용 프로그램입니다. 내가 뭘하고있는 건지 다음 코드가 너무 복잡해져서 제 생각에는 일을 할 수있는 깨끗한 방법이 아니란 것입니다. 하지만 별도의 함수로 분리하는 방법을 알아낼 수없는 것 같습니다. 문자열에서 x_Cord, y_Cord, tilt를 전달하면됩니다. 또는 그것을 전달할 때 하나의 배열을 전달하거나 b) 그것을 필요로하는 각 라인에 대해 수행합니다 ... 함수는 contentBytes에 필요한 정보를 반환합니다 ... 아래는 내가 많이 중복되는 것으로 끝나는 것과 매우 유사합니다. pdf 생성기 함수에서 중복 코드 분리

Dim cb As PdfContentByte = writer.DirectContent 

는 단순히 선명도로 선언 된 것을 CB 보여주기 위를 포함.

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

이 기능을 자체적으로 만드는 것에 대한 아이디어가 있으십니까?

답변

2

"어려운 방법"으로 콘텐츠를 추가하고 있습니다. 내가 너라면, 콘텐츠가있는 Phrase 또는 Paragraph을 만드는 별도의 클래스/팩토리/메소드를 작성한다. 예를 들어 :

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

은 그 때 나는 올바른 위치에 Phrase 또는 Paragraph을 추가 ColumnText을 사용합니다. Phrase의 경우 ColumnText.showTextAligned() 메서드를 사용합니다. Paragraph의 경우,이 구성을 사용하십시오 :

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

당신은 어떤 방향으로 배향, 포장 할 필요가없는 한 줄을 작성해야하는 경우 최고 (A Phrase 사용) 전 네가 원해.

후자

당신이 (왼쪽 하단과 오른쪽 위 모서리의 좌표에 의해 정의 된) 특정 사각형 안에 텍스트를 추가하려면 최선의 (a Paragraph 복합 모드 사용).

취해진 접근법은 ... "거의 수동으로"PDF 구문을 작성하는 것과 관련이 있습니다. 이는 더 어렵고 오류가 발생하기 쉽습니다. 당신은 이미 그것을 발견했다. 그렇지 않다면 질문을하지 않을 것이다 ;-)

좋은 질문이다. 나는 그것을 upvote거야.