2013-10-08 3 views
0

Я пытаюсь сделать pdf в C# с itextsharp, который должен выглядеть примерно так. Мне просто не удалось найти хорошее руководство для использования itextsharp, и я не могу, например, получить заголовок, который можно разместить справа от логотипа. Он просто хочет перейти к следующей строке. Я очень благодарен за помощь.Создание следующего PDF в C# с помощью itextsharp?

http://i.stack.imgur.com/cSaVG.png

+0

Можете ли вы опубликовать код, который вы пробовали? –

+0

http://pastebin.com/KwLj3qxZ Вот код, который я написал. Каков наилучший способ разместить текст, который появляется после логотипа и заголовка? –

+0

Вы можете найти хорошее руководство на официальном сайте: http://developers.itextpdf.com –

ответ

0

Вероятно, способ пойти с абсолютным позиционированием. Например:

ColumnText ct = new ColumnText(<pdfcontentbyte>); 
ct.setSimpleColumn("Headline",<lower_left_x_corner>,<lower_left_y_corner>, 
<upper_right_x_corner>,<upper_right_y_corner>,<leading>,<alignment>); 
ct.Go(); 

вы также можете посмотреть на setTextMatrix или showTextAligned методов.

Просто помните, что координаты начинаются в нижнем левом углу страницы.

+0

Я пробовал ваш код, и единственное, что стоит под логотипом, это линия «LAX». Другой справа от логотипа. – nothing

-1
private void ExportToPdfDemo() 
    { 

     try 
     { 

      Document document = new Document(PageSize.A4); 
      // string appPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\PURCHASE REPORTS\"; 
      string appPath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\Sales Report\"; 
      if (Directory.Exists(appPath) == false) 
      { 
       Directory.CreateDirectory(appPath); 
      } 
      PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(appPath + "/" + txt_ReferenceNo.Text + ".pdf", FileMode.Create)); 
      document.Open(); 
      PdfContentByte cb = writer.DirectContent; 
      cb.SetLineWidth(2.0f); // Make a bit thicker than 1.0 default 
      cb.SetGrayStroke(0.95f); // 1 = black, 0 = white 
      cb.MoveTo(20, document.Top - 30f); 
      cb.LineTo(400, document.Top - 30f); 
      cb.Stroke(); 

      PdfPTable Ttable = new PdfPTable(1); 
      float[] widths = new float[] { 1f }; 
      Ttable.SetWidths(widths); 


      PdfPCell numeroCell = new PdfPCell(new Phrase("Reference No: " + txt_ReferenceNo.Text)); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      Ttable.AddCell(numeroCell); 


      numeroCell = new PdfPCell(new Phrase("Date: " + dtpPurchaseDate.Text)); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      Ttable.AddCell(numeroCell); 

      numeroCell = new PdfPCell(new Phrase("Customer Code: " + txt_CustomerCode.Text)); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      Ttable.AddCell(numeroCell); 



      numeroCell = new PdfPCell(new Phrase("Customer Name: " + txt_CustomerName.Text)); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      Ttable.AddCell(numeroCell); 




      numeroCell = new PdfPCell(new Phrase("")); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      Ttable.AddCell(numeroCell); 


      numeroCell = new PdfPCell(new Phrase("")); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      Ttable.AddCell(numeroCell); 




      PdfPTable table = new PdfPTable(5); 
      widths = new float[] { 1f, 1f, 1f, 1f, 1f }; 
      table.SetWidths(widths); 
      numeroCell = new PdfPCell(new Phrase("Product Code")); 
      numeroCell.BackgroundColor = new iTextSharp.text.Color(1, 159, 222); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      table.AddCell(numeroCell); 

      numeroCell = new PdfPCell(new Phrase("Product Name")); 
      numeroCell.BackgroundColor = new iTextSharp.text.Color(1, 159, 222); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      table.AddCell(numeroCell); 


      numeroCell = new PdfPCell(new Phrase("Unit Price")); 
      numeroCell.BackgroundColor = new iTextSharp.text.Color(1, 159, 222); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      table.AddCell(numeroCell); 


      numeroCell = new PdfPCell(new Phrase("Quantity")); 
      numeroCell.BackgroundColor = new iTextSharp.text.Color(1, 159, 222); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      table.AddCell(numeroCell); 



      numeroCell = new PdfPCell(new Phrase("Total Price")); 
      numeroCell.BackgroundColor = new iTextSharp.text.Color(1, 159, 222); 
      numeroCell.Border = 0; 
      numeroCell.HorizontalAlignment = 0; 
      table.AddCell(numeroCell); 

      PdfPCell cell; 
      foreach (DataGridViewRow row in grd_PurchaseList.Rows) 
      { 

       cell = new PdfPCell(new Phrase(row.Cells["PurchseProductCode"].Value.ToString())); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 

       cell = new PdfPCell(new Phrase(row.Cells["PurchasePName"].Value.ToString())); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 

       cell = new PdfPCell(new Phrase(row.Cells["PurchaseQuantity"].Value.ToString())); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 

       cell = new PdfPCell(new Phrase(row.Cells["PurchaseUnitPrice"].Value.ToString())); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 


       cell = new PdfPCell(new Phrase(row.Cells["PurchaseTotalPrice"].Value.ToString())); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 

      } 


      for (int i = 0; i < 23; i++) 
      { 
       cell = new PdfPCell(new Phrase("")); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 
      } 

      cell = new PdfPCell(new Phrase("Tax Amount :")); 
      cell.Border = 0; 
      cell.HorizontalAlignment = 0; 
      table.AddCell(cell); 

      double taxAmount = Convert.ToDouble(txt_GrandTotal.Text) - Convert.ToDouble(txt_SubTotal.Text); 


      cell = new PdfPCell(new Phrase("" + taxAmount)); 
      cell.Border = 0; 
      cell.HorizontalAlignment = 0; 
      table.AddCell(cell); 



      for (int i = 0; i < 3; i++) 
      { 
       cell = new PdfPCell(new Phrase("")); 
       cell.Border = 0; 
       cell.HorizontalAlignment = 0; 
       table.AddCell(cell); 
      } 


      cell = new PdfPCell(new Phrase("Total Amount :")); 
      cell.Border = 0; 
      cell.HorizontalAlignment = 0; 
      table.AddCell(cell); 


      cell = new PdfPCell(new Phrase(txt_GrandTotal.Text)); 
      cell.Border = 0; 
      cell.HorizontalAlignment = 0; 
      table.AddCell(cell); 


      //table.SpacingBefore = 20f; 
      //table.SpacingAfter = 30f; 

      document.Add(Ttable); 
      Ttable.SpacingAfter = 40f; 
      document.Add(table); 
      document.Close(); 

     } 
    } 
+1

Не могли бы вы объяснить * свой код? Их много - кто-то еще видит, что это не будет так полезно, как вам может захотеть. –

Смежные вопросы