TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Goran Bibic
482
2.9k
197.9k
Print multiple pages pdf itextsharp C#
Apr 29 2021 6:56 AM
My code is export data from data grid to one page, need to export multiple rows to each page
private
void
PrintButton_Click(
object
sender, EventArgs e)
{
//Creating iTextSharp Table from the DataTable data
//Console.WriteLine(PoreziDataGridView.ColumnCount); test za itext sharp
PdfPTable pdfTable =
new
PdfPTable(ListDataGridView.ColumnCount);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 100;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;
pdfTable.DefaultCell.BorderWidth = 1;
float
[] sirina =
new
float
[] { 0f, 20f, 20f, 60f, 25f, 25f };
pdfTable.SetWidths(sirina);
BaseFont bfCalibri = BaseFont.CreateFont(
"c:\\windows\\fonts\\calibri.ttf"
, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
iTextSharp.text.Font calibri =
new
iTextSharp.text.Font(bfCalibri, 8);
iTextSharp.text.Font calibri2 =
new
iTextSharp.text.Font(bfCalibri, 8, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.WHITE);
iTextSharp.text.Font calibri3 =
new
iTextSharp.text.Font(bfCalibri, 12);
//Adding Header row
foreach
(DataGridViewColumn column
in
ListDataGridView.Columns)
{
PdfPCell cell =
new
PdfPCell(
new
Phrase(column.HeaderText, calibri2));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_CENTER;
cell.BackgroundColor =
new
iTextSharp.text.BaseColor(243, 70, 5);
pdfTable.AddCell(cell);
}
//Adding DataRow
foreach
(DataGridViewRow row
in
ListDataGridView.Rows)
{
foreach
(DataGridViewCell cell
in
row.Cells)
{
PdfPCell cell2 =
new
PdfPCell(
new
Phrase(cell.Value.ToString(), calibri));
pdfTable.AddCell(cell2);
}
}
byte
[] Memorandum =
null
;
using
(SqlConnection openCon21 =
new
SqlConnection(Con))
using
(SqlCommand cmd21 =
new
SqlCommand())
{
cmd21.CommandText =
"select memorandum from podaci_o_korisniku"
;
cmd21.Connection = openCon21;
openCon21.Open();
using
(SqlDataReader reader21 = cmd21.ExecuteReader())
{
if
(reader21.Read() && !reader21.IsDBNull(0))
{
Memorandum = (
byte
[])reader21[0];
}
else
{
Memorandum =
null
;
}
}
}
if
(Memorandum ==
null
)
{
MessageBox.Show(
"Ucitajte zaglavlje kako biste odštampali dokument!"
+
"\n"
+
"Zaglavlje možete ucitati u:"
+
"\n"
+
"Šifarnici /1500 Osnovni podaci / 1501 Podaci o korisniku "
,
"Informacija"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Memorandum);
//Exporting to PDF
string
folderPath =
"C:\\Program Files\\BSS\\PDFs\\"
;
if
(!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using
(FileStream stream =
new
FileStream(folderPath +
"Porezi.pdf"
, FileMode.Create))
{
iTextSharp.text.Font calibriSubTitle =
new
iTextSharp.text.Font(bfCalibri, 10);
iTextSharp.text.Document pdfDoc =
new
iTextSharp.text.Document(PageSize.A4, 20f, 15f, 10f, 30f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
logo.ScaleAbsolute(550, 110);
logo.SetAbsolutePosition(25, 715);
pdfDoc.Add(logo);
pdfDoc.Add(
new
ListItem(
" "
));
pdfDoc.Add(
new
ListItem(
"Porezi "
, calibrinaslov));
pdfDoc.Add(
new
ListItem(
" "
));
PdfContentByte cb = writer.DirectContent;
PdfContentByte cb1 = writer.DirectContent;
cb.SetColorStroke(
new
BaseColor(41, 128, 185));
cb1.SetLineWidth(2);
cb1.RoundRectangle(20f, 711f, 560f, 120f, 10f);
cb1.Stroke();
pdfDoc.Add(pdfTable);
cb.SetColorStroke(
new
BaseColor(41, 128, 185));
cb.SetLineWidth(0.5);
cb.MoveTo(20f, 20f);
// linija donja slogan firme
cb.LineTo(585f, 20f);
cb.Stroke();
pdfDoc.Close();
stream.Close();
System.Diagnostics.Process.Start(folderPath +
"Porezi.pdf"
);
}
}
}
Reply
Answers (
2
)
RDP Viewer Blank When Host is Minimized
Asp.Net MVC Gridview Query