iTextSharp: align image and text - image

I am using iTextSharp to export reports in PDF format. Reports' headers should have following format:
The problem is to align report header by center of the page, while there is an image on the left of the page. When I use a table, report header is aligned by center of its cell, not by center of the page. Is there a better approach?
EDIT:
Code for adding header is following:
var doc = new Document(pageSize, margins.Width, margins.Width, margins.Height, margins.Height);
using (PdfWriter.GetInstance(doc, destination))
{
doc.Open();
var headerTable = new PdfPTable(1){ WidthPercentage = 100 };
RenderHeader(headerTable); // adds several lines to headerTable
doc.Add(headerTable);
}

Do you really need a PdfPTable?
Why not add a Paragraph with the data that is right-aligned, followed by a couple of Paragraphs that are centered.
Then add the image at an absolute position, for instance:
Image img = Image.GetInstance(path_to_image);
img.SetAbsolutePosition(36, PdfWriter.getVerticalPosition(true));
document.Add(Image);
Another option is to add the Image in a table or a cell event instead of adding it straight to the table. Suppose that your PdfPTable consists of a single row and a single column, then you could define a cell event like this:
public class ImageCell : IPdfPCellEvent {
public void CellLayout(
PdfPCell cell, Rectangle position, PdfContentByte[] canvases
) {
float x1 = position.Left + 2;
float y1 = position.Top - 2;
float y2 = position.Bottom + 2;
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
Image img = Image.GetInstance(path_to_image);
img.scaleToFit(10000, y1 - y2);
img.SetAbsolutePosition(x1, y2);
canvas.AddImage(img);
}
}
Suppose that cell is the PdfPCell with the date and the title, then you can do this:
cell.CellEvent = new ImageCell();
Note that in this case, the image will be scaled to the height of the cell.
Forgive me if the code doesn't compile right away, I'm writing this by heart (based on experience), I didn't test the actual code.

Related

How to create dynamic stopping Scroll Rect in unity?

I have an gameobject1(added Scroll Rect component)and inside of it another gameobject2(The Scroll rect component's content).In gameobject2 has images.The number of images can be 10 or 20..(Any numbers).The Movement Type is Elastic.As you know it will stop scrolling only until gameobject2 height's length. How to stop on dynamic number's of length.In gameobject2 the number of images can be different. It depends on search results. The results can be 5,8, or 200. So I need to scroll until last of search result.So how to stop scrolling on exactly length in Scroll rect component?
You can use ContentSizeFitter component. GameObject with name "Content", is a content for scrollRect component of "ScrollView"-gameObject.
RectTransform#SetSizeWithCurrentAnchors
I use this a lot when building dynamic scrolling lists. After adding all the items I want (and each having a known size, and all positioned using that size) I update the content's RectTransform with the new size (total number of objects added * size of the object).
For example, I have this code:
int i = 0;
//for each item in a list of skills...
IEnumerator<Skill> list = SkillList.getSkillList();
Transform skillListParent = GuiManager.instance.skillPanel.transform;
while(list.MoveNext()) {
Skill sk = list.Current;
//create a prefab clone...
GameObject go = Main.Instantiate(PrefabManager.instance.SKILL_LISTITEM, skillListParent) as GameObject;
//set its position...
go.transform.localPosition = new Vector3(5, i * -110 -5, 5);
//add a button event or other data (some lines omitted)...
Transform t1 = go.transform.FindChild("BuyOne");
t1.GetComponent<Button>().onClick.AddListener(delegate {
doBuySkill(sk);
});
t1.GetChild(0).GetComponent<Text>().text = Main.AsCurrency(sk.getCost(1)) + " pts";
//track how many...
i++;
}
//update rect transform
((RectTransform)skillListParent).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, (i * 110 + 10));

"page x of y" in a table, how to allign the text within a table instead of the page border?

I was able to create page x of y using the Building Blocks example of Chapter 7: Handling events; setting viewer preferences and writer properties Solving the "Page X of Y" problem. In the example, the texts of "page x of y" are alligned via a Canvas with the page border. But very often is that the "x of y" shall be put into a table like this:
In such cases, the text shall be alligned within the table, how to do this?
In my application, the table which includes the page x of y shall be shown on each page and still at a fixed position, i.e. at the right upper position of a page. And the table format and size will not change for the whole document.
First of all, in order to fit the whole table, you would want to increase the bottom margin of the Document:
Document document = new Document(pdf);
document.setBottomMargin(100);
After that, you can still use Canvas to add a table instead of a paragraph. I will base the answer on the PageXofY example you refer to.
First of all, create a usual Table:
Table table = new Table(UnitValue.createPercentArray(new float[] {50, 50}));
table.addCell(new Cell(4, 1));
table.addCell(new Cell().add("Filename: "));
table.addCell(new Cell().add("Issue date: "));
Paragraph pageXofY = new Paragraph().
add("Page " + String.valueOf(pageNumber) + " of ").
add(new Image(placeholder));
table.addCell(new Cell().add(pageXofY));
table.addCell(new Cell().add("Location: "));
Note that we still use a placeholder FormXObject to store the total number of pages.
Change the side to font size, it is 12 in our case. Create placeholder like this:
placeholder = new PdfFormXObject(new Rectangle(0, 0, 2 * side, side));
Make a slight change to the writeTotal() method. The y position of the text has been changed to -descent:
public void writeTotal(PdfDocument pdf) {
Canvas canvas = new Canvas(placeholder, pdf);
canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
0, -descent, TextAlignment.LEFT);
}
Now all you need to to is add this table to the proper place on the page:
float marginX = 36;
Canvas canvas = new Canvas(pdfCanvas, pdf, new Rectangle(marginX, 10, pageSize.getWidth() - marginX * 2, 100));
canvas.add(table);
pdfCanvas.release();
The result looks like this:

