What i am doing is i am creating one simple html from c# side and tried to convert it in pdf using itextSharp
Here is the sample code of this
Document pdfDoc = new Document(new Rectangle(288f, 144f), 2, 2, 2, 2);
pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
return File(stream.ToArray(), "application/pdf", "CONTRACTREGISTER.pdf");
the issue is as below :
When i have checked my html in browser in print preview it is displayed perfectly as show in below image
now when i process this html with itextsharp it does not preserve my column width setting , please check below image of output of itext sharp
see the difference of columns width for both image
you can use PdfPTable object and set fixed width float array for the columns' width, like this:
PdfPTable table = new PdfPTable(4);
table.TotalWidth = 500f;
table.LockedWidth = true;
float[] widths = new float[] { 20f, 60f, 60f, 30f};
table.SetWidths(widths);
if this didn't do what you need and you need to set column width by percent, then you can use PDFflow library and set the column width like this:
DocumentBuilder.New().AddSection().AddTable()
// add 4 columns to the table
.AddColumnPercent("", 10).ToTable()
.AddColumnPercent("", 50).ToTable()
.AddColumnPercent("", 20).ToTable()
.AddColumnPercent("", 20).ToTable()
// add the first row
.AddRow()
// add the first cell and make it span 4 rows
.AddCellToRow()
.AddCellToRow()
.AddCellToRow()
.AddCellToRow()
// generate the PDF document
.ToDocument().Build("Result.pdf");
the result will be this:
Related
I have two parts to my java project.
I need to populate the fields of a pdf
I need to add a table below the populated section on the blank area of the page (and this table needs to be able to rollover to the next page).
I am able to do these things separately (populate the pdf and create a table). But I cannot effectively merge them. I have tried doing a doc.add(table) which will result in the table being on the next page of the pdf, which I don't want.
I essentially just need to be able to specify where the table starts on the page (so it wouldn't overlap the existing content) and then stamp the table onto the existing pdf.
My other option if this doesn't work is trying to add fields to the original pdf that will be filled by the table contents (so it will instead be a field-based table).
Any suggestions?
EDIT:
I'm new to iText and have not used columntext before, but I'm trying to test it out in the following code but the table is not being displayed. I looked at other columntext examples and I have not seen exactly where the columntext is added back into the pdf.
//CREATE FILLED FORM PDF
PdfReader reader = new PdfReader(sourcePath);
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream(destPath));
pdfStamper.setFormFlattening(true);
AcroFields form = pdfStamper.getAcroFields();
form.setField("ID", "99999");
form.setField("ADDR1", "425 Test Street");
form.setField("ADDR2", "Test, WA 91334");
form.setField("PHNBR", "(999)999-9999");
form.setField("NAME", "John Smith");
//CREATE TABLE
PdfPTable table = new PdfPTable(3);
Font bfBold12 = new Font(FontFamily.HELVETICA, 12, Font.BOLD, new BaseColor(0, 0, 0));
insertCell(table, "Table", Element.ALIGN_CENTER, 1, bfBold12);
table.completeRow();
ColumnText column = new ColumnText(pdfStamper.getOverContent(1));
column.addElement(table);
pdfStamper.close();
reader.close();
Please take a look at the AddExtraTable example. It's a simplification of the AddExtraPage example written in answer to the question How to continue field output on a second page?
That question is almost an exact duplicate of your question, with as only difference the fact that your requirement is easier to achieve.
I simplified the code like this:
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
Rectangle pagesize = reader.getPageSize(1);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField("Name", "Jennifer");
form.setField("Company", "iText's next customer");
form.setField("Country", "No Man's Land");
PdfPTable table = new PdfPTable(2);
table.addCell("#");
table.addCell("description");
table.setHeaderRows(1);
table.setWidths(new int[]{ 1, 15 });
for (int i = 1; i <= 150; i++) {
table.addCell(String.valueOf(i));
table.addCell("test " + i);
}
ColumnText column = new ColumnText(stamper.getOverContent(1));
Rectangle rectPage1 = new Rectangle(36, 36, 559, 540);
column.setSimpleColumn(rectPage1);
column.addElement(table);
int pagecount = 1;
Rectangle rectPage2 = new Rectangle(36, 36, 559, 806);
int status = column.go();
while (ColumnText.hasMoreText(status)) {
status = triggerNewPage(stamper, pagesize, column, rectPage2, ++pagecount);
}
stamper.setFormFlattening(true);
stamper.close();
reader.close();
}
public int triggerNewPage(PdfStamper stamper, Rectangle pagesize, ColumnText column, Rectangle rect, int pagecount) throws DocumentException {
stamper.insertPage(pagecount, pagesize);
PdfContentByte canvas = stamper.getOverContent(pagecount);
column.setCanvas(canvas);
column.setSimpleColumn(rect);
return column.go();
}
As you can see, the main differences are:
We create a rectPage1 for the first page and a rectPage2 for page 2 and all pages that follow. That's because we don't need a full page on the first page.
We don't need to load a PdfImportedPage, instead we're just adding blank pages of the same size as the first page.
Possible improvements: I hardcoded the Rectangle instances. It goes without saying that rect1Page depends on the location of your original form. I also hardcoded rect2Page. If I had more time, I would calculate rect2Page based on the pagesize value.
See the following questions and answers of the official FAQ:
How to add a table on a form (and maybe insert a new page)?
How to continue field output on a second page?
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 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?
I have a very tall asp chart(PNG file), let's say 4000px in height. When the pdf file is generated using iTextSharp, it has only one page and i can see only 25-30% of my chart. I don't want to scale the image to fit into one page. I want to spread/split my chart on multiple pages so I can see the chart's details. If my chart fits into four pages, the PDF also needs to have four pages.
In the image bellow you can see my chart, how I want it (spread on 6 pages), and how it actualy looks generated by iTextSharp (the top of the chart displayed on one single page).
Image
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
using (MemoryStream stream = new MemoryStream())
{
Chart1.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(70f);// This solves the width of the chart
pdfDoc.Add(chartImage);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
You are creating a document with A4 pages in landscape. That means that you can scale the width of your image to 842pt, so instead of using ScalePercent(70f), you could use ScaleToFit(842f, 100000f);
Note that I'm choosing 100000f for the height as an arbitrary high number. I'm only interested in the width because you want to fit the image on A4 pages.
Once you've scaled the image, you need to know the scaled height: chartImage.ScaledHeight;
Suppose this height is h. Then you need to divide h by 595. That's the height of your pages. Unless h is an exact multiple of 595, ou will need (h / 595) + 1 pages to show the complete chart.
Suppose this value is p (the number of pages that are necessary). In that case, you need to create a loop from 1 to p, and add the same image and trigger a new page over and over again.
However: before adding the image, you need to change its absolute position: chartImage.setAbsolutePostion(0, (p - i) * 595);
I don't know how to write C# (I'm a Java developer, actually the original developer of iText), but I'm sure you can cook yourself an example with this info.
This solution saved my day, thanks to Bruno.
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
using (MemoryStream stream = new MemoryStream())
{
Chart1.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScaleToFit(822f, 10000f); // 822 instead of 842 because I have 20f padding (10 + 10) at first line
int pageNbr = Convert.ToInt16(Math.Truncate(chartImage.ScaledHeight / 595));
int p = 0;
if (chartImage.ScaledHeight % 595 == 0)
p = pageNbr;
else
p = pageNbr + 1;
for (int i = 1; i <= p; i++)
{
pdfDoc.NewPage();
chartImage.SetAbsolutePosition(10, -(p-i)*595);
pdfDoc.Add(chartImage);
}
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
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();
}