超出页面高度时创建新的pdf

时间:2019-07-01 08:57:22

标签: c# itext

我正在使用iTextSharp 5生成服装标签作为pdf文件。如果超出页面高度,我需要创建第二个pdf而不是创建新页面。可能吗?这该怎么做?请帮忙。

这是我的代码。如果超出页面高度,我需要继续将这种多语言文本转换为另一个pdf。页面长度为110毫米。有时多语言文字很长。

这是生成的pdf:

Resulting 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);
}

0 个答案:

没有答案