我創建了一個包含圖像的PDF文檔,我試圖在每個圖像下添加文本請記住,該頁面的模板是不同的,這取決於用戶在頁面上想要的圖像數量。我的問題是我在添加和定位文本時遇到問題。在圖像後添加文本到pdf
代碼添加圖片:添加文字
int count = 0;
imageStartX = (docSize.Width/100) * marginSizeProcent;
float imageMaxHeight = 0;
float imageMaxWidth = 0;
iTextSharp.text.Image image = null;
switch (pageLayout)
{
case PageLayoutEnum.SingleImage:
imageMaxWidth = docSize.Width - ((docSize.Width/100) * (2 * (float)marginSizeProcent));
imageMaxHeight = imageStartY - ((docSize.Width/100) * (float)marginSizeProcent);
foreach (PDFObject o in pdfObjects)
{
if (count > 0)
AddPageWithHeader(false);
image = iTextSharp.text.Image.GetInstance(o.File);
image.ScaleToFit(imageMaxWidth, imageMaxHeight);
image.SetAbsolutePosition(imageStartX + (imageMaxWidth - image.ScaledWidth)/2, imageStartY - image.ScaledHeight - (imageMaxHeight - image.ScaledHeight)/2);
image.Border = Rectangle.BOX;
image.BorderWidth = 2f;
image.BorderColor = BaseColor.DARK_GRAY;
document.Add(image);
count++;
}
break;
case PageLayoutEnum.TwoImages:
代碼:
MemoryStream memoryStream = new MemoryStream();
PdfReader pdfReader = new PdfReader(documentStream.ToArray());
PdfStamper stamper = new PdfStamper(pdfReader, memoryStream);
PdfContentByte contentbyte = stamper.GetUnderContent(1);
ColumnText dispalyIdText = new ColumnText(contentbyte);
Paragraph idText;
int counter = 0;
switch (pageLayout)
{
case PageLayoutEnum.SingleImage:
foreach (PDFObject item in pdfObjects)
{
dispalyIdText.SetSimpleColumn(200, 200, 200, 200, 200, Element.ALIGN_LEFT);
idText = new Paragraph(new Chunk(item.DisplayId, FontFactory.GetFont("Arial", 20, Font.BOLD, BaseColor.RED)));
dispalyIdText.AddElement(idText);
}
break;
case PageLayoutEnum.TwoImages:
感謝克里斯,我發現使用PDFContentByte顯示和文本位置的另一個解決方案。通常你對問題的解決方案非常合適,所以我也猜測你對這個問題的權利!如果我必須重新訪問我的代碼的這一部分,那麼我會嘗試你的建議。 – DaNet 2012-02-01 15:31:18