PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 8:57 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Feb 22, 2023 12:10 am 
Offline

Joined: Mon Feb 20, 2023 1:26 am
Posts: 5
RenderDocument generates Index out of Range error. How do you troubleshoot? Should I use an older version of PDFSharp? Please advise.

public static Document CreateDownloadableDirectory(long userID, string filePath)
{
// get user info
UserInfo uInfo = UserDAL.GetUserByID(userID);

// Create a new MigraDoc document
Document document = new Document();
document.Info.Title = "www.greenthumbhub.com Direectory";
document.Info.Subject = "www.greenthumbhub.com Direectory";
document.Info.Author = "https://www.greenthumbhub.com";

MigraDoc.DocumentObjectModel.Style style = document.Styles["Normal"];
style.Font.Name = "Verdana";
style.Font.Size = 10;

// Each MigraDoc document needs at least one section.
Section section = document.AddSection();

// Create footer
Paragraph paragraph = section.Footers.Primary.AddParagraph();
paragraph.AddText("www.greenthumbhub.com - Directory.pdf - " + DateTime.Today.ToShortDateString());
paragraph.Format.Font.Size = 10;
paragraph.Format.Alignment = ParagraphAlignment.Center;

// Create the item table
MigraDoc.DocumentObjectModel.Tables.Table tbl = section.AddTable();
tbl.Style = "Table";
tbl.Borders.Color = Color.FromRgb(0, 0, 0);
tbl.Rows.LeftIndent = 0;

// Before you can add a row, you must define the columns
Column column = tbl.AddColumn("6in");
column.Format.Alignment = ParagraphAlignment.Center;

Row row = tbl.AddRow();
row.Format.Alignment = ParagraphAlignment.Center;
string file = filePath + "Images/header-sm.jpg";
row.Cells[0].AddImage(file);
row.Cells[0].Format.Alignment = ParagraphAlignment.Center;
row.Cells[0].VerticalAlignment = VerticalAlignment.Bottom;
row.Cells[0].Borders.Top.Visible = false;
row.Cells[0].Borders.Right.Visible = false;
row.Cells[0].Borders.Left.Visible = false;
row.Cells[0].Borders.Bottom.Visible = false;

string latitude = string.Empty;
string longitude = string.Empty;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MainDB"].ToString()))
{
conn.Open();
// retrieve latitude and longitude for zip
string sSQL = @"select Latitude, Longitude
from tblZipCodes
where (ZipCode = @ZIP)";
using (SqlCommand command = new SqlCommand(sSQL, conn))
{
command.Parameters.Add("@ZIP", SqlDbType.NVarChar).Value = uInfo.ZIP;
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
latitude = reader["LATITUDE"] == DBNull.Value ? string.Empty : Convert.ToString(reader["LATITUDE"]);
longitude = reader["LONGITUDE"] == DBNull.Value ? string.Empty : Convert.ToString(reader["LONGITUDE"]);
}
}
}

DataTable dtAds = new DataTable();
sSQL = @"select distinct a.ID, a.SiteAddress1, a.SiteAddress2, a.SiteCity, a.SiteState, a.SiteZip, va.Distance
from tblAds a,
(select ads.ID, convert(decimal(5,1),(3959 * acos(cos(radians(@lat)) * cos(radians(z.Latitude)) * cos(radians(z.Longitude) - radians(@long)) + sin(radians(@lat)) * sin(radians(z.Latitude))))) Distance
from tblAds ads Inner Join tblZipCodes z ON ((ads.SiteZip = z.ZipCode) And (3959 * acos(cos(radians(@lat)) * cos(radians(z.Latitude)) * cos(radians(z.Longitude) - radians(@long)) + sin(radians(@lat)) * sin(radians(z.Latitude)))) < @dist)) va
where (a.ID = va.ID) And (a.Status = 3)
Order By va.Distance, a.SiteState, a.SiteCity ";
using (SqlCommand command = new SqlCommand(sSQL, conn))
{
command.Parameters.Add("@lat", SqlDbType.NVarChar).Value = latitude;
command.Parameters.Add("@long", SqlDbType.NVarChar).Value = longitude;
command.Parameters.Add("@dist", SqlDbType.Int).Value = 180;
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(dtAds);
}
}

