ShowText() must be between BeginText()/EndText(). ColumnText can't be between 
BeginText()/EndText().

Paulo 

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of Terje Kristensen
> Sent: Thursday, November 22, 2007 11:01 AM
> To: [email protected]
> Subject: [iText-questions] Illegal operation 'BT' inside a 
> text object.
> 
> Hi, 
> 
>  
> 
> I am trying to make a multi-page PDF using iTextSharp. But I 
> get the error: “Illegal operation 'BT' inside a text object.” 
> When trying to open it in Adobe Reader 6.0, and a general 
> error in Adobe Reader 8: “An error exists on this page. 
> Acrobat may not display correctly. Please contact the person 
> who created the PDF document to correct the problem.”
> 
>  
> 
> I’m not sure how much code I should add here, so I have 
> stripped most of the “no-iText”-code. My apologies if I have 
> included to much code.
> 
>  
> 
> Can someone please help me?
> 
>  
> 
> Best regards
> 
>  
> 
> Terje Kristensen
> 
>  
> 
> CODE:
> 
>  
> 
>     protected void Page_Load(object sender, EventArgs e)
> 
>     {
> 
>         Document pdfDocument = new Document();
> 
>         pdfDocument.SetMargins(2, 2, 10, 2);
> 
>         PdfWriter writer = PdfWriter.GetInstance(pdfDocument, 
> Response.OutputStream);
> 
>  
> 
>         Response.ContentType = "application/pdf";
> 
>         pdfDocument.Open();
> 
>  
> 
>         PdfReader reader = new PdfReader(appFolder + "foo.pdf");
> 
>         PdfImportedPage page1 = writer.GetImportedPage(reader, 1);
> 
>         String fontsDirName = 
> (String)Microsoft.Win32.Registry.CurrentUser.
> 
>     OpenSubKey("Software").
> 
>     OpenSubKey("Microsoft").
> 
>     OpenSubKey("Windows").
> 
>     OpenSubKey("CurrentVersion").
> 
>     OpenSubKey("Explorer").
> 
>     OpenSubKey("Shell Folders").
> 
>     GetValue("Fonts");
> 
>         FontFactory.RegisterDirectory(fontsDirName);
> 
>         BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, 
> BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
> 
>  
> 
>         writer.DirectContent.AddTemplate(page1, .5f, .5f);
> 
>         writer.DirectContent.BeginText();
> 
>         writer.DirectContent.SetFontAndSize(bf, 14);
> 
>         using (SqlConnection dbConnection = 
> DBAccess.GetSQLConnection())
> 
>         {
> 
>             try
> 
>             {
> 
>                 dbConnection.Open();
> 
>                 SqlCommand command = new SqlCommand(SQLQuery);
> 
>                 command.Connection = dbConnection;
> 
>                 SqlDataReader Reader = command.ExecuteReader();
> 
>                 while (Reader.Read())
> 
>                 {
> 
>                     priceCategory = Reader.IsDBNull(24) ? 0 : 
> Reader.GetInt32(24);
> 
>                     writer.DirectContent.SetTextMatrix(130, 800);
> 
>                     
> writer.DirectContent.ShowText(CabinNo.Substring(3, 
> CabinNo.Length - 3) + " " + Reader.GetString(19) + " på " + 
> Reader.GetString(2));
> 
>  
> 
>                     ColumnText CADesc = new 
> ColumnText(writer.DirectContent);
> 
>                     CADesc.SetSimpleColumn(130, 600, 570, 
> 760, 8, Element.ALIGN_LEFT);
> 
>                     CADesc.AddText(new 
> Phrase(Reader.GetString(2) + "\n", 
> FontFactory.GetFont("Calibri", 11, Font.BOLD)));
> 
>                     CADesc.AddText(new 
> Phrase(Reader.GetString(4) + "\n", 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>                     CADesc.Go();
> 
>  
> 
>                     ColumnText CInfo = new 
> ColumnText(writer.DirectContent);
> 
>                     CInfo.SetSimpleColumn(132, 490, 570, 618, 
> 8, Element.ALIGN_LEFT);
> 
>                     CInfo.AddText(new 
> Chunk(Reader.GetString(9) + "\n", 
> FontFactory.GetFont("Calibri", 9, Font.ITALIC)));
> 
>                     CInfo.Go();
> 
>  
> 
>                     ColumnText CDesc = new 
> ColumnText(writer.DirectContent);
> 
>                     CDesc.SetSimpleColumn(130, 400, 570, 480, 
> 8, Element.ALIGN_LEFT);
> 
>                     CDesc.AddText(new 
> Phrase(Reader.GetString(19) + "\n", 
> FontFactory.GetFont("Calibri", 11, Font.BOLD)));
> 
>                     CDesc.AddText(new 
> Phrase(Reader.GetString(10) + "\n", 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>                     CDesc.Go();
> 
>  
> 
>                     ColumnText CInventory = new 
> ColumnText(writer.DirectContent);
> 
>                     CInventory.SetSimpleColumn(130, 320, 570, 
> 390, 8, Element.ALIGN_LEFT);
> 
>                     CInventory.AddText(new 
> Phrase("Inventar\n", FontFactory.GetFont("Calibri", 11, Font.BOLD)));
> 
>                     CInventory.AddText(new 
> Phrase(Reader.GetString(12) + "\n", 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>                     CInventory.Go();
> 
>  
> 
>                     ColumnText CDirections = new 
> ColumnText(writer.DirectContent);
> 
>                     CDirections.SetSimpleColumn(130, 250, 
> 570, 310, 8, Element.ALIGN_LEFT);
> 
>                     CDirections.AddText(new 
> Phrase("Veibeskrivelse\n", FontFactory.GetFont("Calibri", 11, 
> Font.BOLD)));
> 
>                     CDirections.AddText(new 
> Phrase(Reader.GetString(11) + "\n", 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>                     CDirections.Go();
> 
>  
> 
>                     ColumnText CParking = new 
> ColumnText(writer.DirectContent);
> 
>                     CParking.SetSimpleColumn(130, 190, 570, 
> 240, 8, Element.ALIGN_LEFT);
> 
>                     CParking.AddText(new 
> Phrase("Parkering\n", FontFactory.GetFont("Calibri", 11, Font.BOLD)));
> 
>                     CParking.AddText(new 
> Phrase(Reader.GetString(14) + "\n", 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>                     CParking.Go();
> 
>  
> 
>                     PdfPTable InfoTable = new PdfPTable(2);
> 
>                     float[] InfoCellWidth = new float[2];
> 
>                     InfoCellWidth[0] = 45F;
> 
>                     InfoCellWidth[1] = 45F;
> 
>                     InfoTable.SetTotalWidth(InfoCellWidth);
> 
>                     InfoTable.AddCell(new PdfPCell(new 
> Phrase("Standard", FontFactory.GetFont("Calibri", 9, Font.BOLD))));
> 
>                     InfoTable.AddCell(new PdfPCell(new 
> Phrase(Reader.GetString(13), FontFactory.GetFont("Calibri", 
> 8, Font.NORMAL))));
> 
>  
> 
>                     InfoTable.AddCell(new PdfPCell(new 
> Phrase("Sesong", FontFactory.GetFont("Calibri", 9, Font.BOLD))));
> 
>                     InfoTable.AddCell(new PdfPCell(new 
> Phrase(Reader.GetString(6), FontFactory.GetFont("Calibri", 8, 
> Font.NORMAL))));
> 
>  
> 
>                     InfoTable.AddCell(new PdfPCell(new 
> Phrase("Type", FontFactory.GetFont("Calibri", 9, Font.BOLD))));
> 
>                     InfoTable.AddCell(new PdfPCell(new 
> Phrase(Reader.GetString(7), FontFactory.GetFont("Calibri", 8, 
> Font.NORMAL))));
> 
>  
> 
>                     string AntSenger = Reader.IsDBNull(8) ? 
> "mangler" : Reader.GetDecimal(8).ToString();
> 
>                     InfoTable.AddCell(new PdfPCell(new 
> Phrase("Senger", FontFactory.GetFont("Calibri", 9, Font.BOLD))));
> 
>                     InfoTable.AddCell(new PdfPCell(new 
> Phrase(AntSenger, FontFactory.GetFont("Calibri", 8, Font.NORMAL))));
> 
>  
> 
>                     InfoTable.WriteSelectedRows(0, -1, 22, 
> 800, writer.DirectContent);
> 
>  
> 
>  
> 
>                 }
> 
>  
> 
>  
> 
>             }
> 
>             catch (SqlException se)
> 
>             {
> 
>                 throw se;
> 
>             }
> 
>         }
> 
>  
> 
>         PdfPTable FooterTable = new PdfPTable(3);
> 
>         FooterTable.DefaultCell.Border = 0;
> 
>         float[] FooterCellWidth = new float[3];
> 
>         FooterCellWidth[0] = 105F;
> 
>         FooterCellWidth[1] = 345F;
> 
>         FooterCellWidth[2] = 105F;
> 
>         FooterTable.SetTotalWidth(FooterCellWidth);
> 
>         PdfPCell dateCell = new PdfPCell(new 
> Phrase(DateTime.Now.ToShortDateString(), 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>         dateCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>         dateCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>  
> 
>         PdfPCell delfiCell = new PdfPCell(new 
> Phrase("www.cabinweb.no - en hyttesystemløsning utviklet av 
> Delfi Data AS", FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>         delfiCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>         delfiCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>         int sideNo = 1;
> 
>         PdfPCell pageCell = new PdfPCell(new Phrase("Side " + 
> sideNo, FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>         pageCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>         pageCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>         pageCell.Padding = 3;
> 
>         FooterTable.AddCell(dateCell);
> 
>         FooterTable.AddCell(delfiCell);
> 
>         FooterTable.AddCell(pageCell);
> 
>         FooterTable.WriteSelectedRows(0, -1, 15, 18, 
> writer.DirectContent);
> 
>  
> 
>         Paragraph p = new Paragraph();
> 
>         if (priceCategory != 0)
> 
>         {
> 
>             ColumnText PTHead = new ColumnText(writer.DirectContent);
> 
>             PTHead.SetSimpleColumn(130, 180, 570, 190, 8, 
> Element.ALIGN_LEFT);
> 
>             PTHead.AddText(new Phrase("Pristabell\n", 
> FontFactory.GetFont("Calibri", 11, Font.BOLD)));
> 
>             PTHead.Go();
> 
>  
> 
>             iTextSharp.text.pdf.PdfPTable table = 
> drawPriceTable(priceCategory);
> 
>             table.WriteSelectedRows(0, -1, 135, 180, 
> writer.DirectContent);
> 
>         }
> 
>          writer.DirectContent.EndText();
> 
>         // next pages with pictures
> 
>         pdfDocument.NewPage();
> 
>         writer.DirectContent.BeginText();
> 
>         PdfReader reader2 = new PdfReader(appFolder + "foo2.pdf");
> 
>         PdfImportedPage page2 = writer.GetImportedPage(reader2, 1);
> 
>         writer.DirectContent.AddTemplate(page2, .5f, .5f);
> 
>  
> 
>         DirectoryInfo dirInfo = new 
> DirectoryInfo(Server.MapPath("~" + catalogue));
> 
>         bool FilesInFolder = true;
> 
>         try
> 
>         {
> 
>             FileInfo[] FilledFileInfo = dirInfo.GetFiles("*.jpg");
> 
>         }
> 
>         catch (Exception)
> 
>         {
> 
>             FilesInFolder = false;
> 
>         }
> 
>         FileInfo[] fileInfo = FilesInFolder ? 
> dirInfo.GetFiles("*.jpg") : new FileInfo[0];
> 
>     
> 
>         for (int i = 0; i < fileInfo.Length; i++)
> 
>         {
> 
>             int remainder = 0;
> 
>             int mod = Math.DivRem((i + 1), 4, out remainder); ;
> 
>             if (remainder == 0)
> 
>             {
> 
>                 FooterTable = new PdfPTable(3);
> 
>                 FooterTable.SetTotalWidth(FooterCellWidth);
> 
>                 dateCell = new PdfPCell(new 
> Phrase(DateTime.Now.ToShortDateString(), 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>                 dateCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>                 dateCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>  
> 
>                 delfiCell = new PdfPCell(new 
> Phrase("www.cabinweb.no - en hyttesystemløsning utviklet av 
> Delfi Data AS", FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>                 delfiCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>                 delfiCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>                 sideNo++;
> 
>                 pageCell = new PdfPCell(new Phrase("Side " + 
> sideNo, FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>                 pageCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>                 pageCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>                 pageCell.Padding = 3;
> 
>                 FooterTable.AddCell(dateCell);
> 
>                 FooterTable.AddCell(delfiCell);
> 
>                 FooterTable.AddCell(pageCell);
> 
>                 FooterTable.WriteSelectedRows(0, -1, 15, 18, 
> writer.DirectContent);
> 
>  
> 
>                 writer.DirectContent.EndText();
> 
>                 // next pages with pictures
> 
>                 pdfDocument.NewPage();
> 
>                 writer.DirectContent.BeginText();
> 
>                 writer.DirectContent.AddTemplate(page2, .5f, .5f);
> 
>             }
> 
>             Uri bildeurl = new Uri(appFolder + catalogue + 
> "/" + fileInfo[i].ToString());
> 
>             iTextSharp.text.Image image = 
> iTextSharp.text.Image.GetInstance(bildeurl);
> 
>             image.Alignment = Element.ALIGN_CENTER;
> 
>             image.ScaleToFit(300, 300);
> 
>             if (i == 0)
> 
>             {
> 
>                 image.SetAbsolutePosition(130F, 590);
> 
>             }
> 
>             else
> 
>             {
> 
>                 image.SetAbsolutePosition(130F, 590 - (270 * 
> (remainder - 1)));
> 
>             }
> 
>             writer.DirectContent.AddImage(image);
> 
>  
> 
>  
> 
>         }
> 
>         FooterTable = new PdfPTable(3);
> 
>         FooterTable.SetTotalWidth(FooterCellWidth);
> 
>         dateCell = new PdfPCell(new 
> Phrase(DateTime.Now.ToShortDateString(), 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>         dateCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>         dateCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>  
> 
>         delfiCell = new PdfPCell(new Phrase("www.cabinweb.no 
> - en hyttesystemløsning utviklet av Delfi Data AS", 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>         delfiCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>         delfiCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>         sideNo++;
> 
>         pageCell = new PdfPCell(new Phrase("Side " + sideNo, 
> FontFactory.GetFont("Calibri", 9, Font.NORMAL)));
> 
>         pageCell.VerticalAlignment = Element.ALIGN_MIDDLE;
> 
>         pageCell.HorizontalAlignment = Element.ALIGN_CENTER;
> 
>         pageCell.Padding = 3;
> 
>         FooterTable.AddCell(dateCell);
> 
>         FooterTable.AddCell(delfiCell);
> 
>         FooterTable.AddCell(pageCell);
> 
>         FooterTable.WriteSelectedRows(0, -1, 15, 18, 
> writer.DirectContent);
> 
>  
> 
>         writer.DirectContent.EndText();
> 
>  
> 
>         pdfDocument.Close();
> 
>     }
> 
>  
> 
> 


Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter 
informação confidencial ou legalmente protegida. A incorrecta transmissão desta 
mensagem não significa a perca de confidencialidade. Se esta mensagem for 
recebida por engano, por favor envie-a de volta para o remetente e apague-a do 
seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de 
usar, revelar ou distribuir qualquer parte desta mensagem. 

Disclaimer:
This message is destined exclusively to the intended receiver. It may contain 
confidential or legally protected information. The incorrect transmission of 
this message does not mean the loss of its confidentiality. If this message is 
received by mistake, please send it back to the sender and delete it from your 
system immediately. It is forbidden to any person who is not the intended 
receiver to use, distribute or copy any part of this message.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to