For smart Primates & ROBOTS (oh and ALIENS ).

Showing posts with label itextSharp. Show all posts
Showing posts with label itextSharp. Show all posts

Thursday, January 08, 2015

Paragraph overlapping problem in itextsharp

Problem:

If you are working with itextSharp in Dot net winform C# or VB.net then you are very familiar with Paragraph. if  Paragraph font size is big then the Paragraph text will overlap as below example image.

Paragraph overlapping problem in itextsharp
Paragraph overlapping problem in itextsharp


So  how to overcome this problem?

Solution: 

To overcome paragraph text overlapping problem in itextsharp you will use  Paragraph SpacingAfter property.

Example:

Use below C# code, you can convert it into VB.net code if you are working in VB.net.

Paragraph para= new Paragraph("This is a test");
para.Alignment = Element.ALIGN_CENTER;
para.SpacingAfter=50f;
doc.Add(para);
Share:

Wednesday, September 24, 2014

use Table in itextsharp C#

use Table in itextsharp C#





iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4, 6, 72, 20, 16);
doc.Open();
PdfWriter docWriter = PdfWriter.GetInstance(doc, new FileStream(PdfName, FileMode.Create));
docWriter.StrictImageSequence = true;

PdfPTable table = new PdfPTable(1);
table.TotalWidth = 550f;
table.LockedWidth = true;

for(n records) {
    PdfPCell cell = new PdfPCell(new Phrase(""));
        cell.AddElement(pdfImage);
        cell.BorderColor = BaseColor.WHITE;
        table.AddCell(cell);
}
doc.Add(table);
doc.Close();
Share:

Thursday, August 14, 2014

Watermark and heading in existing PDF Using itextSharp C#

Watermark and heading in existing PDF Using itextSharp C#

In recently my project of Learning Management System,ICT training programmes,academic software modules and teaching application software/technologies for researchers in the institutions of higher education
in Dot Net, I have faced a problem to Add Watermark and heading in existing PDF Using itextSharp C# at the same time. I have solved this problem. So If you are looking for the solution you can use my code.


  string sourceFile="c://aa.pdf", string outputFile="c://bb.pdf", string watermarkText="Heading Water Mark";
            iTextSharp.text.pdf.PdfReader reader = null;
            iTextSharp.text.pdf.PdfStamper stamper = null;
            iTextSharp.text.pdf.PdfGState gstate = null;
            iTextSharp.text.pdf.PdfContentByte contentByte = null;
            iTextSharp.text.Rectangle rect = null;
            int pageCount = 0;
            BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
            iTextSharp.text.Font black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.UNDEFINED, 30f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.GRAY);
            float fontSize = 50, xPosition = 300, yPosition = 400, angle = 55;
            try
            {
                reader = new iTextSharp.text.pdf.PdfReader(sourceFile);
                rect = reader.GetPageSizeWithRotation(1);
                stamper = new iTextSharp.text.pdf.PdfStamper(reader, new System.IO.FileStream(outputFile, System.IO.FileMode.Create));
                gstate = new iTextSharp.text.pdf.PdfGState();
                pageCount = reader.NumberOfPages;
                for (int i = 1; i <= pageCount; i++)
                {
                    contentByte = stamper.GetOverContent(i);
                    contentByte.SaveState();
                    contentByte.SetGState(gstate);
                    if (i == 1)
                    {
                        PdfPTable table = new PdfPTable(1);
                        float[] widths = new float[] { 500f };
                        table.SetWidths(widths);
                        table.TotalWidth = 600f;
                        table.LockedWidth = true;
                        Chunk chk_subject = new Chunk("My Heading Text", black);
                        Phrase ph_subject = new Phrase();
                        ph_subject.Add(chk_subject);
                        PdfPCell cell = new PdfPCell(ph_subject);
                        cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                        cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
                        cell.BorderWidthLeft = 1f;
                        cell.BorderWidthRight = 1f;
                        cell.BorderWidthTop = 1f;
                        cell.BorderWidthBottom = 1f;
                        cell.BorderColor = BaseColor.WHITE;
                        table.AddCell(cell);
                        table.WriteSelectedRows(0, -1, 0, 690, stamper.GetOverContent(1));
                    }
                    gstate.FillOpacity = (float)0.3;
                    gstate.StrokeOpacity = (float)0.3;
                    contentByte.SetGState(gstate);
                    contentByte.BeginText();
                    contentByte.SetColorFill(BaseColor.LIGHT_GRAY);
                    contentByte.SetFontAndSize(baseFont, fontSize);
                    contentByte.SetTextMatrix(30, 30);
                    contentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
                    contentByte.EndText();
                }
                stamper.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            MessageBox.Show("Done");

Share:

Friday, November 15, 2013

image quality in iTextSharp

How to improve image quality in iTextSharp.

I was working in question paper project. in this project I need to generate PDF file from images. but when I generated the PDF file, image's quality was so bad. so to overcome this problem I have used listed below code.


 System.Drawing.Image image = System.Drawing.Image.FromFile("myimage.jpg");
 iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4);
 doc.Open();
 doc.Add(new Paragraph(""));
 PdfWriter.GetInstance(doc, new FileStream("my.pdf", FileMode.Create));
 iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
pdfImage.ScalePercent(50f);
doc.Add(pdfImage);
doc.Close();

Share:

Ads Inside Post

Powered by Blogger.

Archive