#c# #pdf #rdlc #pdfsharp
#c# #PDF #rdlc #pdfsharp
Вопрос:
Я пытаюсь объединить 2 PDF-данные из отчета rdlc.
Проблема в том, что в результате получаются пустые страницы.
Я не знаю почему, может кто-нибудь мне помочь, пожалуйста.
вот мой код:
private ActionResult ConcatPdf(byte[] pdfData1, byte[] pdfData2)
{
MemoryStream ms1 = new MemoryStream(pdfData1);
MemoryStream ms2 = new MemoryStream(pdfData2);
PdfDocument inputDoc1 = PdfReader.Open(ms1, PdfDocumentOpenMode.Import);
PdfDocument inputDoc2 = PdfReader.Open(ms2, PdfDocumentOpenMode.Import);
PdfDocument outputDoc = new PdfDocument();
foreach (PdfPage page in inputDoc1.Pages)
{
outputDoc.AddPage(page);
}
foreach (PdfPage page in inputDoc2.Pages)
{
outputDoc.AddPage(page);
}
MemoryStream outputMs = new MemoryStream();
outputDoc.Save(outputMs);
return File(outputMs.ToArray(), "application/pdf");
}
В функции создания отчета выглядит так:
public ActionResult TestPDF(int id)
{
// Set report path.
LocalReport rep = viewer.LocalReport;
rep.ReportPath = Server.MapPath("~/Reports/rptExternalTransferIndividual.rdlc");
rep.DataSources.Clear();
//
// Set data and parameter to report.
//
...
...
return ConcatPdf(viewer.LocalReport.Render("PDF"), viewer.LocalReport.Render("PDF"));
}
Ответ №1:
Я знаю, что это устарело, но добавьте HumanReadablePDF:
string deviceInfo = "<DeviceInfo>"
" <OutputFormat>PDF</OutputFormat>"
" <PageWidth>29.7cm</PageWidth>"
" <PageHeight>21cm</PageHeight>"
" <MarginTop>0cm</MarginTop>"
" <MarginLeft>0cm</MarginLeft>"
" <MarginRight>0cm</MarginRight>"
" <MarginBottom>0cm</MarginBottom>"
" <HumanReadablePDF>True</HumanReadablePDF>"
"</DeviceInfo>";
byte[] reportBytes = LocalReport.Render(
"PDF", deviceInfo, out mimeType, out encoding,
out extension,
out streamids, out warnings);
Затем верните массив байтов в PDFsharp.
Комментарии:
1. Если вы не хотите изменять все размеры, это сработало только
"<DeviceInfo><OutputFormat>PDF</OutputFormat><HumanReadablePDF>True</HumanReadablePDF></DeviceInfo>"
для меня.
Ответ №2:
Возможно, есть что-то необычное в файлах PDF, созданных с помощью программы просмотра отчетов. Нам нужны образцы файлов, чтобы проверить это.
Смотрите также:
http://forum.pdfsharp.net/viewtopic.php?f=3amp;t=1818amp;p=5174
Комментарии:
1. Спасибо за это, я столкнулся с той же проблемой с PDF-файлами, созданными средством просмотра отчетов, и информация во 2-й ссылке исправила это … из интереса, будет ли это исправление включено в будущую версию библиотеки?
Ответ №3:
Я использую iTextSharp, чтобы сделать то же самое.
Передача того же параметра -> просмотрщик.LocalReport.Render («PDF»). Это работает хорошо.
Вот мой код:
private ActionResult ConcatPdf(List<byte[]> pdfDataList)
{
MemoryStream outputMS = new MemoryStream();
Document document = new Document();
PdfCopy writer = new PdfCopy(document, outputMS);
PdfImportedPage page = null;
document.Open();
foreach (byte[] pdfData in pdfDataList)
{
PdfReader reader = new PdfReader(pdfData);
int n = reader.NumberOfPages;
for (int i = 1; i <= n; i )
{
page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}
PRAcroForm form = reader.AcroForm;
if (form != null)
writer.CopyAcroForm(reader);
}
document.Close();
return File(outputMS.ToArray(), "application/pdf");
}