C# 使用iTextSharp将图像和文本放置在特定(绝对)位置
我用iTeX-PACK生成PDF,页面中间有一个动态内容,它把PDFP表的底部推得太远了。有时甚至从页面底部转到另一页 是否可以将底部PdfPTable放置在一个不会被向下推的位置,当上面的表格需要更多的垂直空间时?这是我的解决方案 Views\Home\Index.cshtml:C# 使用iTextSharp将图像和文本放置在特定(绝对)位置,c#,itextsharp,C#,Itextsharp,我用iTeX-PACK生成PDF,页面中间有一个动态内容,它把PDFP表的底部推得太远了。有时甚至从页面底部转到另一页 是否可以将底部PdfPTable放置在一个不会被向下推的位置,当上面的表格需要更多的垂直空间时?这是我的解决方案 Views\Home\Index.cshtml: 你熟悉WriteSelectedRows()方法吗?@BrunoLowagie-我会调查一下,然后再联系你。谢谢你的评论。@BrunoLowagie-在这篇文章()的帮助下,我几乎可以让它工作了。由于C#中没有使用
你熟悉
WriteSelectedRows()
方法吗?@BrunoLowagie-我会调查一下,然后再联系你。谢谢你的评论。@BrunoLowagie-在这篇文章()的帮助下,我几乎可以让它工作了。由于C#中没有使用WriteSelectedRows()的简单示例,因此我想在完成后发布一些基本的示例代码。我是否应该补充这一点作为我自己问题的答案?这里应该有一个C#示例:但是,是的,在这里发布一个小样本作为进一步参考并接受该答案总是一个好主意。这将有助于其他遇到相同问题的人,也有助于我将来回答类似的问题。我找到了一个使用WriteSelectedRows()的问题,今天我将看一看,看看效果如何。()
<h2>Create PDF</h2>
@Html.ActionLink("Create the PDF", "CreatePDF", "Home")
</div>
public ActionResult Index()
{
return View();
}
public FileStreamResult CreatePDF()
{
Stream fileStream = GeneratePDF();
HttpContext.Response.AddHeader("content-disposition", "inline; filename=MyPDF.pdf");
var fileStreamResult = new FileStreamResult(fileStream, "application/pdf");
return fileStreamResult;
}
private Stream GeneratePDF()
{
var rect = new Rectangle(288f, 144f);
var document = new Document(rect, 10, 10, 10, 10);
document.SetPageSize(PageSize.LETTER.Rotate());
MemoryStream memoryStream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(document, memoryStream);
document.Open();
////////////////////////
//Place Image in a Specific (Absolute) Location
////////////////////////
Image myImage = Image.GetInstance(Server.MapPath("../Content/BI_logo_0709.png"));
myImage.SetAbsolutePosition(45, 45);
//[dpi of page]/[dpi of image]*100=[scale percent]
//72 / 200 * 100 = 36%
//myImage.ScalePercent(36f);
myImage.ScalePercent(36f);
document.Add(myImage);
////////////////////////
//Place Text in a Specific (Absolute) Location
////////////////////////
//Create a table to hold everything
PdfPTable myTable = new PdfPTable(1);
myTable.TotalWidth = 200f;
myTable.LockedWidth = true;
//Create a paragraph with the text to be placed
BaseFont bfArialNarrow = BaseFont.CreateFont(Server.MapPath("../Content/ARIALN.ttf"), BaseFont.CP1252, BaseFont.EMBEDDED);
var basicSmaller = new Font(bfArialNarrow, 10);
var myString = "Hello World!" +
Environment.NewLine +
"Here is more text." +
Environment.NewLine +
"Have fun programming!";
var myParagraph = new Paragraph(myString, basicSmaller);
//Create a cell to hold the text aka paragraph
PdfPCell myCell = new PdfPCell(myParagraph);
myCell.Border = 0;
//Add the cell to the table
myTable.AddCell(myCell);
//Add the table to the document in a specific (absolute) location
myTable.WriteSelectedRows(0, -1, 550, 80, pdfWriter.DirectContent);
pdfWriter.CloseStream = false;
document.Close();
memoryStream.Position = 0;
return memoryStream;
}