Itext7 Saving table margins if the table goes to the next page - c#

Can anybody give me a hand, i am traying to create a pdf file who has a table of employees inside, i get the employee data form a database, so the table could be occupied one o serveral pages of the document.
I set the margin of the table
table.SetMarginTop(100);
table.SetMarginBottom(20);
But as soon the employee data generates another page of the pdf documetn the margins get lost
Here is my complete code of how i am generating the pdf and some screenshots
internal bool CreatePdfInspectorDelegates(string storeName, DateTimePicker date, string inspector, DataTable delegateStoreDT, DataTable delegateEmployeeDT)
{try
{
string auxPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
auxPath = auxPath + #"\PDF\Creados\notifacionInspectorDelegados" + storeName + ".pdf";
PdfWriter writer = new PdfWriter(auxPath);
PageSize ps = PageSize.LETTER;
PdfDocument pdf = new PdfDocument(writer);
Document doc = new Document(pdf, ps);
//Fuentes de texto
PdfFont fontNormal = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
PdfFont fontBold = PdfFontFactory.CreateFont(StandardFonts.TIMES_BOLD);
//Datos
string dateIn = String.Format("Fecha{0}", date.Value.ToString("dd/MM/yyyy"));
string instpector = String.Format("{0}", date.Value.ToString("dd/MM/yyyy"));
//Cuerpo
Paragraph pTittel = new Paragraph();
pTittel.SetPaddingTop(70);
pTittel.SetFixedLeading(25);
Text text = new Text("NOTIFICACIÓN AL INSPECTOR O INSPECTORA DEL TRABAJO\n DE LA VOLUNTAD DE LOS TRABAJADORES Y LAS TRABAJADORAS\n DE ELEGIR A LOS DELEGADOS Y A LAS DELEGADAS DE PREVENCIÓN").SetFont(fontBold).SetTextAlignment(TextAlignment.CENTER);
pTittel.SetTextAlignment(TextAlignment.CENTER);
pTittel.Add(text);
Paragraph pBody = new Paragraph();
pBody.SetFixedLeading(22);
Text text2 = new Text("\nFecha: ").SetFont(fontNormal);
Text textDate = new Text(date.Value.ToString("dd/MM/yyyy")).SetFont(fontNormal).SetUnderline();
Text text3 = new Text("\n\nCiudadano(a):\n").SetFont(fontNormal);
Text textInspector = new Text(inspector).SetFont(fontNormal).SetUnderline();
Text text4= new Text("\n\nNosotros, los(as) trabajadores(as), en cumplimiento a lo señalado en el artículo 41 de la Ley Orgánica de Prevención, Condiciones y Medio Ambiente de Trabajo(Lopcymat) y del artículo 58 de su Reglamento Parcial, nos dirigimos a Usted con el objeto de manifestarle nuestra voluntad de elegir a los Delegados y / o Delegadas de Prevención correspondientes a la entidad de trabajo: ").SetFont(fontNormal);
Text textCenter = new Text("GRUPO TOTAL 99 C.A.").SetFont(fontBold).SetUnderline();
Text text5 = new Text(" cuya dirección es: ").SetFont(fontNormal);
Text textCenterAddress = new Text("CALLE LAS VEGAS CRUCE CON SOLEDAD EDIFICIO CLARIANT VENEZUELA, ZONA INDUSTRIAL DE LA TRINIDAD, CARACAS. ").SetFont(fontNormal).SetUnderline();
Text text6 = new Text(" , específicamente los correspondientes al centro de trabajo: ").SetFont(fontNormal);
Text textBranch = new Text(GetStoreBranch(delegateStoreDT).ToUpper()).SetFont(fontNormal).SetUnderline();
Text text7 = new Text(" , ubicado en: ").SetFont(fontNormal);
Text textBranchAddress = new Text(GetStoreAddress1(delegateStoreDT).ToUpper() + ", " + GetStoreAddress2(delegateStoreDT).ToUpper()).SetFont(fontNormal).SetUnderline();
Text text8 = new Text("\n\nNotificación que se hace para efectos del artículo 59 del Reglamento Parcial de la Ley Orgánica de Prevención, Condiciones y Medio Ambiente de Trabajo(RLopcymat).\n\nA continuación firman los(as) trabajadores(as) solicitantes:\n").SetFont(fontNormal);
pBody.Add(text2);
pBody.Add(textDate);
pBody.Add(text3);
pBody.Add(textInspector);
pBody.Add(text4);
pBody.Add(textCenter);
pBody.Add(text5);
pBody.Add(textCenterAddress);
pBody.Add(text6);
pBody.Add(textBranch);
pBody.Add(text7);
pBody.Add(textBranchAddress);
pBody.Add(text8);
pBody.SetTextAlignment(TextAlignment.JUSTIFIED);
doc.Add(pTittel);
doc.Add(pBody);
//Segunda pagina
doc.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
//Empleados
float[] columnWidths = { 5, 5, 5, 5, 4 };
Table table = new Table(UnitValue.CreatePercentArray(columnWidths));
table.SetWidth(UnitValue.CreatePercentValue(100));
Cell[] header =
{
new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Nombres y Apeliidos").SetTextAlignment(TextAlignment.CENTER).SetBold()),
new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Cedula\nIdentidad N°").SetTextAlignment(TextAlignment.CENTER).SetBold()),
new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Cargo Actual").SetTextAlignment(TextAlignment.CENTER).SetBold()),
new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Firma").SetTextAlignment(TextAlignment.CENTER).SetBold()),
new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Huella").SetTextAlignment(TextAlignment.CENTER).SetBold())
};
foreach (Cell cells in header)
{
table.AddCell(cells);
}
foreach (DataRow row in delegateEmployeeDT.Rows)
{
if (row[0].ToString() == "True")
{
table.AddCell(new Cell().SetMinHeight(80).SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(row[2].ToString().ToUpper() + "\n" + row[3].ToString().ToUpper()).SetTextAlignment(TextAlignment.CENTER)));
table.AddCell(new Cell().SetMinHeight(80).SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(row[4].ToString().ToUpper()).SetTextAlignment(TextAlignment.CENTER)));
table.AddCell(new Cell().SetMinHeight(80).SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(row[5].ToString().ToUpper()).SetTextAlignment(TextAlignment.CENTER)));
table.AddCell(new Cell().SetMinHeight(80).SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph("")));
table.AddCell(new Cell().SetMinHeight(80).SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph("")));
}
}
table.SetMarginTop(100);
table.SetMarginBottom(20);
doc.Add(table);
doc.Close();
return true;
}
catch (Exception e)
{
return false;
}
}

