여러 섹션으로 PDF를 작성해야하며 각 섹션이 라인을 추가해야하지만이 라인을 그릴 위치를 모르겠습니다.iTextSharp에서 현재 (X, Y) 위치를 찾는 방법은 무엇입니까?
문서의 다음 요소가 쓰여지는 정확한 좌표 [x, y]를 찾아야합니다. 나는 Y 위치를 사용할 수는있다 생각
여러 섹션으로 PDF를 작성해야하며 각 섹션이 라인을 추가해야하지만이 라인을 그릴 위치를 모르겠습니다.iTextSharp에서 현재 (X, Y) 위치를 찾는 방법은 무엇입니까?
문서의 다음 요소가 쓰여지는 정확한 좌표 [x, y]를 찾아야합니다. 나는 Y 위치를 사용할 수는있다 생각
다음 Y
를 얻을 수 GetVerticalPosition
를 사용
PdfWriter.getVerticalPosition()
처럼 @Olaf 말했다보십시오. X
은 문서의 LeftMargin
입니다. 다음은 iTextSharp를 대상으로 전체 작업을 WinForms 응용 프로그램이 잘하면 당신이 찾고있는 무엇을한다는 것을 5.1.1.0 :
using System;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Test file name
string TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
//Standard iTextSharp setup
using (FileStream fs = new FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (Document doc = new Document(PageSize.LETTER))
{
using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
{
//Open the document for writing
doc.Open();
//Will hold our current x,y coordinates;
float curY;
float curX;
//Add a paragraph
doc.Add(new Paragraph("It was the best of times"));
//Get the current Y value
curY = w.GetVerticalPosition(true);
//The current X is just the left margin
curX = doc.LeftMargin;
//Set a color fill
w.DirectContent.SetRGBColorStroke(0, 0, 0);
//Set the x,y of where to start drawing
w.DirectContent.MoveTo(curX, curY);
//Draw a line
w.DirectContent.LineTo(doc.PageSize.Width - doc.RightMargin, curY);
//Fill the line in
w.DirectContent.Stroke();
//Add another paragraph
doc.Add(new Paragraph("It was the word of times"));
//Repeat the above. curX never really changes unless you modify the document's margins
curY = w.GetVerticalPosition(true);
w.DirectContent.SetRGBColorStroke(0, 0, 0);
w.DirectContent.MoveTo(curX, curY);
w.DirectContent.LineTo(doc.PageSize.Width - doc.RightMargin, curY);
w.DirectContent.Stroke();
//Close the document
doc.Close();
}
}
}
this.Close();
}
}
}
실제로 단지의 y 위치에 있습니다.
그러나 하나는 간단한 텍스트를 렌더링 할 필요가 그 사진을 넣어하거나 선을 그 후, 그는 항상 렌더링 된 텍스트의 크기를 계산 할 수있는 경우 : 당신은 그냥 선을 그릴해야하는 경우
var chunk = new Chunk(String.Format("Sample text {0}",));
document.Add(new Paragraph(t));
float curY = writer.GetVerticalPosition(false);
float x = document.Left + chunk.GetWidthPoint();
을 현재 섹션 이후에 어쩌면 현재 x와 y를 알 필요가 없습니다. 시험해보기 :
iTextSharp.text.pdf.draw.DottedLineSeparator sepLINE = new iTextSharp.text.pdf.draw.DottedLineSeparator();
sepLINE.LineWidth = 1;
sepLINE.Gap = 2;
sepLINE.Percentage = 50;
sepLINE.LineColor = new iTextSharp.text.BaseColor(System.Drawing.Color.Blue);
Chunk chnkLINE = new Chunk(sepLINE);
pdfDoc.Add(chnkLINE);