I am using a deprecated version of iTextSharp (4.1.6.0) to produce PDFs form my MVC3 application, and really need to be able to lay translucent shapes over the top of other shapes and images, the goal being to fade the colours of the image underneath it, or grey it out. I would have thought this would be as simple as setting the alpha channel when choosing a colour for the shapes fill, so i tried this:
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(#"C:/Filepath/doc.pdf", FileMode.Create))
doc.Open();
PdfContentByte over = writer.DirectContent;
// draw shape to be faded out
over.Rectangle(10, 10, 50, 50);
over.SetColorFill(Color.BLUE);
over.Fill();
// draw shape over the top to do the fading (red so i can easily see where it is)
over.Rectangle(0, 0, 60, 60);
over.SetColorFill(new Color(255,0,0,150)); // rgba
over.Fill();
doc.Close();
I would expect this to draw two rectangles near the bottom left of the page, a small blue one overlaid with a larger red, translucent one, but the red one is not translucent!
So i did some googling and found this page, which is actually about iText not iTextSharp, where they suggest using PdfGstate to set the fill opacity like this:
PdfGState gstate = new PdfGState();
gstate.setFillOpacity(0.3);
but when i try that the gstate object has no method that is anything like .setFillOpacity()! If anyone can point me in the right direction I would be most grateful.
One of the rules of converting Java libraries to C# libraries is that all of the getXYZ and setXYZ methods should be converted to simple C# properties.
So gstate.setFillOpacity(0.3); will be come gstate.FillOpacity = 0.3f;
using (Document doc = new Document())
{
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(#"mod.pdf", FileMode.Create));
doc.Open();
PdfContentByte over = writer.DirectContent;
over.SaveState();
over.Rectangle(10, 10, 50, 50);
over.SetColorFill(BaseColor.BLUE);
over.Fill();
PdfGState gs1 = new PdfGState();
gs1.FillOpacity = 0.5f;
over.SetGState(gs1);
over.Rectangle(0, 0, 60, 60);
over.SetColorFill(new BaseColor(255, 0, 0, 150));
over.Fill();
over.RestoreState();
doc.Close();
}
Related
I want to fill a shape with a bitmap, in other words, mask a bitmap with a shape. I can't find a direct example. I am using iText 7 and C#.
You can fill a shape with a bitmap by making that shape a clip path and adding the bitmap, e.g. like this:
using (PdfWriter writer = new PdfWriter(...))
using (PdfDocument pdfDoc = new PdfDocument(writer))
{
ImageData data = ImageDataFactory.Create(...);
PdfCanvas pdfCanvas = new PdfCanvas(pdfDoc.AddNewPage());
pdfCanvas.SaveState()
.MoveTo(100, 100)
.LineTo(300, 200)
.LineTo(400, 400)
.LineTo(200, 300)
.ClosePath()
.EoClip()
.EndPath();
pdfCanvas.AddImageAt(data, 100, 100, false);
pdfCanvas.RestoreState();
}
(AddImageWithMask test testAddImageInShape)
The result looks like this:
How do I place text at a specific location on the pdf? I did a little bit of searching but didn't find anything too good. I have document.Add(new Paragraph("Date:" + DateTime.Now)); and I wanted to place that on a specific area on the pdf file.
My code:
private void savePDF_Click(object sender, EventArgs e)
{
FileStream fileStream = new FileStream(nameTxtB.Text + "Repair.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
Document document = new Document();
document.Open();
iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(PageSize.LETTER);
PdfWriter pdfWriter = PdfWriter.GetInstance(document, fileStream);
iTextSharp.text.Image r3tsLogo = iTextSharp.text.Image.GetInstance("rt3slogo.PNG"); //creates r3ts logo
iTextSharp.text.Image r3Info = iTextSharp.text.Image.GetInstance("R3 Information.PNG"); //creates r3 information text below r3ts logo
r3tsLogo.SetAbsolutePosition(document.PageSize.Width - 375 - 0f, document.PageSize.Height - 130 - 0f);
r3Info.SetAbsolutePosition(document.PageSize.Width - 365 - 0f, document.PageSize.Height - 170 - 0f); //higher the number in height the lower the place of text on paper
//less number will result in text more to right in width
//increase size of picture
r3tsLogo.ScalePercent(120);
r3Info.ScalePercent(65);
//---------------adds all images to pdf file ---------------------------------
document.Add(r3tsLogo);
document.Add(r3Info);
document.Add(new Paragraph("Date:" + DateTime.Now));
document.Close();
}
Assuming that you know how to add images at an absolute position (see Joris' answer), but looking at how to add text, then the answer to your question is: use ColumnText.
If you only need to add a single line that doesn't need to be wrapped, you can use the ShowTextAligned() method:
ColumnText.showTextAligned(writer.DirectContent,
Element.ALIGN_CENTER, new Phrase("single line"), x, y, rotation);
In this line of code, x and y are the coordinates for the middle of the text (other possible alignment values are ALIGN_LEFT and ALIGN_RIGHT). The rotation parameter defines a rotation in degrees. Note that the text "single line" won't be wrapped. You can add text that "falls off the page" this way if the text you're adding is too long.
If you want to add text inside a specific rectangle, then you need to define the column using a Rectangle object:
ColumnText ct = new ColumnText(writer.DirectContent);
ct.setSimpleColumn(new Rectangle(0, 0, 523, 50));
ct.addElement(new Paragraph("This could be a very long sentence that needs to be wrapped"));
ct.go();
If you provide more text than fits the rectangle, that text will not be rendered. However, it will still be available in the ct object so that you can add that remaining text at another position.
All of this has been asked and answered before:
Single line:
http://stackoverflow.com/questions/16370428/how-to-write-in-a-specific-location-the-zapfdingbatslist-in-a-pdf-document-using
http://stackoverflow.com/questions/17998306/rotating-text-using-center-in-itext
Multiple lines:
http://stackoverflow.com/questions/33609447
http://stackoverflow.com/questions/31152874/how-to-add-text-in-pdfcontentbyte-rectangle-using-itextsharp
http://stackoverflow.com/questions/15414923/rotate-paragraphs-or-cells-some-arbitrary-number-of-degrees-itext
Did I have to search long for these examples? No, I found them on the official web site under Absolute Positioning of text.
Wisdom is there for those who search...
This concept is thoroughly explained in the book 'iText in action'. Which can be found on the website.
http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-3
Short code sample (check the site for other examples):
// step 1
Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
// Create and add a Paragraph
Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);
// Create and add an Image
Image img = Image.getInstance(RESOURCE);
img.setAbsolutePosition(
(PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
(PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
document.add(img);
I am trying to draw lines on image that needs to be loaded on pdf document, just like we draw graphics on paint event of any control, but it fails to do so.
Any Suggestions ?
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);
So, how do I modify the pic object of ItextSharpImage so that it can draw lines on the image?
Please take a look at the 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?
whereas we add lines in the example written in answer to your question.
We add images like this:
document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1)));
The getWatermarkedImage() method looks like this:
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);
}
As you can see, I add two green lines using moveTo(), lineTo() and stroke(). I also add a white ellipse using the ellipse() and stroke() method.
This results in a PDF that looks like this:
As you can see, the shape of the ellipse and the position of the lines are different for the different images because I defined my coordinates based on the width and the height of the image.
I need some help with tweaking my PDF header/footer alongside my text areas. The first page looks okay and it just gets worse from there. Is the header and footer eating into my existing margin spaces?
I would like to know what is going wrong and what I can tweak to set the below:
Page widths
Margin widths
Header
Footer
Text area
My header override function is as below:
public partial class Header : PdfPageEventHelper
{
public override void OnStartPage(PdfWriter writer, Document doc)
{
PdfPTable headerTbl = new PdfPTable(2);
headerTbl.SetWidths(new float[] { 4, 1 });
headerTbl.TotalWidth = doc.PageSize.Width;
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/view.gif"));
logo.ScalePercent(5);
PdfPCell cell = new PdfPCell(logo);
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
cell.PaddingRight = 20;
cell.Border = Rectangle.NO_BORDER;
Font timesH = new Font(Font.FontFamily.TIMES_ROMAN, 20);
Font times = new Font(Font.FontFamily.TIMES_ROMAN, 10);
Chunk c1= new Chunk("THIS IS MY HEADER TEXT", timesH);
Chunk c = new Chunk("\n", times);
Chunk c2=new Chunk("PLEASE HAVE A NICE DAY", times);
Phrase p = new Phrase();
p.Add(c1);
p.Add(c);
p.Add(c2);
PdfPCell cell2 = new PdfPCell(p);
cell2.Border = Rectangle.NO_BORDER;
headerTbl.AddCell(cell2);
headerTbl.AddCell(cell);
headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);
}
}
stringWrite is a StringWriter that contains a bunch of data. More clarity HERE.
I create the pdf as follows:
StringReader sr = new StringReader(stringWrite.ToString());
Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 30f, 30f);
pdfDoc.SetPageSize(PageSize.A4.Rotate());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter pdfwriter = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfwriter.PageEvent = new Footer();
pdfwriter.PageEvent = new Header();
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
I'm using iTextSharp, C#, Asp.net in my application.
You initialize the document creation with
Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 30f, 30f);
This means especially that you reserve 10 units both left and right and 30 units both top and bottom as margin. The whole remaining inner space can be used by the automatic content layout mechanisms.
Header material, therefore, has to be drawn in that margin area, otherwise it may overlap with page content.
Your code, on the other hand, creates a paragraph with two lines, the first one set in a 20 unit font, the second one in a 10 unit font, and wraps it in a table. Thus, this table has a height of more than 30 units (the combined height of those two lines plus some inter-line space and possibly some table cell margin overhead). Then it draws it like this
headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);
So it starts 10 units beneath the top of the page. You defined the top page margin to be merely 30, though. Thus, the header and the page content overlap in a stripe more than 10 units high.
Thus, I would propose you increase the top margin by 20 (more than ten plus some distance for the looks of it):
Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 50f, 30f);
The reason why the first page looked all right most likely is that your HTML starts with some empty space (at least as far as the HTMLWorker is concerned).
Additional remarks:
Adding content in OnStartPage is discouraged. You should use OnEndPage for all such manipulations of the content, headers, footers, background images, ...
HTMLWorker is deprecated. You should use XMLWorker.
Is there a reason why you don't set the final page size from the start (in new Document) but instead separately?
Is it possible to set a background image for a pdf page in ITextSharp?
Whats the correct way to define a background image for a pdf page? Is there a property of the document I set?
Or is it just like creating another image(my image has the dimensions of a A4 page)? If I add the background image as a normal image will I be able to place paragraphs over the top of the background image?
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWrite object, writing the output to a MemoryStream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/test.jpg"));
logo.SetAbsolutePosition(0,0);
document.Add(logo);
// Will the following paragraph be ON TOP or below the background image?
// I am aiming for on top
document.Add( new Paragraph("sjfkkjdsfk") );
document.Close();
The logo is fine - it's just a regular image content on the page, not backgound img (like in HTML). To put content on top of that, you need to use Direct Content:
PdfContentByte over = writer.DirectContent;
over.SaveState();
over.BeginText();
over.SetFontAndSize(BaseFont.CreateFont(), 9);
over.ShowTextAligned(Element.ALIGN_LEFT, "Your Text", x, y, 0);
over.SetLineWidth(0.3f);
over.EndText();
over.RestoreState();
Please note the x and y cord are from the bottom left corner. The last parameter is for rotation.
Another tip: do a doc.NewPage(); if you want to start a new page with the image background, so the the text cord is on the new page.