Top and bottom margins are not supposed to be applied to parts of the table that breaks across several pages. They only define space between previous element and current one (top margin) and between current element and next one (bottom margin).
If you want to preserve spacing on top of a table when it splits across pages you can use header and footer functionality. Table headers and footers are repeated on each page occupied by a table, and you can add a cell without borders with fixed height which will result in spacing on each page. Here is an example:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc, PageSize.A4.rotate());
doc.add(new Paragraph("Starting text"));
Table table = new Table(UnitValue.createPercentArray(5)).useAllAvailableWidth();
table.addHeaderCell(new Cell(1, 5).setHeight(100).setBorder(Border.NO_BORDER));
table.addHeaderCell(new Cell(1, 5).
add(new Paragraph("Header")));
table.addFooterCell(new Cell(1, 5).
add(new Paragraph("Footer")));
table.addFooterCell(new Cell(1, 5).setHeight(100).setBorder(Border.NO_BORDER));
for (int i = 0; i < 350; i++) {
table.addCell(new Cell().add(new Paragraph(String.valueOf(i + 1))));
}
doc.add(table);
doc.close();
Here is what the result looks like:

Related

couldnt find the path in merging pdf files in one single pdf document

im trying in c# to merge 2 pdf in a single one using the pdfsharp library, it shows an error that it couldnt find the path in the path where im going to save it, this is where i put my paths
string directorio = #"C:\Users\Usuario\Documents\PDF\";//Directorio donde buscara los archivos pdf
string destino = #"C:\Users\Usuario\Documents\Prueba\";//Directorio donde estara el archivo unido
string[] archivo = Directory.GetFiles(directorio);
Unir(destino, archivo);
if (MessageBox.Show("Espere un momento mientras su solicitud es procesada", "INFORMACION" + MessageBoxButtons.OK + MessageBoxIcon.Information) == DialogResult.OK)
{
MessageBox.Show("Su documento se ha unido con exito, fue enviado a la direccion: " + destino);
}
this is where my merge method
private void Unir(string outfilePath , string[] pdfFiles){
PdfDocument documento = new PdfDocument();
foreach(string pdfFile in pdfFiles)
{
PdfDocument inputDocumento = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
documento.Version = inputDocumento.Version;
foreach(PdfPage page in inputDocumento.Pages)
{
documento.AddPage(page);
}
documento.Options.CompressContentStreams = true;
documento.Options.NoCompression = false;
documento.Save(outfilePath);
}
}
A file name is needed to save a PDF file, not a folder.
The Save should be made after the foreach loop to save only once.

