简体   繁体   English

使用itextsharp对齐图像右侧

[英]Align image right side using itextsharp

Code is like 代码就像

iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Logo_Path);
Phrase p = new Phrase("");
logo.RotationDegrees = 180f;
p.Add(new Chunk(logo, 30, 30, false));
ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_RIGHT, p, 400f, 5f, 0);

but it shows image left alinged..how to make it right alined? 但是它显示图像偏左..如何使其偏右?

You are confusing left and right, and left-aligned and right-aligned. 您会混淆左右,左对齐和右对齐。

I have an image of a dog and an image of a fox. 我有一只狗的形象和一只狐狸的形象。

I am addding the dog left-aligned, and the fox right-aligned: 我将狗左对齐,而狐狸右对齐:

Image d = Image.getInstance(DOG);
d.setScaleToFitHeight(false);
Image f = Image.getInstance(FOX);
f.setScaleToFitHeight(false);
Chunk dog = new Chunk(d, 0, 0, false);
Chunk fox = new Chunk(f, 0, 0, false);
PdfContentByte canvas = writer.getDirectContent();
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(dog), 250, 750f, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, new Phrase(fox), 250, 750f, 0);

This is the result: 结果如下: 在此处输入图片说明

The dog is to the right, because "left-aligned" means that x = 250 will coincide with the left side of the image will be to the left. 狗在右边,因为“左对齐”意味着x = 250将与图像的左侧重合。 The fox is to the left because "right-aligned" means that x = 250 will coincide with the right side of the image. 狐狸在左侧,因为“右对齐”表示x = 250将与图像的右侧重合。

            FileStream fs = new FileStream(@"D:\test.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
            Document doc = new Document();
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);
            doc.Open();
            iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(@"D:\testLogo.png");
            logo.Alignment = 6;
            doc.Add(logo);
            doc.Add(new Paragraph(@"Test"));
            doc.Close();

you just need to set Alignment for your image as 6 form Right Alignment 您只需要为图像设置Alignment为6形式Right Alignment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM