我正在尝试使用 itextsharp 在 C# 中制作一个 pdf,它应该看起来像这个图像。我只是很难找到使用 itextsharp 的好手册,例如,我无法将标题放在徽标的右侧。它只是想进入下一行。我真的很感谢所有的帮助。
问问题
558 次
2 回答
0
Probably the way to go is with absolute position. For example:
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();
you can also look up to the setTextMatrix or showTextAligned methods.
Just remember that the coordinates start at the bottom left corner of the page.
于 2013-10-09T16:06:09.763 回答
-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();
}
}
于 2016-04-21T04:58:41.750 回答