简体   繁体   中英

create PDF file using itextsharp in C#

I have need to create PDF files where draw line on page using margin of left,right and top. But here, confusion due to that, calculation of these margin are in pixel value. So, how can it possible that draw line with setting of margin in pixel value ?

Sample code as below:

        PdfContentByte contentByte = writer.DirectContent;
        contentByte.SetLineWidth(1);
        float x1, y1, x2, y2;
        x1 = myDocument.PageSize.Width - 84;
        x2 = myDocument.PageSize.Width - 36;
        y1 = myDocument.PageSize.Height - 56;
        y2 = myDocument.PageSize.Height - 56;
        contentByte.MoveTo(x1, y1);
        contentByte.LineTo(x2, y2);
        contentByte.Stroke();

Actually, i want to draw line of width is 48 which have right margin is 36px, and top margin is 36px.

Have any idea to calculate it ?

Try this way:

string pdfpath = Server.MapPath("PDFs");
  Document doc = new Document();
  try
  {
    PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Graphics.pdf", FileMode.Create));
    doc.Open();
    PdfContentByte cb = writer.DirectContent;
    ...

Now that we have a working PdfContentByte object, we can use it to start drawing:

cb.MoveTo(doc.PageSize.Width / 2, doc.PageSize.Height / 2);
cb.LineTo(doc.PageSize.Width / 2, doc.PageSize.Height);
cb.Stroke();
cb.MoveTo(0, doc.PageSize.Height/2);
cb.LineTo(doc.PageSize.Width, doc.PageSize.Height / 2);
cb.Stroke();

Took from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM