为有中文需求的客户提供多渠道中文技术支持.

Thu Jul 28, 2022 3:24 pm

您好,

做出来的PDF文件用Chrome或者Edge都可以正常打开,但是使用Adobe Reader DC打开的时候报错。
PDF文件在附件.

Code: Select all
public ActionResult MakeSamplePDF3()
        {
            try
            {
                PdfDocument pdf = new PdfDocument();
                PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins(10));
                PdfFont font_title = new PdfFont(PdfFontFamily.Helvetica, 18f, PdfFontStyle.Bold);
                PdfFont font_normal = new PdfFont(PdfFontFamily.Helvetica, 15f, PdfFontStyle.Bold);
                PdfFont font_small = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
                PdfFont font_smaller = new PdfFont(PdfFontFamily.Helvetica, 8f, PdfFontStyle.Bold);
                PdfFont font_small_underline = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Underline);
                PdfFont font_thin = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Regular);
                PdfFont font_thinner = new PdfFont(PdfFontFamily.Helvetica, 8f, PdfFontStyle.Regular);
                PdfFont font_thinner_ita = new PdfFont(PdfFontFamily.Helvetica, 8f, PdfFontStyle.Italic);
                float height = 63;
                float x = 0;
                float y = 0;

                PdfStringFormat centeralign = new PdfStringFormat(PdfTextAlignment.Center);
                PdfStringFormat rightalign = new PdfStringFormat(PdfTextAlignment.Right);
                page.Canvas.DrawString("PDF Maker", font_title, PdfBrushes.Black, page.Canvas.ClientSize.Width / 2, height + 10, centeralign);
                page.Canvas.DrawString("TEST1", font_normal, PdfBrushes.Black, page.Canvas.ClientSize.Width / 2, height + 35, centeralign);

                page.Canvas.DrawString("Date:", font_small, PdfBrushes.Black, x, height + 70);
                page.Canvas.DrawString(DateTime.Now.ToString("yyyy/MM/dd"), font_small_underline, PdfBrushes.Black, 50, height + 70);

                //创建一个PdfGrid对象
                PdfGrid grid = new PdfGrid();

                //设置单元格边距
                grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

                //设置表格默认字体
                grid.Style.Font = font_thin;

                //添加4行4列
                PdfGridRow row1 = grid.Rows.Add();
                PdfGridRow row2 = grid.Rows.Add();
                PdfGridRow row3 = grid.Rows.Add();
                PdfGridRow row4 = grid.Rows.Add();
                PdfGridRow row5 = grid.Rows.Add();
                PdfGridRow row6 = grid.Rows.Add();
                PdfGridRow row7 = grid.Rows.Add();
                PdfGridRow row8 = grid.Rows.Add();
                grid.Columns.Add(4);
                grid.Columns[0].Format.Alignment = PdfTextAlignment.Center;
                grid.Columns[1].Format.Alignment = PdfTextAlignment.Center;
                grid.Columns[2].Format.Alignment = PdfTextAlignment.Center;
                grid.Columns[3].Format.Alignment = PdfTextAlignment.Center;

                //设置列宽
                grid.Columns[0].Width = page.Canvas.ClientSize.Width * 0.3f;
                grid.Columns[1].Width = page.Canvas.ClientSize.Width * 0.2333f;
                grid.Columns[2].Width = page.Canvas.ClientSize.Width * 0.2333f;
                grid.Columns[3].Width = page.Canvas.ClientSize.Width * 0.2333f;

                //写入数据
                row1.Cells[0].Value = "Customer Name";
                row1.Cells[0].ColumnSpan = 4;
                row1.Cells[0].Style.Font = font_normal;
                row1.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row1.Cells[0].Style.BackgroundBrush = PdfBrushes.AliceBlue;

                row2.Cells[0].Value = "Factors";
                row2.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row2.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGray;

                row2.Cells[1].Value = "2022";
                row2.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row2.Cells[1].Style.BackgroundBrush = PdfBrushes.LightGray;

                row2.Cells[2].Value = "2021";
                row2.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row2.Cells[2].Style.BackgroundBrush = PdfBrushes.LightGray;

                row2.Cells[3].Value = "2020";
                row2.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row2.Cells[3].Style.BackgroundBrush = PdfBrushes.LightGray;

                row3.Cells[0].Value = "Overall";
                row3.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
                row3.Cells[1].Value = "100";
                row3.Cells[2].Value = "100";
                row3.Cells[3].Value = "100";
                row3.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row3.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row3.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;

                row4.Cells[0].Value = "Delivery";
                row4.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
                row4.Cells[1].Value = "100";
                row4.Cells[2].Value = "100";
                row4.Cells[3].Value = "100";
                row4.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row4.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row4.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;

                row5.Cells[0].Value = "Price & Payment Term";
                row5.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
                row5.Cells[1].Value = "100";
                row5.Cells[2].Value = "100";
                row5.Cells[3].Value = "100";
                row5.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row5.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row5.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;

                row6.Cells[0].Value = "Responsiveness";
                row6.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
                row6.Cells[1].Value = "100";
                row6.Cells[2].Value = "100";
                row6.Cells[3].Value = "100";
                row6.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row6.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row6.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;

                row7.Cells[0].Value = "Final Score";
                row7.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
                row7.Cells[0].Style.Font = font_small;
                row7.Cells[1].Value = "100";
                row7.Cells[2].Value = "100";
                row7.Cells[3].Value = "100";
                row7.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row7.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row7.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row7.Cells[1].Style.Font = font_small;
                row7.Cells[2].Style.Font = font_small;
                row7.Cells[3].Style.Font = font_small;
                row7.Cells[1].Style.BackgroundBrush = PdfBrushes.OrangeRed;
                row7.Cells[2].Style.BackgroundBrush = PdfBrushes.LightBlue;
                row7.Cells[3].Style.BackgroundBrush = PdfBrushes.LightCyan;

                row8.Cells[0].Value = "Rank";
                row8.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
                row8.Cells[0].Style.Font = font_small;
                row8.Cells[1].Value = "100";
                row8.Cells[2].Value = "100";
                row8.Cells[3].Value = "100";
                row8.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row8.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row8.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
                row8.Cells[1].Style.Font = font_small;
                row8.Cells[2].Style.Font = font_small;
                row8.Cells[3].Style.Font = font_small;
                row8.Cells[1].Style.BackgroundBrush = PdfBrushes.LightCyan;
                row8.Cells[2].Style.BackgroundBrush = PdfBrushes.LightCyan;
                row8.Cells[3].Style.BackgroundBrush = PdfBrushes.LightCyan;

                //设置边框颜色、粗细
                PdfBorders borders = new PdfBorders();
                borders.All = new PdfPen(Color.Black, 0.1f);
                foreach (PdfGridRow pgr in grid.Rows)
                {
                    foreach (PdfGridCell pgc in pgr.Cells)
                    {
                        pgc.Style.Borders = borders;
                    }
                }

                //在指定为绘入表格
                grid.Draw(page, new PointF(0, height + 100));

                page.Canvas.DrawString("[Grade = Your score / Max]", font_smaller, PdfBrushes.Black, page.Canvas.ClientSize.Width - 3, height + 106, rightalign);

                page.Canvas.DrawString("*Certain category may be excluded from final score calculation if data is insufficient.Remaining KPI weights will be adjusted to account for the missing weights.", font_thinner_ita, PdfBrushes.Black, 0, height + 240);
                //page.Canvas.DrawString("Remaining KPI weights will be adjusted to account for the missing weights.", font_thinner_ita, PdfBrushes.Black, 50, height + 245);

                //创建一个PdfGrid对象
                PdfGrid grid2 = new PdfGrid();

                //设置单元格边距
                grid2.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

                //设置表格默认字体
                grid2.Style.Font = font_thin;

                //添加4行4列
                PdfGridRow grid2row1 = grid2.Rows.Add();
                PdfGridRow grid2row2 = grid2.Rows.Add();
                PdfGridRow grid2row3 = grid2.Rows.Add();

                grid2.Columns.Add(2);
                grid2.Columns[0].Format.Alignment = PdfTextAlignment.Center;
                grid2.Columns[1].Format.Alignment = PdfTextAlignment.Center;

                //设置列宽
                grid2.Columns[0].Width = page.Canvas.ClientSize.Width * 0.45f;
                grid2.Columns[1].Width = page.Canvas.ClientSize.Width * 0.15f;

                //写入数据
                grid2row1.Cells[0].Value = "Preferred Supplier (Ranking A)";
                grid2row1.Cells[1].Value = ">= 90%";
                grid2row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGreen;
                grid2row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightGreen;

                grid2row2.Cells[0].Value = "Acceptable Supplier (Ranking B & C)";
                grid2row2.Cells[1].Value = "70% - 89%";
                grid2row2.Cells[0].Style.BackgroundBrush = PdfBrushes.Yellow;
                grid2row2.Cells[1].Style.BackgroundBrush = PdfBrushes.Yellow;

                grid2row3.Cells[0].Value = "Under Performing Supplier (Ranking D & E)";
                grid2row3.Cells[1].Value = "< 70%";
                grid2row3.Cells[0].Style.BackgroundBrush = PdfBrushes.Red;
                grid2row3.Cells[1].Style.BackgroundBrush = PdfBrushes.Red;

                //在指定为绘入表格
                grid2.Draw(page, new PointF(page.Canvas.ClientSize.Width * 0.2f, height + 270));

                page.Canvas.DrawString("Areas for Improvement (Rank D or E)", font_normal, PdfBrushes.Black, 0, height + 340);
                PdfTextBoxField textboxbig = new PdfTextBoxField(page, "Improvement");
                textboxbig.Bounds = new RectangleF(0, height + 360, page.Canvas.ClientSize.Width, 100);
                textboxbig.Multiline = true;
                textboxbig.ForeColor = new PdfRGBColor(Color.Black);
                textboxbig.Font = font_thin;
                textboxbig.Text = @"Example:"
+ Environment.NewLine + "Overall Quality: Prevent RMA cases."
+ Environment.NewLine + "Delivery: Avoid shipment delays past confirmed dates."
+ Environment.NewLine + "Responsiveness: Shorten email response rate and order schedule confirmation time."
+ Environment.NewLine + "Price & Payment Term: Support in cost down requests and better credit term.";
                pdf.Form.Fields.Add(textboxbig);

                page.Canvas.DrawString("Action Plan (Rank D or E)", font_normal, PdfBrushes.Black, 0, height + 480);
                PdfTextBoxField textboxbig2 = new PdfTextBoxField(page, "ActionPlan");
                textboxbig2.Bounds = new RectangleF(0, height + 500, page.Canvas.ClientSize.Width, 100);
                textboxbig2.Multiline = true;
                textboxbig2.ForeColor = new PdfRGBColor(Color.Black);
                textboxbig2.Font = font_thin;
                pdf.Form.Fields.Add(textboxbig2);

                page.Canvas.DrawString("*Supplier whose rank falls under D or E must complete the Areas for Improvement and Action Plan sections and return the scorecard copy, signed and dated, ", font_thinner_ita, PdfBrushes.Black, 0, height + 620);
                page.Canvas.DrawString("within 2 weeks of scorecard date.", font_thinner_ita, PdfBrushes.Black, 0, height + 630);

                page.Canvas.DrawString("Completed By:", font_normal, PdfBrushes.Black, 0, height + 650);
                PdfPen pen = new PdfPen(PdfBrushes.Black, 0.5f);
                pen.DashStyle = PdfDashStyle.Solid;
                page.Canvas.DrawLine(pen, new PointF(115, height + 665), new PointF(280, height + 665));
                page.Canvas.DrawLine(pen, new PointF(315, height + 665), new PointF(480, height + 665));
                page.Canvas.DrawString("(Signature)", font_thinner, PdfBrushes.Black, 150, height + 670);
                page.Canvas.DrawString("(Print Name)", font_thinner, PdfBrushes.Black, 350, height + 670);

                page.Canvas.DrawString("Title:", font_normal, PdfBrushes.Black, 0, height + 690);
                page.Canvas.DrawLine(pen, new PointF(50, height + 705), new PointF(280, height + 705));
                page.Canvas.DrawString("Date:", font_normal, PdfBrushes.Black, 290, height + 690);
                page.Canvas.DrawLine(pen, new PointF(335, height + 705), new PointF(480, height + 705));

                page.Canvas.DrawString("12-12-0002 rev3", font_thinner, PdfBrushes.Black, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height - 10, rightalign);

                var stream = new MemoryStream();
                pdf.SaveToStream(stream, FileFormat.PDF);
                byte[] fileBytes = stream.GetBuffer();
                string fileName = "Test.pdf";
                return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
            }
            catch (Exception ex)
            {
                return View("Error");
            }
        }

