I'm using iTextSharp 5 to generate a clothing label as a pdf file. I need to create a second pdf rather than creating a new page if the page height is exceeded. Is it possible? How to do this? Please help.
This is my code. I needed to continue this multi language text to an another pdf if it exceeds the page height. Page length is 110 mm. Sometimes multi language text is long.
This is the generated pdf:
protected void btnOpenLabel_Click(object sender, EventArgs e)
{
//BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
//iTextSharp.text.Font titreProgFont = FontFactory.GetFont("HELVETICA", 7, Font.NORMAL,BaseColor.RED);
string fireWarning = "KEEP AWAY FROM FIRE";
string madeinText = "Made in the U.K.";
string sizeuk = "UK";
string multiLanguageText = "This is a long multi language text. This should be continue to next page if the page height exceeded. This should happen automatically when using a very long text in pdfs";
string lastLine = "Edge";
string nextpageText="This text should be placed in next page";
try
{
FileStream fs = new FileStream(Server.MapPath("pdf")+"\\"+ "MnSLabel"+".pdf",FileMode.Create);
float xpointValue = iTextSharp.text.Utilities.MillimetersToPoints(20);
float ypointvalue = iTextSharp.text.Utilities.MillimetersToPoints(110);
Rectangle pageSize = new Rectangle(xpointValue, ypointvalue);
Document document = new Document(pageSize, 0, 0, 3, 0);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.AddAuthor("Chathu");
document.Open();
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
//first text by method calling
iTextSharp.text.Font firewarningFont = FontFactory.GetFont("HELVETICA", 6, Font.NORMAL, BaseColor.RED);
Chunk c1 = new Chunk();
Phrase textPhrase = new Phrase();
ColumnText ct1 = new ColumnText(cb);
SetPDFColumnText(ct1,fireWarning,firewarningFont,c1,textPhrase,4,8,17,90,cb,5,Element.ALIGN_CENTER,Element.ALIGN_CENTER);
//end fire warning by method calling
//made in text method calling
iTextSharp.text.Font madein_textFont = FontFactory.GetFont("HELVETICA",4,Font.NORMAL,BaseColor.BLACK);
SetPDFColumnText(ct1,madeinText,madein_textFont,c1,textPhrase,2,4,18,94,cb,5,Element.ALIGN_CENTER,Element.ALIGN_CENTER);
//end made in method calling
//sizes uk
iTextSharp.text.Font sizeUKFont = FontFactory.GetFont("HELVETICA", 4, Font.NORMAL, BaseColor.BLACK);
SetPDFColumnText(ct1,sizeuk,sizeUKFont,c1,textPhrase,5,45,15,84,cb,1,Element.ALIGN_CENTER,Element.ALIGN_CENTER);
//end size uk
SetPDFColumnText(ct1, multiLanguageText , sizeUKFont, c1, textPhrase, 0, 2, 20, 1, cb, 1, Element.ALIGN_CENTER, Element.ALIGN_CENTER);
cb.EndText();
//care symbols start
PdfReader bgReader = new PdfReader(Server.MapPath("attachments") + "\\" + "CL1.pdf");
PdfImportedPage bg = writer.GetImportedPage(bgReader, 1);
float Scale = 0.25f;
cb.AddTemplate(bg, Scale, 0, 0, Scale, 2, 130);
//care symbols end
//FIBER content
document.Close();
writer.Close();
fs.Close();
lblSuccess.Text = "Sample M&S Label saved to folder";
}
catch (Exception ex)
{
lblSuccess.Text = ex.Message;
}
}
public void SetPDFColumnText(ColumnText ct, string pdfText, iTextSharp.text.Font pdfFont,Chunk pdfChunk,Phrase pdfPhrase,float lowerleft_x,float lowerleft_y,float upperright_x,float upperright_y,PdfContentByte cb,float textLeading,int lineAlignment,int textAlignment) {
float edge_lowerLeftY = 4;
float edge_upperRightY = 2;
int pdfCounter = 1;
edge_lowerLeftY = iTextSharp.text.Utilities.MillimetersToPoints(edge_lowerLeftY);
edge_upperRightY = iTextSharp.text.Utilities.MillimetersToPoints(edge_upperRightY);
//ColumnText ct1 = new ColumnText(cb);
lowerleft_x = iTextSharp.text.Utilities.MillimetersToPoints(lowerleft_x);
lowerleft_y = iTextSharp.text.Utilities.MillimetersToPoints(lowerleft_y);
upperright_x = iTextSharp.text.Utilities.MillimetersToPoints(upperright_x);
upperright_y = iTextSharp.text.Utilities.MillimetersToPoints(upperright_y);
}