Put an image in the center of a cell with migradoc

I need to put an image in the center of a table's cell.
When I add the image in a cell the image is aligned topleft.
How can I align the image in the center of a cell?
You might need to add a paragraph to the cell, set the alignment on the paragraph, and then add the image to the paragraph.
row.Cells[0].Format.Alignment = ParagraphAlignment.Center;
row.Cells[0].VerticalAlignment = VerticalAlignment.Center;
row.Cells[0].AddParagraph().AddImage(imageLocation);
row.Cells[0].Format.Alignment = ParagraphAlignment.Center;
row.Cells[0].VerticalAlignment = VerticalAlignment.Center;
In my version of MigraDoc, Jeff and Reuben's solution does not work as shown: Setting the cell's Format.Alignment property has no effect, even when the image is inside a paragraph.
However, what does work for me is to put the image in a paragraph, just as Jeff and Reuben say, then give the paragraph a named style which includes centering.
In my method which predefines all my styles, I do something like:
// Here we assume the "TableText" style has already been created
var sty = doc.Styles.AddStyle( "TableTextCenter", "TableText" );
sty.ParagraphFormat.Alignment = ParagraphAlignment.Center;
And at the point where I add the image, I do this:
var para = table.Cells[ 0 ].AddParagraph();
para.Style = "TableTextCenter"; // <----- This is the magic!
var img = para.AddImage( imageFileSpec );
img.LockAspectRatio = true;
img.Width = "4cm";
img.WrapFormat = new WrapFormat {
Style = WrapStyle.Through
};
As #Reuben says, what is interesting is:
row.Cells[0].AddParagraph().AddImage(imageLocation);
I was trying with
row.Cells[0].AddImage(imageLocation);
And the image was inserted in the document but I couln't get it centered.

Printing a multi-page HTML5 canvas element

This is the first question I've ever asked on here, as normally I can find an answer that will solve all my problems. However, this week I've hit quite the wall.
I'm using a canvas on my page that's working great and can be saved as an image file. I've also created a button that will open the canvas in a new window for printing after using toDataURL. I'm using the code below:
function printable() {
var dataUrl = document.getElementById("mainCanvas").toDataURL("image/png");
var windowContent = '<!DOCTYPE html>';
windowContent += '<head><title>Print Map</title></head>';
windowContent += '<body>';
windowContent += '<img src = "' + dataUrl + '">';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('', '', width = 340, height = 260);
printWin.document.open();
printWin.document.write(windowContent);
printWin.document.close();
printWin.focus();
printWin.print();
}
This works great and opens the print dialogue to print my canvas image. My issue is that my image is larger than a single page and only the first page of my image prints. Is there any way to print my canvas across multiple pages, or should i just chalk this up to bad browser to printer functionality and get my users who need to print to use a different program after saving their canvas locally?
Thanks
there is a stupid solution, but definitely work.
use a script to split canvas, and put contents to several separate canvases.
use these functions:
ImageData getImageData(in float x, in float y, in float width, in float height);
void putImageData(in ImageData imagedata, in float dx, double dy, in float dirtyX Optional , in float dirtyY Optional , in float dirtyWidth Optional , in float dirtyHeight Optional );

RDLC Reports Proportional Image Alignment

I'm working on an RDLC Report ,I use a DB Image, and set it's size type to Proportional, if the image isn't the exact size as it's borders, borders won't fit .. this is ok, but the image will be aligned top-left according to borders while I need it to be centered (on PDF) while on IE it's centered .., here are image examples,, Please I Need Help for this ..
This is the I Already Have
http://up.g4z4.com/uploads/f1fdee3542.jpg
This is the Desired One
http://up.g4z4.com/uploads/a3d2ca5b8e.jpg
I read once that the image origin or Registration Point in RDLC Reports is on the top left, that's why it shows like this .. but the one who wrote that said he's not sure about it .. is this possible? or is it related to a default alignment that can't be changed?
Thank you,
For every image we can find out its aspect ratio either width/height or height/width.
Assuming that your cell has known width and height you can determine if its going to be too narrow or too short. Moreover you can calculate its new width and height with following formulas:
newWidth = cellheight*oldwidth/oldheight
newHeight = cellwidth*oldheight/oldwidth
CellWidth - NewWidth = 2x PaddingLeft
CellHeight - NewHeight = 2x PaddingTop
So all you need to do now is just divide the result by 2 and cast it onto an integer.
I implemented following getters for each image in database(I am actually pulling these images from sqlite database) in the SubReportProcessing Event for my main report I simply add all my images as DataSource.
public class ImageModel
{
public int ImgId { get; set; }
public byte[] Blob { get; set; }
public string PaddingLeft
{
get
{
var img = byteArrayToImage(Blob);
//cell width and height must be specified in points
//(cellwidth - cellheight * image aspect ratio) / 2
var result = (int)((256.0f - 256.0f * ((float)img.Width / (float)img.Height)) / (float)2) + "pt";
return result;
}
}
public string PaddingTop
{
get
{
var img = byteArrayToImage(Blob);
var result = (int)((256.0f - 256.0f * ((float)img.Height / (float)img.Width)) / (float)2) + "pt";
return result;
}
}
After that operation I can now set padding value under image properties as follows:
=Fields!PaddingLeft.Value
=Fields!PaddingTop.Value
Hope that helps!
Cheers ;)
I'm looking for an answer to this as well; I found this code which does the trick (it works) but it's not very generic (depends on a hard coded page width). With a little work this code could work.

Resources