Hey Logan, here you go.
I adjust the line (meaning, Y position of each group of elements that compose a line), by readjusting the largest box with fitText. So, again, fitText adjusts the box, but it formats it to take the remaining width of the page. I don’t want that. I want the box to adjust given a width constraint. I would also be satisfied with a different method for building tables on a PDF. Thanks
function addIssues(annotations, pageHeight: number, docViewer, annotationList: Annotations.WidgetAnnotation[]) {
const result: any[] = [];
const doc = docViewer.getDocument();
const pageNumber = 1;
const pageInfo = doc.getPageInfo(pageNumber);
const pageMatrix = doc.getPageMatrix(pageNumber);
const pageRotation = doc.getPageRotation(pageNumber);
const maxIter = props.state?.length ?? 0;
// Issue item widgets
const issueProperties = ["description", "locationCode", "itemCode", "deficiencyCode", "statusCode", "isSiteCompleted", "completedDate"];
const fieldNames = ["description", "location", "item", "deficiency", "status", "site-done", "complete-date"];
const freeTextDict: { [key: string]: any } = {};
const filteredWidgetsDict: { [key: string]: any[] } = {};
fieldNames.forEach(prop => filteredWidgetsDict[prop] = annotationList.filter(el => el.fieldName?.includes(prop)));
// Define bottom and page correction factor
let bottom: number = filteredWidgetsDict["description"]?.[0].getBottom();
let pageCorr = filteredWidgetsDict["description"]?.[0].getPageNumber() - 1;
// Create freetext annotations for each property in each issue,
// MaxIter is the length of the issues list
for (let i = 0; i < maxIter; ++i) {
fieldNames.forEach(prop => freeTextDict[prop] = new annotations.FreeTextAnnotation());
for (let index = 0; index < fieldNames.length; ++index) {
const property = issueProperties[index];
const fieldName = fieldNames[index];
// Place them on the page, items are placed using the bottom of the last description widget as the initial Y position
if (property === "description") {
if (i === 0) {
placeCustomWidgets(filteredWidgetsDict[fieldName][pageCorr], freeTextDict[fieldName])
} else {
placeCustomWidgets(filteredWidgetsDict[fieldName][pageCorr], freeTextDict[fieldName], 0, bottom);
}
} else {
if (i === 0) {
placeCustomWidgets(filteredWidgetsDict[fieldName][pageCorr], freeTextDict[fieldName], filteredWidgetsDict[fieldName][pageCorr].X, filteredWidgetsDict[fieldName][pageCorr].Y)
} else {
placeCustomWidgets(filteredWidgetsDict[fieldName][pageCorr], freeTextDict[fieldName], filteredWidgetsDict[fieldName][pageCorr].X, bottom)
}
}
}
const pageFraction = freeTextDict["description"].getBottom() / pageHeight;
for (let fi = 0; fi < fieldNames.length; ++fi) {
const content = props.state?.[i]?.[issueProperties[fi]];
const fieldName = fieldNames[fi];
const padding = new annotations.Rect(0, 0, 0, 0);
const textColor = new annotations.Color(0, 0, 0);
const strokeColor = new annotations.Color(255, 255, 255);
const fontSize = '8pt';
formulateTextWidget(freeTextDict[fieldName], fieldName, content, padding, textColor, strokeColor, fontSize);
}
// Adjust the largest box
freeTextDict["description"].fitText(pageInfo, pageMatrix, pageRotation);
bottom = freeTextDict["description"].getBottom();
if (pageFraction > 0.95) {
// console.log("Turning page");
pageCorr += 1;
bottom = filteredWidgetsDict["description"]?.[pageCorr].Y;
}
// console.log(`The bottom is at ${bottom}`);
Object.keys(freeTextDict).forEach(k => result.push(freeTextDict[k]));
}
return result;
}
export function placeCustomWidgets(templateWidget: Annotations.WidgetAnnotation, newWidget: Annotations.WidgetAnnotation, x?: number, y?: number) {
const pageNumber = templateWidget.getPageNumber();
const newX = x ? x : templateWidget.X;
const newY = y ? y : templateWidget.Y;
newWidget.setPageNumber(pageNumber);
newWidget.X = newX;
newWidget.Y = newY;
// console.log("PageNumber placing issues");
// console.log(pageNumber)
newWidget.Width = templateWidget.Width;
newWidget.Height = templateWidget.Height;
return newWidget;
}