Leo.Liu.Us
 
Posts: 3
Joined: Thu Jul 28, 2022 3:02 pm

Fri Jul 29, 2022 7:07 am

Hello Leo Liu,

感谢你的来信。
我使用了最新版本的Spire.PDF Pack(Hot Fix) Version:8.7.9测试了您的代码,但没有重现您所提到的问题, 附件是我测试的结果文档。如果您使用的其他版本,我建议您先使用这个版本再重新测试下。如果您的测试结果文档还是不能用Adobe打开,为了我们进一步调查,请帮忙提供下面更多信息:
1.您的测试系统环境,例如win10,X64。
2.您的项目框架,例如.NET framework 4.0
3.您使用的Adobe 版本信息。

Sincerely
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Fri Jul 29, 2022 4:22 pm

Hi 您好,

我使用最新版8.7.9还是会有同样的错误发生。

1, 系统环境: Windows10 Pro x64 version : 21H2
2, 项目框架:.Net framework 4.6.1
3,Adobe Acrobat Reader DC(32 bit)

Leo.Liu.Us
 
Posts: 3
Joined: Thu Jul 28, 2022 3:02 pm

Mon Aug 01, 2022 11:03 am

Hello Leo Liu,

感谢您的回复。抱歉我之前保存使用的stream.ToArray, 我才注意到您保存文件的方式采用的是stream.GetBuffer(), 这样会导致使用Adobe打开会报错的问题。我将进一步调查下,再进一步给你反馈。

Sincerely
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Mon Aug 01, 2022 2:19 pm

非常感谢您提供的思路,使用ToArray果然没有报错了。

看了一下GetBuffer的Doc,可能是因为有unused的字节导致的这个错误,没有经过验证,只是猜测。

Remarks
Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.

Leo.Liu.Us
 
Posts: 3
Joined: Thu Jul 28, 2022 3:02 pm

Tue Aug 02, 2022 1:19 am

Hello Leo Liu,

感谢你的反馈和分享,是的,由于GetBuffer()和ToArray()本身机制的区别,GetBuffer()这种方式可能导致了保存的PDF数据损坏,因此adobe无法打开。所以请使用ToArray()来将流写入PDF文件。

Sincerely
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Return to 中文技术支持