It's posible print PDF when PDF is open in memorystream?

Its posible open print dialog when is open in memorystram?
Actually I save the pdf in "~/App_Data/Rep_PDF/" and after show a pdf in an iframe but I cant do it because I recive a message :
"Not allowed to load local resource: file:///C:/Users/andres.martinez/Documents/TFS%20Monterrey%20Gob/InterfaceMonterrey%20PROD/Interface%20Monterrey%20PROD/InterfaceMonterrey/App_Data/Rep_PDF/Copia_C_Administrativa%2030-04-2019.pdf"
Could you help me How to print a pdf in asp mvc?
this is part of my code:
public ActionResult ImprimeReporte()
{
//Indicamos donde vamos a guardar el documento
string directorioRaiz = "~/App_Data/Rep_PDF/";
string NombreArchivoPDF = "C_Administrativa " + DateTime.Today.ToString("dd-MM-yyyy") + ".pdf";
string path_Original=Server.MapPath(directorioRaiz+NombreArchivoPDF);
string path_Copia = Server.MapPath(directorioRaiz + "Copia_"+NombreArchivoPDF);
if (System.IO.File.Exists(path_Original))
{
//SI EXISTE EL ARCHIVO EN LA CARPETA, LO MANDA A IMPRIMIR
Inicia_PrintScript(path_Original, path_Copia);
ViewData["path_Copia"] = path_Copia;
//Elimina los archivos , despues de que se imprimio
// System.IO.File.Delete(path_Original);
//System.IO.File.Delete(path_Copia);
}
else
{
//SI NO EXISTE MANDA LLAMAR AL METODO PARA DESCARGAR EL ARCHIVO Y DESPUES IMPRIMIRLO
Genera_Pdf_Administrativo();
Inicia_PrintScript(path_Original, path_Copia);
ViewData["path_Copia"] = path_Copia;
//Elimina los archivos , despues de que se imprimio
//System.IO.File.Delete(path_Original);
//System.IO.File.Delete(path_Copia);
}
return View();
}
second part
public static void Inicia_PrintScript(string Original, string Copia)
{
PdfReader reader = new PdfReader(Original);
PdfStamper stamper = new PdfStamper(reader, new FileStream(Copia, FileMode.Create));
AcroFields fields = stamper.AcroFields;
stamper.JavaScript = "this.print(true);\r";
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
View
<iframe src="#ViewData["path_Copia"].ToString()" id="myFrame" frameborder="0" style="border:0;" width="700" height="300"></iframe>
Well, it's possible to open a PDF with print dialog in another tab.
In your controller you need to get the file in memory just to return it, or generate the file on fly.
public FileResult PDFToReturn()
{
string filePath = Server.MapPath("Path_to_PDF");
return File(System.IO.File.ReadAllBytes(filePath), System.Web.MimeMapping.GetMimeMapping(filePath));
}
javascript does the magic.
function printPDF() {
let link = 'javascript:w=window.open("/Controller/PDFToReturn"); w.print();';
location.href = link;
}

iTextSharp edit PDF's based on SQL Query

I need to edit merging PDF's to have a colored bar on each PDF page based on a value in an SQL database where the PDF filename is also stored. I am not great with C# lists but thought perhaps I could build a supplemental list to the iTextSharp "PDFReader" list then when iterating through the PDFReader list write a conditional statement that says "if list 2 value = "Utilities" then create green square" during the PDF merge process.
protected void Page_Load(object sender, EventArgs e)
{
try
{
if ((Session["AccessLevel"].ToString() == "admin") || (Session["AccessLevel"].ToString() == "worker") || (Session["AccessLevel"].ToString() == "client"))
{
if (Request.QueryString["type"] == "QC")
{
//SqlDataSource2.Update();
}
else
{
}
string checkID = Request.QueryString["id"];
SqlDataReader rdr = null;
SqlConnection con2 = new SqlConnection(sqlConnection);
con2.Open();
//string sqlRowCount = "SELECT COUNT(*) FROM [Attachment] WHERE RequestId = '" + checkID + "' AND AttachType != 'Invoice' AND AttachType != 'Cover Sheet' ORDER BY AttachOrder ASC";
sqlUserName2 = "SELECT AttachmentName,AttachType FROM [Attachment] WHERE RequestId = '" + checkID + "' AND AttachType != 'Invoice' AND AttachType != 'Cover Sheet' ORDER BY AttachOrder ASC";
//SqlCommand cmd = new SqlCommand(sqlRowCount, con2);
//string count = cmd.ExecuteScalar().ToString();
SqlCommand cmd2 = new SqlCommand(sqlUserName2, con2);
rdr = cmd2.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
List<PdfReader> readerList = new List<PdfReader>();
List<string> pdfName = new List<string>();
foreach (DataRow row in dt.Rows)
{
PdfReader pdfReader = new PdfReader(Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/uploads/reports/" +
Convert.ToString(row[0])));
readerList.Add(pdfReader);
pdfName.Add(Convert.ToString(row[1]));
//pdfName.Add(rdr["AttachType"].ToString());
}
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Pdf;
Document document = new Document(PageSize.A4, 0, 0, 40, 0);
//Get instance response output stream to write output file.
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
// document.Header = new HeaderFooter(new Phrase("Header Text"), false);
// Parameters passed on to the function that creates the PDF
String headerText = "";
String footerText = "PAGE";
// Define a font and font-size in points (plus f for float) and pick a color
// This one is for both header and footer but you can also create seperate ones
Font fontHeaderFooter = FontFactory.GetFont("arial", 12f);
fontHeaderFooter.Color = Color.BLACK;
// Apply the font to the headerText and create a Phrase with the result
Chunk chkHeader = new Chunk(headerText, fontHeaderFooter);
Phrase p1 = new Phrase(chkHeader);
// create a HeaderFooter element for the header using the Phrase
// The boolean turns numbering on or off
HeaderFooter header = new HeaderFooter(p1, false);
// Remove the border that is set by default
header.Border = Rectangle.NO_BORDER;
// Align the text: 0 is left, 1 center and 2 right.
header.Alignment = 1;
// add the header to the document
document.Header = header;
// The footer is created in an similar way
// If you want to use numbering like in this example, add a whitespace to the
// text because by default there's no space in between them
if (footerText.Substring(footerText.Length - 1) != " ") footerText += " ";
//string newFooter = footerText + pageCount;
Chunk chkFooter = new Chunk(footerText, fontHeaderFooter);
Phrase p2 = new Phrase(chkFooter);
// Turn on numbering by setting the boolean to true
HeaderFooter footer = new HeaderFooter(p2, true);
footer.Border = Rectangle.NO_BORDER;
footer.Alignment = 1;
document.Footer = footer;
Response.Write(pdfName);
Response.Write("test");
// Open the Document for writing and continue creating its content
document.Open();
foreach (PdfReader reader in readerList)
{
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
if ("if list 2 value = "Utilities" then create green square")
{
PdfContentByte cb = writer.DirectContent;
var rect = new iTextSharp.text.Rectangle(200, 200, 100, 100);
rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER;
rect.BorderWidth = 5; rect.BorderColor = new BaseColor(2, 3, 0);
cb.Rectangle(rect);
}
document.Add(iTextSharp.text.Image.GetInstance(page));
}
}
document.Close();
Response.AppendHeader("content-disposition", "inline; filename=" + Request.QueryString["id"] + "-Final");
Response.ContentType = "application/pdf";
con2.Close();
Response.Write(pdfName);
}
}
catch
{
// Response.Redirect("~/PDFProblem.aspx", false);
}
}
This is a bit clunky but it works until I refactor it. I simply iterated the second list (pdfName) within the first iTextSharp one (pdfReader) using an incrementing integer to move the second list forward when the first list did:
foreach (PdfReader reader in readerList)
{
for (int i = 1; i <= reader.NumberOfPages; i++)
{
string totalValue = pdfName[nextOne].ToString();
PdfImportedPage page = writer.GetImportedPage(reader, i);
if (totalValue == "Permit")
{
PdfContentByte cb = writer.DirectContent;
var rect = new iTextSharp.text.Rectangle(200, 200, 100, 100);
rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER;
rect.BorderWidth = 5; rect.BorderColor = new BaseColor(2, 3, 0);
cb.Rectangle(rect);
}
if (totalValue == "TaxBill")
{
PdfContentByte cb = writer.DirectContent;
var rect = new iTextSharp.text.Rectangle(200, 200, 100, 100);
rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER;
rect.BorderWidth = 15; rect.BorderColor = new BaseColor(3, 2, 0);
cb.Rectangle(rect);
}
nextOne = nextOne + 1;
document.Add(iTextSharp.text.Image.GetInstance(page));
}
}

