当前位置: 首页 > 知识库问答 >
问题:

iTextSharp PDF旋转页面移位

阎鸿煊
2023-03-14

我尝试使用iTextSharp创建一个多页pdf文档。我有一个包含自身方向(横向或纵向)的对象。当第一个对象包含需要横向模式的信息时,我用< code > Document doc = new Document(PageSize。A4.Rotate(),10f,10f,10f,0f)。这工作得很好,直到下一个元素是肖像模式!如果一个元素处于纵向模式,我再次设置页面大小:< code>doc。SetPageSize(PageSize。a4);。

此时,该元素应该位于PDF文档中的纵向A4页面上,但仍处于横向模式。在到达新对象或当前元素中的分页符之前,它不会切换页面!

以下是我的代码:

TableObject to_first = myTables.First();
//current object need landscape orientation
if (to_first._orientation == "landscape")
{
    //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
    using (Document doc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f))
    {
        //Create a writer that's bound to our PDF abstraction and our stream
        using (PdfWriter writer = PdfWriter.GetInstance(doc, ms))
        {
            //Open the document for writing
            doc.Open();

            //writer.CloseStream = false;
            //loop all tableobjects inside the document & the instance of PDFWriter itself! 
            foreach (TableObject to in myTables.ToList())
            {
                doc.NewPage();
                //look for the requested orientation by the current object and apply it
                if (to._orientation == "landscape")
                {
                    doc.SetPageSize(PageSize.A4.Rotate());
                }
                else if (to._orientation == "portrait")
                {
                    doc.SetPageSize(PageSize.A4);
                }
                currentTable = to;
                //Get the data from database corresponding to the current tableobject and fill all the stuff we need!
                DataTable dt = getDTFromID(currentTable._tableID);
                Object[] genObjects = new Object[5];
                genObjects = gen.generateTable(dt, currentTable._tableName, currentTable._tableID.ToString(), currentTable, true);

                StringBuilder sb = (StringBuilder)genObjects[1];
                String tableName = sb.ToString();
                Table myGenTable = (Table)genObjects[0];
                String table = genObjects[2].ToString();

                using (StringReader srHtml = new StringReader(table))
                {
                    //Parse the HTML
                    iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                }
            }

            //After all of the PDF "stuff" above is done and closed but **before** we
            //close the MemoryStream, grab all of the active bytes from the stream
            doc.Close();
            bytes = ms.ToArray();
        }
    }
}

如何保证每一页都旋转正确?

共有1个答案

尉迟明辉
2023-03-14

doc.SetPageSize仅设置用于创建新页面的大小,而不是现有页面的大小。因此,您应该移动您的

doc.NewPage();

SetPageSize调用后调用:

//look for the requested orientation by the current object and apply it
if (to._orientation == "landscape")
{
    doc.SetPageSize(PageSize.A4.Rotate());
}
else if (to._orientation == "portrait")
{
    doc.SetPageSize(PageSize.A4);
}

// After setting the page size, trigger the generation of the new page
doc.NewPage();
 类似资料:
  • 我在代码中实现旋转器时遇到了问题。不太清楚它是怎么工作的。但是这里有一个我想做的简单的想法。(例如:点击旋转器,你有两个选项“第2页”和“第3页”。所以,如果你点击“第2页”,它会转到有新内容的第2页。有什么方法可以做到这一点吗?? 我也有图像按钮去下一页和返回。只想实现一个旋转框,这样你就可以更快地导航到不同的页面。 } 05-22 01:32:40.058:E/SpannableStringB

  • 我想生产一个PDF的网页在景观。虽然可以使用以下方法将页面大小设置为横向: 这并没有达到我想要的,因为我添加的任何内容仍然是面向左->右的,而我希望它是底部->顶部的。 即。这就是我得到的: 我已经能够实现所需的输出打开PDF后,它已经创建并使用iText旋转它,但我想要一个解决方案,让我旋转后立即与iText添加内容。

  • 我正在尝试建立一个简单的“飞鸟”游戏,我需要鸟的图像倾斜,上升时指向上方,反之亦然。然而,当旋转我的图像时,它会在稍微下降或轻触屏幕后部分或完全从屏幕上消失。谁能告诉我怎么解决这个问题吗?

  • 本文向大家介绍如何自动转移到新的页面?相关面试题,主要包含被问及如何自动转移到新的页面?时的应答技巧和注意事项,需要的朋友参考一下

  • 我对jQuery移动锚有问题。当我点击带有某个锚的_blank链接时,我会重定向到新选项卡中的新页面并导航到锚链接。但是当页面完全加载时,页面跳转到页面顶部

  • 8.1节给大家提到过纹理对象Texture,简单的说纹理对象Texture就是包含一张图片的对象,纹理对象Texture所包含的图片就是.image属性,除此外,纹理对象Texture还提供了一些实际开发中经常会用到的属性和方法。 阵列 纹理贴图阵列映射。 var texture = textureLoader.load('太阳能板.png'); // 设置阵列模式 默认ClampToEdge