У меня есть страница, которая создает PDF-документ Itext, а затем загружается документ. Следующий код будет делать именно это. Но страница освежает. Я бы хотел, чтобы страница не обновлялась. Я думаю, что это обеспечит лучший пользовательский интерфейс, так что входные данные формы страницы не будут стерты при обновлении страницы.Загрузка IText .pdf с использованием JS/AJAX
Есть ли лучший способ справиться с этой задачей, чтобы страница не нуждалась в обновлении? Есть ли какая-то модификация этого существующего кода, который можно сделать, чтобы избежать обновления страницы?
labelReprint.js:
$.ajax({
url: "labelReprint.aspx/DoLabelPrint",
data: "{'type':'" + lblType + "','scan':'" + scan + "','qty':'" + qty + "','split':'" + split + "'}",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
console.log('Downloading .pdf');
//pdf
var downloadpdf = $('<a id="downloadpdf" download="' + fileName + '.pdf" href="data:application/pdf;base64,' + result.d + '" >');
$('#download').append(downloadpdf);
document.getElementById("downloadpdf").click();
$("#downloadpdf").remove();
},
error: function (result) {
console.log(result.responseJSON.Message + "\n\r\n\r" + result.responseJSON.StackTrace);
}
});
Функция последующей вызывается из labelReprint.aspx/DoLabelPrint ссылается на AJAX выше. Это обрабатывает создание Itext/pdf.
Public Shared Function mfgrLabel(ByVal barCode As String, ByVal user As String) As String
Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim subs = New subs
Dim pSize As New iTextSharp.text.Rectangle(252, 36)
Dim pdfDoc As New iTextSharp.text.Document(pSize, 0.0F, 0.0F, 0.0F, 0.0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, MS)
pdfDoc.Open()
Dim cb As PdfContentByte = writer.DirectContent
Dim tblLbl As iTextSharp.text.pdf.PdfPTable = New iTextSharp.text.pdf.PdfPTable(2)
Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, False)
Dim tRsmall As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 5, iTextSharp.text.Font.NORMAL)
Dim tR As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 6, iTextSharp.text.Font.NORMAL)
Dim tRMed As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 7, iTextSharp.text.Font.NORMAL)
Dim tRMedLrg As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL)
Dim tRLrg As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.NORMAL)
Dim tXRLrg As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL)
Dim cBlankNB As iTextSharp.text.pdf.PdfPCell = New iTextSharp.text.pdf.PdfPCell
cBlankNB.Border = 0
tblLbl.HorizontalAlignment = 0
'*************** Labels ***************************
Dim cellTimestamp As iTextSharp.text.pdf.PdfPCell = New iTextSharp.text.pdf.PdfPCell(New iTextSharp.text.Phrase(barCode, tR)) '"Inovar P/N:"
Dim cPartNo As iTextSharp.text.pdf.PdfPCell = New iTextSharp.text.pdf.PdfPCell(New iTextSharp.text.Phrase(barCode.Split("|")(0) & " " & barCode.Split("|")(1), tRMedLrg))
Dim uInfo As String = user.ToUpper & " " & Now.ToString("dd-MMM-yyyy").ToUpper
Dim pInfo As String = barCode.Split("|")(0) & " " & barCode.Split("|")(1)
cellTimestamp.Border = 0
cPartNo.Border = 0
'**************************************************
'************ Bar Codes **************************
Dim safeFileName As String = subs.fixFileNameBeforeSave(barCode.Split("|")(1))
Dim inoDm As Barcodes.DataMatrix.DataMatrixWebControl = New Barcodes.DataMatrix.DataMatrixWebControl
inoDm.DataToEncode = barCode.Split("|")(1)
If File.Exists(HttpContext.Current.Server.MapPath(safeFileName & ".jpg")) Then
File.Delete(HttpContext.Current.Server.MapPath(safeFileName & ".jpg"))
End If
inoDm.SaveToImageFile(25, 25, HttpContext.Current.Server.MapPath(safeFileName & ".jpg"), 300, Barcodes.DataMatrix.Dimensions.dmMM)
Dim inoDmImg As iTextSharp.text.Image
inoDmImg = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(safeFileName & ".jpg"))
inoDmImg.ScaleAbsolute(25, 25)
Dim inoPBC As iTextSharp.text.pdf.PdfPCell
inoPBC = New iTextSharp.text.pdf.PdfPCell(iTextSharp.text.Image.GetInstance(inoDmImg))
inoPBC.VerticalAlignment = 6
inoPBC.HorizontalAlignment = 0
inoPBC.Border = 0
inoPBC.PaddingLeft = 10.0F
inoPBC.PaddingTop = 15.0F
'************ End Bar Codes **************************
inoPBC.PaddingTop = 5.0F
cPartNo.NoWrap = True
inoPBC.Colspan = 2
tblLbl.AddCell(inoPBC)
pdfDoc.Add(tblLbl)
cb.SetFontAndSize(bf, 8)
cb.BeginText()
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, pInfo, 40, 20, 0) '180
cb.EndText()
cb.SetFontAndSize(bf, 4)
cb.BeginText()
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, uInfo, 40, 4, 0) '180
cb.EndText()
pdfDoc.Close()
'Clean up
If File.Exists(HttpContext.Current.Server.MapPath(safeFileName & ".jpg")) Then
File.Delete(HttpContext.Current.Server.MapPath(safeFileName & ".jpg"))
End If
Return System.Convert.ToBase64String(MS.ToArray())
Любые мысли или идеи были бы наиболее ценными.