Resize an added picture in a ppt slide with c#

I'm getting some images from my database and populating a ppt file with them like this:
foreach (DataRow item in imagens.Rows)
{
nomeArquivo = item[0].ToString();
imagebytes = (byte[])item[1];
extensao = item[2].ToString();
MemoryStream ms = new MemoryStream(imagebytes);
img = Image.FromStream(ms);
img.Save(nomeArquivo + ".png", ImageFormat.Png);
slideAdd = slides.Add(indice, PowerPoint.PpSlideLayout.ppLayoutPictureWithCaption);//no objeto de slides, vou criar um no índice especificado - começa a partir de 1
shape = slideAdd.Shapes[2];
string caminho = Path.Combine(executableLocation, nomeArquivo + ".png");
slideAdd.Shapes.AddPicture2(caminho, MsoTriState.msoFalse, MsoTriState.msoTrue, shape.Left, shape.Top, shape.Width, shape.Height, MsoPictureCompress.msoPictureCompressFalse);
slideAdd.Shapes[1].TextFrame.TextRange.Text = nomeArquivo;
slideAdd.Shapes[3].TextFrame.TextRange.Text = nomeArquivo;
indice++;
}
However, they are not resized to fit the containg shape:
Is there a way to resize them beforehand?

Show Data on Google Map using C# / DataTable

