1. 程式人生 > >利用iTextSharp實現分頁功能

利用iTextSharp實現分頁功能

string[] htmlTextArr=Regex.Split(htmlText,"##",RegexOptions.IgnoreCase);
MemoryStream outputStream = new MemoryStream();
Document doc = new Document();
PdfWriter writer =PdfWriter.GetInstance(doc,outputStream);
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ,0,doc.PageSize.Height,1f);
doc.open();
for(int i=0;i<htmlTextArr.Length;i++){
	byte[] data=Encoding.UTF8.GetBytes(htmlTextArr[i]);
	MemoryStream msInput =new MemoryStream(data);
	XMLWorkerHelper.GetInstance().ParseXHtml(writer,doc,msInput,null,Encoding.UTF8,new UnicodeFontFactory());
	if(i<htmlTextArr.Length-1){
		doc.NewPage();
	}
	if(i==htmlTextArr.Length-1){
		PdfAction action =PdfAction.GotoLocalPage(1,pdfDest,writer);
		writer.SetOpenAction(action);
		doc.Close();
		msInput.Close();
		outputStream.Close();
	}
}