I'm evaluating Spire PDFViewer using the FreeSpire.PDFViewer component, in conjunction with SSRS Reporting. The FreeSpire.PDFViewer version is 2.4.35 (the latest available via NuGet).
SSRS is correctly rendering a PDF file, which I am able to view in a form that contains the FreeSpire.PDFViewer control. This PDF renders correctly on the screen.
When I add a printing function however, all that is printed is blank pages.
Is this expected behaviour?
Code:
- Code: Select all
public partial class PDFViewer : Form
{
private string _currentPDF = string.Empty;
private PdfDocumentViewer _pdfViewer = null;
public PDFViewer()
{
InitializeComponent();
_pdfViewer = new PdfDocumentViewer();
_pdfViewer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
_pdfViewer.Dock = DockStyle.Fill;
panPDFContainer.Controls.Add(_pdfViewer);
this.FormClosing += PDFViewer_FormClosing;
}
private void PDFViewer_FormClosing(object sender, FormClosingEventArgs e)
{
if (!string.IsNullOrEmpty(_currentPDF))
{
if (File.Exists(_currentPDF))
{
try
{
File.Delete(_currentPDF);
}
catch (Exception exception)
{
}
}
}
}
public void LoadPDF(string pdfPath, ReportSetting reportSettings)
{
_currentPDF = pdfPath;
_pdfViewer.SetViewerMode(PdfViewerMode.Auto);
_pdfViewer.LoadFromFile(pdfPath);
SetPageAndPrinterSettings(reportSettings);
}
private void SetPageAndPrinterSettings(ReportSetting settings)
{
System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings();
IEnumerable<PaperSize> paperSizes = printSettings.PaperSizes.Cast<PaperSize>();
PaperSize sizeA4 = null;
if (paperSizes.Count<PaperSize>(size => size.Kind == PaperKind.A4) > 0)
{
sizeA4 = paperSizes.First<PaperSize>(size => size.Kind == PaperKind.A4); // setting paper size to A4 size
}
else
{
// Do nothing for now...
}
if (string.IsNullOrEmpty(settings.PrinterName))
{
System.Drawing.Printing.PrinterSettings defaultPrinter = Utility.Utilities.GetDefaultPrinter();
if (defaultPrinter != null)
{
settings.PrinterName = defaultPrinter.PrinterName;
}
printSettings.PrinterName = settings.PrinterName;
}
printSettings.DefaultPageSettings.Landscape = settings.PrintOrientation == Business.Reporting.ReportDocument.Orientation.Landscape;
printSettings.DefaultPageSettings.Color = settings.PrintInColor;
if (sizeA4 != null)
{
printSettings.DefaultPageSettings.PaperSize = sizeA4;
}
printSettings.DefaultPageSettings.Margins = new Margins(settings.LeftMargin, settings.RightMargin, settings.TopMargin, settings.BottomMargin);
PageSettings pageSettings = new PageSettings();
pageSettings.PrinterSettings = printSettings;
pageSettings.Landscape = settings.PrintOrientation == Reporting.ReportDocument.Orientation.Landscape;
pageSettings.Color = settings.PrintInColor;
pageSettings.PaperSize = sizeA4;
pageSettings.Margins = new Margins(settings.LeftMargin, settings.RightMargin, settings.TopMargin, settings.BottomMargin);
_pdfViewer.PrintDocument.PrinterSettings = printSettings;
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument printDoc = _pdfViewer.PrintDocument;
printDoc.Print();
}
NOTE: the ReportSetting class is a database class that allows the user to manipulate printers/trays for direct printing without preview.