使用 ItextSharp 在 PDF 中的图像上绘制线条
Draw Lines on Image in PDF using ItextSharp
我试图在需要加载到pdf文档的图像上画线,就像我们在任何控件的paint事件上画图形一样,但它没有这样做。
有什么建议吗?
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
pdfDoc.AddHeader("Batting Report - ", txtSearchBox.Text);
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(Properties.Resources.bgWW
, System.Drawing.Imaging.ImageFormat.Jpeg);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pic);
那么,如何修改ItextSharpImage的pic对象,使其可以在图像上画线呢?
请看WatermarkedImages4 example. It is based on the WatermarkedImages1 example I referred to in the comments. The only different between the two examples is that we add text in the example written in answer to How to add text to an image?
而我们在示例中添加了一些行来回答您的问题。
我们添加这样的图片:
document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1)));
getWatermarkedImage()
方法如下所示:
public Image getWatermarkedImage(PdfContentByte cb, Image img) throws DocumentException {
float width = img.getScaledWidth();
float height = img.getScaledHeight();
PdfTemplate template = cb.createTemplate(width, height);
template.addImage(img, width, 0, 0, height, 0, 0);
template.saveState();
template.setColorStroke(BaseColor.GREEN);
template.setLineWidth(3);
template.moveTo(width * .25f, height * .25f);
template.lineTo(width * .75f, height * .75f);
template.moveTo(width * .25f, height * .75f);
template.lineTo(width * .25f, height * .25f);
template.stroke();
template.setColorStroke(BaseColor.WHITE);
template.ellipse(0, 0, width, height);
template.stroke();
template.restoreState();
return Image.getInstance(template);
}
如您所见,我使用 moveTo()
、lineTo()
和 stroke()
添加了两条绿线。我还使用 ellipse()
和 stroke()
方法添加了一个白色椭圆。
这会生成如下所示的 PDF:
正如你所看到的,椭圆的形状和线条的位置对于不同的图像是不同的,因为我根据图像的宽度和高度定义了我的坐标。
我试图在需要加载到pdf文档的图像上画线,就像我们在任何控件的paint事件上画图形一样,但它没有这样做。
有什么建议吗?
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
pdfDoc.AddHeader("Batting Report - ", txtSearchBox.Text);
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(Properties.Resources.bgWW
, System.Drawing.Imaging.ImageFormat.Jpeg);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pic);
那么,如何修改ItextSharpImage的pic对象,使其可以在图像上画线呢?
请看WatermarkedImages4 example. It is based on the WatermarkedImages1 example I referred to in the comments. The only different between the two examples is that we add text in the example written in answer to How to add text to an image? 而我们在示例中添加了一些行来回答您的问题。
我们添加这样的图片:
document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1)));
getWatermarkedImage()
方法如下所示:
public Image getWatermarkedImage(PdfContentByte cb, Image img) throws DocumentException {
float width = img.getScaledWidth();
float height = img.getScaledHeight();
PdfTemplate template = cb.createTemplate(width, height);
template.addImage(img, width, 0, 0, height, 0, 0);
template.saveState();
template.setColorStroke(BaseColor.GREEN);
template.setLineWidth(3);
template.moveTo(width * .25f, height * .25f);
template.lineTo(width * .75f, height * .75f);
template.moveTo(width * .25f, height * .75f);
template.lineTo(width * .25f, height * .25f);
template.stroke();
template.setColorStroke(BaseColor.WHITE);
template.ellipse(0, 0, width, height);
template.stroke();
template.restoreState();
return Image.getInstance(template);
}
如您所见,我使用 moveTo()
、lineTo()
和 stroke()
添加了两条绿线。我还使用 ellipse()
和 stroke()
方法添加了一个白色椭圆。
这会生成如下所示的 PDF:
正如你所看到的,椭圆的形状和线条的位置对于不同的图像是不同的,因为我根据图像的宽度和高度定义了我的坐标。