如何为.net在ABCPDF中配置landScape方向?

时间:2018-08-14 17:54:17

标签: c# asp.net .net visual-studio abcpdf

我曾经去过有关landScape示例的文档,但没有致力于将肖像更改为landScape方向。这是ABCPDF

中的示例

但是我在WebServices中使用了此代码,在该服务中我以参数形式接收HTML中的文件路径并将HTML转换为PDF,在这一部分还可以,但是当我尝试将方向转换为横向时对我来说不起作用。

这是我的代码

   [WebMethod]
    public string convertPDF(string filePath, string filePathPDF)
    {
        bool pdfBuilt = false;
        bool landscape = true;

        try
        {
            var theDoc = new Doc();

            //if (landscape)
            //{
            //    double w = theDoc.MediaBox.Width ;
            //    double h = theDoc.MediaBox.Height;
            //    double l = theDoc.MediaBox.Left;
            //    double b = theDoc.MediaBox.Bottom;
            //    theDoc.Transform.Rotate(90, 0, 0);
            //    theDoc.Transform.Translate(h, 0);
            //    // rotate our rectangle
            //    theDoc.Rect.Width = h;
            //    theDoc.Rect.Height = w;

            //}


            string pdGeneral = filePath;

            int theID = theDoc.GetInfoInt(theDoc.Root, "Pages");
            theDoc.SetInfo(theID, "/Rotate", "90");

            theDoc = CreateNewDocument(pdGeneral, theDoc);

            theDoc.Save(filePathPDF);
            theDoc.ClearCachedDecompressedStreams();
            theDoc.Clear();
            theDoc.Dispose();

             pdfBuilt = true;
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
            }

        return null;

    }

将HTML转换为PDF时,在CreateNewDocument函数中是可以的。

  private static Doc CreateNewDocument(string currentURL,Doc theDoc)
    {
         theDoc.HtmlOptions.PageCacheEnabled = false;
         theDoc.HtmlOptions.TargetLinks = true;
         theDoc.HtmlOptions.AddLinks = true;


        theDoc.HtmlOptions.Engine = EngineType.Chrome; 
        theDoc.Rect.Rectangle = new System.Drawing.Rectangle(20, 20, 650, 750);//755
        string pdfUrl = currentURL;


        try
        {


            int theID = theDoc.AddImageUrl(pdfUrl, true, (int)theDoc.MediaBox.Width, true);
            for (int i = 1; i <= 50; i++)
            {
                if (!theDoc.Chainable(theID))
                    break;
                theDoc.Page = theDoc.AddPage();
                theID = theDoc.AddImageToChain(theID);
            }


                theDoc.PageNumber = 1;


        }
        catch (Exception ex)
        {

            throw new ApplicationException("Error generating pdf..." + "Exception: " + ex + "<br/>URL for render: " + pdfUrl + "<br/>Base URL: " + currentURL);
        }

        return theDoc;
    }

希望您能对此有所帮助。

谢谢

0 个答案:

没有答案