foreach (DataRow drAd in dtAds.Rows)
{
MigraDoc.DocumentObjectModel.Tables.Table table = section.AddTable();
table.Style = "Table";
table.Borders.Color = Color.FromRgb(0, 0, 0);
table.Rows.LeftIndent = 0;

// Before you can add a row, you must define the columns
column = table.AddColumn("5in");
column.Format.Alignment = ParagraphAlignment.Left;

column = table.AddColumn("1in");
column.Format.Alignment = ParagraphAlignment.Center;

string siteAddress = "<b>Site Address:</b><br />";
if (!string.IsNullOrWhiteSpace(drAd["SITEADDRESS2"].ToString()))
{
siteAddress += drAd["SITEADDRESS1"].ToString() + "<br />" + drAd["SITEADDRESS2"].ToString() + "<br />";
}
else
{
siteAddress += drAd["SITEADDRESS1"].ToString() + "<br />";
}
siteAddress += drAd["SITECITY"].ToString() + ", " + drAd["SITESTATE"].ToString() + " " + drAd["SITEZIP"].ToString() + "<br />";
siteAddress += "<b>Ad Link:</b><br />https://www.greenthumbhub.com/ViewAd.aspx?ID=" + drAd["ID"].ToString() + "";

DataTable dtFruit = new DataTable();
DataTable dtVegetables = new DataTable();
DataTable dtHerbs = new DataTable();
DataTable dtNuts = new DataTable();
DataTable dtMisc = new DataTable();

DataTable dtItems = AdDAL.GetCurrentItemsForAdDisplayCategory(Convert.ToInt64(drAd["ID"]));
DataRow[] drItems;

drItems = dtItems.Select("ItemCatID = " + GreenThumbConstants.ITEM_CAT_FRUIT.ToString(), "Name Asc");
if (drItems.Length > 0)
{
dtFruit = drItems.CopyToDataTable();
}

drItems = dtItems.Select("ItemCatID = " + GreenThumbConstants.ITEM_CAT_VEGETABLES.ToString(), "Name Asc");
if (drItems.Length > 0)
{
dtVegetables = drItems.CopyToDataTable();
}

drItems = dtItems.Select("ItemCatID = " + GreenThumbConstants.ITEM_CAT_HERBS.ToString(), "Name Asc");
if (drItems.Length > 0)
{
dtHerbs = drItems.CopyToDataTable();
}

drItems = dtItems.Select("ItemCatID = " + GreenThumbConstants.ITEM_CAT_NUTS.ToString(), "Name Asc");
if (drItems.Length > 0)
{
dtNuts = drItems.CopyToDataTable();
}

drItems = dtItems.Select("ItemCatID = " + GreenThumbConstants.ITEM_CAT_MISC.ToString(), "Name Asc");
if (drItems.Length > 0)
{
dtMisc = drItems.CopyToDataTable();
}
string item = string.Empty;
string sProduce = string.Empty;
if (dtFruit.Rows.Count > 0)
{
sProduce = "<b>Fruit:</b><br/>";
foreach (DataRow dr in dtFruit.Rows)
{
item = string.IsNullOrWhiteSpace(dr["Name"].ToString()) ? string.Empty : dr["Name"].ToString();
if (!string.IsNullOrWhiteSpace(item))
sProduce += string.Format("{0}, ", item);
}
sProduce = sProduce.Remove(sProduce.Length - 2, 2);
sProduce += "<br />";
}
if (dtVegetables.Rows.Count > 0)
{
sProduce += "<b>Vegetables:</b><br/>";
foreach (DataRow dr in dtVegetables.Rows)
{
item = string.IsNullOrWhiteSpace(dr["Name"].ToString()) ? string.Empty : dr["Name"].ToString();
if (!string.IsNullOrWhiteSpace(item))
sProduce += string.Format("{0}, ", item);
}
sProduce = sProduce.Remove(sProduce.Length - 2, 2);
sProduce += "<br />";
}
if (dtHerbs.Rows.Count > 0)
{
sProduce += "<b>Herbs:</b><br/>";
foreach (DataRow dr in dtHerbs.Rows)
{
item = string.IsNullOrWhiteSpace(dr["Name"].ToString()) ? string.Empty : dr["Name"].ToString();
if (!string.IsNullOrWhiteSpace(item))
sProduce += string.Format("{0}, ", item);
}
sProduce = sProduce.Remove(sProduce.Length - 2, 2);
sProduce += "<br />";
}
if (dtNuts.Rows.Count > 0)
{
sProduce += "<b>Nuts:</b><br/>";
foreach (DataRow dr in dtNuts.Rows)
{
item = string.IsNullOrWhiteSpace(dr["Name"].ToString()) ? string.Empty : dr["Name"].ToString();
if (!string.IsNullOrWhiteSpace(item))
sProduce += string.Format("{0}, ", item);
}
sProduce = sProduce.Remove(sProduce.Length - 2, 2);
sProduce += "<br />";
}
if (dtMisc.Rows.Count > 0)
{
sProduce += "<b>Other Items:</b><br/>";
foreach (DataRow dr in dtMisc.Rows)
{
item = string.IsNullOrWhiteSpace(dr["Name"].ToString()) ? string.Empty : dr["Name"].ToString();
if (!string.IsNullOrWhiteSpace(item))
sProduce += string.Format("{0}, ", item);
}
sProduce = sProduce.Remove(sProduce.Length - 2, 2);
sProduce += "<br />";
}

AdsInfo aInfo = AdDAL.GetAdByID(Convert.ToInt64(drAd["ID"]));

row = table.AddRow();
row.Format.Alignment = ParagraphAlignment.Center;
row.Cells[0].AddParagraph(siteAddress);
row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
row.Cells[0].VerticalAlignment = VerticalAlignment.Bottom;
row.Cells[1].AddParagraph("<b>Distance:</b><br />" + drAd["Distance"].ToString());
row.Cells[1].Format.Alignment = ParagraphAlignment.Center;
row.Cells[1].VerticalAlignment = VerticalAlignment.Bottom;
row.Cells[1].MergeDown = 3;

row = table.AddRow();
row.Format.Alignment = ParagraphAlignment.Center;
row.Cells[0].AddParagraph(sProduce);
row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
row.Cells[0].VerticalAlignment = VerticalAlignment.Bottom;

row = table.AddRow();
row.Format.Alignment = ParagraphAlignment.Center;
row.Cells[0].AddParagraph("<b>Description:</b><br />" + aInfo.DESCRIPTION);
row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
row.Cells[0].VerticalAlignment = VerticalAlignment.Bottom;
}
}
return document;

}


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 22, 2023 10:35 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
marty.nord wrote:
Should I use an older version of PDFSharp?
Definitely maybe.

Would be good to know which version you are using. Would be good to see error details. Would be good if we could replicate the issue.
See also:
http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 22, 2023 11:24 am 
Offline

Joined: Mon Feb 20, 2023 1:26 am
Posts: 5
I was able to figure out the issue.

For some reason, I had to change MergeDown from 3 to 2 which seems weird to me. After I made this change, it no longer generated an error.

I thought MergeDown included the existing cell that you are in just like a colspan does in HTML.

You may close this issue.

Thank you,
Marty


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 151 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group