Vote count:
0
I am creating PdfPtables, populating them, and then adding them to a PDF doc using iTextSharp.
The first table works just fine:
PdfPTable tblHeadings = new PdfPTable(3);
tblHeadings.WidthPercentage = 100;
tblHeadings.SpacingBefore = 10f;
float[] headingRowWidths = new float[] { 550f, 50f, 400f };
tblHeadings.SetWidths(headingRowWidths);
tblHeadings.HorizontalAlignment = Element.ALIGN_LEFT;
Chunk sec1Heading = new Chunk("Section 1: Payment Information", timesRoman9BoldFont);
Paragraph parSectionHeading1 = new Paragraph();
parSectionHeading1.Add(sec1Heading);
PdfPCell cellSec1 = new PdfPCell(parSectionHeading1);
cellSec1.BackgroundColor = ucscgold; //lightGreen; // BaseColor.GREEN;
tblHeadings.AddCell(cellSec1);
// Blank cell
Chunk chunkBlank = new Chunk("");
Paragraph parBlank = new Paragraph();
parBlank.Add(chunkBlank);
PdfPCell cellBlank = new PdfPCell(parBlank);
cellBlank.BorderWidth = PdfPCell.NO_BORDER;
tblHeadings.AddCell(cellBlank);
Chunk sec2Heading = new Chunk("Section 2: Requester Information Section", timesRoman9BoldFont);
Paragraph parSectionHeading2 = new Paragraph();
parSectionHeading2.Add(sec2Heading);
PdfPCell cellSec2 = new PdfPCell(parSectionHeading2);
cellSec2.BackgroundColor = ucscgold; //lightGreen;
tblHeadings.AddCell(cellSec2);
doc.Add(tblHeadings);
But the second table, same width, but containing 7 columns instead of 3, fails with "General exception: cannot access a closed stream" What stream is being closed/has been closed here, though? The code is fundamentally the same:
// Table (single-row) for first row of controls
PdfPTable tblFirstRow = new PdfPTable(7);
tblFirstRow.SpacingBefore = 4f;
tblFirstRow.HorizontalAlignment =
Element.ALIGN_LEFT;
tblFirstRow.WidthPercentage = 100;
tblFirstRow.LockedWidth = true;
float[] FirstRowWidths = new float[] { 137f, 138f,
137f, 138f, 50f, 175f, 225f };
tblFirstRow.SetWidths(FirstRowWidths);
// "Required Date"
Chunk reqDate = new Chunk("Required Date: ",
timesRoman9Font);
Paragraph parReqDate = new Paragraph();
parReqDate.Add(reqDate);
PdfPCell cellReqDate = new PdfPCell(parReqDate);
cellReqDate.BorderWidth = PdfPCell.NO_BORDER;
//cellReqDate.HorizontalAlignment =
PdfPCell.ALIGN_CENTER;
//cellReqDate.VerticalAlignment =
PdfPCell.ALIGN_MIDDLE;
tblFirstRow.AddCell(cellReqDate);
PdfPCell cellReqDateTextBox = new PdfPCell()
{
CellEvent = new
DynamicTextbox("textBoxReqDate"),
Phrase = new Phrase("4/15/2015"),
FixedHeight = 16
// set borders, or other cell options here
};
tblFirstRow.AddCell(cellReqDateTextBox);
// "Payment Amount"
Chunk paymentAmount = new Chunk("Payment Amount: ",
timesRoman9Font);
Paragraph parPaymentAmount = new Paragraph();
parPaymentAmount.Add(paymentAmount);
PdfPCell cellPaymentAmount = new
PdfPCell(parPaymentAmount);
cellPaymentAmount.BorderWidth = PdfPCell.NO_BORDER;
tblFirstRow.AddCell(cellPaymentAmount);
PdfPCell cellPaymentAmountTextBox = new PdfPCell()
{
CellEvent = new
DynamicTextbox("textBoxPaymentAmount")
};
tblFirstRow.AddCell(cellPaymentAmountTextBox);
// Blank cell
Chunk chunkBlank2 = new Chunk("");
Paragraph parBlank2 = new Paragraph();
parBlank2.Add(chunkBlank2);
PdfPCell cellBlank2 = new PdfPCell(parBlank2);
cellBlank2.BorderWidth = PdfPCell.NO_BORDER;
tblFirstRow.AddCell(cellBlank2);
// "Requester Name"
Chunk requesterName = new Chunk("Requester Name: ",
timesRoman9Font);
Paragraph parRequesterName = new Paragraph();
parRequesterName.Add(requesterName);
PdfPCell cellRequesterName = new
PdfPCell(parRequesterName);
cellRequesterName.BorderWidth = PdfPCell.NO_BORDER;
tblFirstRow.AddCell(cellRequesterName);
PdfPCell cellRequesterNameTextBox = new PdfPCell()
{
CellEvent = new
DynamicTextbox("textBoxRequesterName")
};
tblFirstRow.AddCell(cellRequesterNameTextBox);
doc.Add(tblFirstRow);
Note: The "General exception: cannot access a closed stream" occurs within the most generic catch clause, the last one below:
catch (DocumentException dex)
{
MessageBox.Show(String.Format("DocumentException: {0}", dex.Message));
throw (dex);
}
catch (IOException ioex)
{
MessageBox.Show(String.Format("IOException: {0}", ioex.Message));
throw (ioex);
}
catch (Exception ex)
{
String exMsg = ex.Message;
MessageBox.Show(String.Format("General Exception: {0}", ex.Message));
}
asked 56 secs ago
Why/Where is this stream getting closed?
Aucun commentaire:
Enregistrer un commentaire