I have an existing PDF in which i am trying to add a logo in Header, I have found a good example from
How can I insert an image with iTextSharp in an existing PDF?
It is adding logo in Footer passing 0,0 in image.SetAbsolutePosition(100, 100);
but i want to add logo in Header. If anyone know about it, please suggest.
Are you creating the document from scratch?
If so,
you know the dimensions of the page. It's PageSize.A4 by default, or whatever Rectangle you passed to the Document constructor. You need to adjust the X and Y values depending on the value of that Rectangle. For instance:
image.setAbsolutePosition(rect.Left, rect.Top - image.ScaledHeight);
Where rect is the page size.
As you're adding a header, you want this header to appear on each page, hence you'll use a page event. Take a look at the OnEndPage() method in this example. Make sure you don't add the image bytes as many times as there are pages! Create the image instance outside the onEndPage method, for instance in the constructor of your page event implementation.
If not, you need to get the CropBox of every page:
rect = reader.GetCropBox(page);
If no CropBox was defined, you need to get the MediaBox:
rect = reader.GetPageSize(page);
Where page is a page number (e.g. 1). Based on the value of rect, you can define the position of the image, as shown above.
I hope you understand that your code where you've used x = 0 and y = 0 won't always show the image in the footer. You're making the assumption that the lower-left corner of each page in each PDF has the coordinate (0, 0). That assumption is wrong!
Related
I was creating a C# Console Application that fills in the details in a certificate template (pdf) file using ITextSharp. The text that needs to be filled is dynamically generated.
If the dynamically generated text exceeds a certain size the field name gets truncated. I have already thought of an algorithm to solve this problem for which I require the maximum number of characters that can be accommodated in a page with the given font and font-size. The width of the page where the text has to be accommodated is given to us already.
I used the following code:
string text = "This is the test string that needs to be accommodated";
// Registering a font
FontFactory.Register("somefont.ttf", "script");
var script = FontFactory.GetFont("script", 20f); // registers the font with the given type and size.
Phrase phrase_text = new Phrase();
phrase_text.Add(new Chunk(text, script));
ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(phrase_text), PageSize.A4.Height/2, 140, 0);
This code seems to work fine, but when I am adding a bigger text in the screen, the text gets truncated with the alignment still in the center (in the horizontal sense).
I was reading something on using the Graphics library, but could not resolve the issue.
You say that you are filling out a template. That assumes that you have at least one AcroForm field in your document. Filling out fields is explained in the answer to this question: itextsharp setting the stamper FormFlatttening=true results in no output
Usually, the field has a property that defines the font size that needs to be used, but you are right: if the text doesn't fit the rectangle defined by the form field, the text gets truncated. You can change this by setting the font size to "auto". This way, the font size will decrease if the text doesn't fit. This is explained here: Set AcroField Text Size to Auto
Or you could use a multiline field. See How to get the row count of a multiline field?
If you don't have a form field (which would be strange, because that means that you have to program the coordinates, e.g. PageSize.A4.Height/2, 140), you shouldn't use the showTextAligned() method. If there is no way you can define the location using a form field (but I can't think of any argument why you couldn't), you should take a look at the MovieAds example (taken from my book iText in Action).
I have a method AddParagraph that looks like this:
public bool AddParagraph(Paragraph p, PdfContentByte canvas,
AcroFields.FieldPosition f, bool simulate)
{
ColumnText ct = new ColumnText(canvas);
ct.SetSimpleColumn(
f.position.Left, f.position.GetBottom(2),
f.position.GetRight(2), f.position.Top
);
ct.AddElement(p);
return ColumnText.HasMoreText(ct.Go(simulate));
}
In this example, I create a ColumnText object for which I define a column. I add a paragraph, and I render that paragraph. If simulate is true, I don't add it for real, I add it virtually to see if the paragraph fits. If it fits, I call the method anew for real (with simulate = false).
I repeat this as many time as needed, decreasing the font with 0.2 user units at every try. Only if the font gets smaller than 6pt, I allow iText to truncate the content.
while (AddParagraph(CreateMovieParagraph(movie, size),
canvas, f, true) && size > 6)
{
size -= 0.2f;
}
I am currently working with iTextSharp. I get some polygon cordinates from a file (location of each significant point) and trying to draw them in PDF. It is working great but problem is that the shape is always in bottom left corner on a page. It looks like this:
I don't want to change my data (it will be difficult and very problematic due to fact that I want to copy this shape 4 times on page) but I want to set local coordinate system in some place and then start drawing. Any ideas? Maybe another PDF C# library?
The problem was resolved by code:
PdfTemplate tp = cb.CreateTemplate(width, height);
tp.MoveTo(oStartPoly.dStartX, oStartPoly.dStartY);
etc...
than you do like so:
cb.AddTemplate(tp, fScaleFactor, 0, 0, fScaleFactor, fX, doc.PageSize.Height / 3);
Within this method you can scale and set absolute location.
The only problem is that line width is scaled too.
I would like to know how I can create one image from many. I would like to create a tile in my windows phone application like in this image (specifically, the People tile):
(source: addictivetips.com)
I have nine pictures, and I would create an image, that I will add like tile to background. Does anybody know how can I create an image that looks like the one in that picture?
I have very little experience in this space, but have you considered creating a control that simply displays up to 9 pictures side by side in a grid like that? You then can bind each image independently & change them out however you want. This article touches on how to bind phontos in WP7 nicely:
http://msdn.microsoft.com/en-us/library/hh286418(v=vs.92).aspx
If you're talking about assembling an actual graphic image like a jpeg or bitmap, you'll need to look at the Image Class, Bitmap Class, and Graphics Class. Essentially you'll need to implement the following steps:
Load the relevant images with From method in Image, typically Image.FromFile.
Determine how many rows and columns you'll be using.
Calculate the total width and height for your layout using the widths and heights of the loaded images with appropriate padding added.
Create a new Bitmap of the appropriate size with the correct background color and iamge format.
Have variables for the current drawing location (x & y).
Have variables for the current row and column in your layout.
In a loop, Create your Graphics object.
Use Graphics.DrawImage to add your loaded image to the layout bitmap.
Increment your drawing row and or column as appropriate.
Calculate your new drawing location.
Repeat until done.
One of the options is to use WriteableBitmapEx
Also you can probably find an answer to your question here: How can I merge two images into one?
I am trying to create a pdf document in c# using iTextSharp 5.0.6. I want to add header and footer to every page in OnStartPage and OnEndPage events respectively.
In case of footer there is a problem that the footer is created right where the page ends whereas I would like to be at the bottom of page.
Is there a way in iTextSharp to specify page height so that footer is always created at the bottom.
Thanks!
The page's height is always defined:
document.PageSize.Height // document.getPageSize().getHeight() in Java
Keep in mind that in PDF 0,0 is the lower left corner, and coordinates increase as you go right and UP.
Within a PdfPageEvent you need to use absolute coordinates. It sounds like you're either getting the current Y from the document, or Just Drawing Stuff at the current location. Don't do that.
Also, if you want to use the same exact footer on every page, you can draw everything into a PdfTemplate, then draw that template into the various pages on which you want it.
PdfTemplate footerTmpl = writer.getDirectContent().createTemplate( 0, 0, pageWidth, footerHeight );
footerTmpl.setFontAndSize( someFont, someSize );
footerTmpl.setTextMatrix( x, y );
footer.showText("blah");
// etc
Then in your PdfPageEvent, you can just add footerTempl at the bottom of your page:
writer.getDirectContent().addTemplateSimple( footerTmpl, 0, 0 );
Even if most of your footer is the same, you can use this technique to save memory, execution time, and file size.
Furthermore, if you don't want to mess with PdfContentByte drawing commands directly, you can avoid them to some extent via ColumnText. There are several SO questions tagged with iText or iTextSharp dealing with that class. Poke around, you'll find them.
I'm writing a web application to generate labels. The label printer that I'm targeting utilizes 12mm tape and the customer specified that they want to limit the labels to 3.25". I know that with iTextSharp I can specify the size of the document I want to create, however it appears that I have to specify both a width and height.
Document document = new Document(new iTextSharp.text.Rectangle(234f, 33.84f));
234 is 3.25" converted to points, and 33.84 is 12mm converted to points, so this sets the document to the maximum size allowed. Is there any way to set just the height and let the document auto-expand to the amount of content? With that, is there a way to determine if the expanded width of the document exceeds to maximum allowed by the customer? Thanks in advance for any help you can offer.
No, that is not possible in iText. It's not a particularly good idea anywhere else either. As Redman said, PDF is a print-based format. It's not HTML.
There are some tricks you can do to get around this to some extent:
Create your page with the maximum legal height. 200" * 72dpi = 14400 points.
Add a "generic tag" to your paragraphs.
create a tag event handler that tracks where the bottom of your last paragraph was drawn.
Save the PDF.
Open it again with a PDFStamper
Set the bottom of the page to match the location of that last paragraph. Remember that the bottom left starts off at 0,0, and the top right will be
Save the final PDF
This trick will only work if your total output is less that 200" high. If you go over that, you'll still get a second page, and your "where the bottom is" code had better be prepared for it.
PS: I don't see what's wrong with having a number of 12mm x 3.25" pages... won't that perfectly fit the labels they want printed?
As far as I know, because PDF is a print format, the document width and height must be set.
This is why reporting tools generally default page sizes to the default printer page size.
You best bet to try and calculate the size of the content and create the PDF accordingly.
Depending on the volume and nature of the workflow, you could provide a preview for the customer to check before printing.