what am I doing wrong? I'm trying to load Google Maps with data from the database and I am using the following code, but am having an error when I enter the foreach:
"An exception of type 'System.Exception' occurred in GMaps.dll but was not handled in user code"
"Additional information: Latitude must be beetwen -90 and 90"
if (!IsPostBack)
{
GLatLng mainLocation = new GLatLng(-18.8533, -41.9450);
GMapMyFriends.setCenter(mainLocation, 15);
XPinLetter xpinLetter = new XPinLetter(PinShapes.pin_star, "C", Color.Green, Color.White, Color.Chocolate);
GMapMyFriends.Add(new GMarker(mainLocation, new GMarkerOptions(new GIcon(xpinLetter.ToString(), xpinLetter.Shadow()))));
DataTable MyFriends = db.ExecuteSelect("SELECT lat ,lng FROM tblAdress;");
PinIcon p;
GMarker gm;
foreach (DataRow dtRow in MyFriends.Rows)
{
foreach (DataColumn dc in MyFriends.Columns)
{ p = new PinIcon(PinIcons.home, Color.Cyan);
gm = new GMarker(new GLatLng(Convert.ToDouble(dtRow["lat"]), Convert.ToDouble(dtRow["lng"])),
new GMarkerOptions(new GIcon(p.ToString(), p.Shadow())));
}
}
}
}
One of the values coming back from your database is either greater than 90 or less than -90. Create a breakpoint on your code and step through the values coming back from the database to determine which one.
You should also trap bad values and set them to something else (either don't display or default to some other value)
you have to change the "." by "," to accept the correct variable parameters
example
-18.8533 -18.8533 by substituting for the value of the variable is correct
hola
tienes que cambiar las "." por las "," para que la variable acepte los parámetros correcto
ejemplo
sustituir -18.8533 por -18,8533 para que el valor de la variable sea correcto

Categories