gpt4 book ai didi

.net - 告诉 abcPdf 缩放 html 以适合单个 pdf 页面

转载 作者:行者123 更新时间:2023-12-02 02:25:28 24 4
gpt4 key购买 nike

我正在使用 abcPdf 将 HTML 报告转换为 pdf 文件。 pdf 必须是单个横向 A4 页面。

您知道是否有任何方法可以告诉 abcPdf 缩放 HTML 页面以适合 pdf 中的单个页面?我尝试使用 Magnify() method ,它会缩放内容,但仍然将其分成页面,即使它适合一页。我对此已经摸不着头脑有一段时间了,想知道是否有人做到了。

这是我目前使用的代码:

public byte[] UrlToPdf(string url, PageOrientation po)
{
using (Doc theDoc = new Doc())
{
// When in landscape mode:
// We use two transforms to apply a generic 90 degree rotation around
// the center of the document and rotate the drawing rectangle by the same amount.
if (po == PageOrientation.Landscape)
{
// apply a rotation transform
double w = theDoc.MediaBox.Width;
double h = theDoc.MediaBox.Height;
double l = theDoc.MediaBox.Left;
double b = theDoc.MediaBox.Bottom;
theDoc.Transform.Rotate(90, l, b);
theDoc.Transform.Translate(w, 0);

// rotate our rectangle
theDoc.Rect.Width = h;
theDoc.Rect.Height = w;

// To change the default orientation of the document we need to apply a rotation to the root page object.
//By doing this we ensure that every page in the document is viewed rotated.
int theDocID = Convert.ToInt32(theDoc.GetInfo(theDoc.Root, "Pages"));
theDoc.SetInfo(theDocID, "/Rotate", "90");
}

theDoc.HtmlOptions.PageCacheEnabled = false;
theDoc.HtmlOptions.AddForms = false;
theDoc.HtmlOptions.AddLinks = false;
theDoc.HtmlOptions.AddMovies = false;
theDoc.HtmlOptions.FontEmbed = false;
theDoc.HtmlOptions.UseResync = false;
theDoc.HtmlOptions.UseVideo = false;
theDoc.HtmlOptions.UseScript = false;
theDoc.HtmlOptions.HideBackground = false;
theDoc.HtmlOptions.Timeout = 60000;
theDoc.HtmlOptions.BrowserWidth = 0;
theDoc.HtmlOptions.ImageQuality = 101;

// Add url to document.
int theID = theDoc.AddImageUrl(url, true, 0, true);
while (true)
{
if (!theDoc.Chainable(theID))
break;
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
}
//Flattening the pages (Whatever that means)
for (int i = 1; i <= theDoc.PageCount; i++)
{
theDoc.PageNumber = i;
theDoc.Flatten();
}

return theDoc.GetData();
}
}

最佳答案

这就是我解决这个问题的方法。

首先,我需要将 HTML 页面的高度传递给 pdf 生成方法,因此我将其添加到要进行 pdf 编辑的页面上:

<asp:HiddenField ID="hfHeight" runat="server" />

并在后面的代码中:

protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
string scriptKey = "WidhtHeightForPdf";
if (!Page.ClientScript.IsClientScriptBlockRegistered(scriptKey))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script>")
.AppendLine("document.getElementById('" + hfHeight.ClientID + "').value = document.body.clientHeight;")
.AppendLine("</script>");

Page.ClientScript.RegisterStartupScript(typeof(Page), scriptKey, sb.ToString());
}
}
}

现在,当我调用 pdf 生成方法时,我可以向它传递 HTML 的高度。一旦我有了高度,就只需计算 pdf“视口(viewport)”的宽度,使高度适合 pdf 页面即可:

int intHTMLWidth = height.Value * Convert.ToInt32(theDoc.Rect.Width / theDoc.Rect.Height);

然后通过 theDocHtmlOptions 指定 BrowserWidth 参数:

theDoc.HtmlOptions.BrowserWidth = intHTMLWidth;

或者将 url 添加到 theDoc 时:

int theID = theDoc.AddImageUrl(url, true, intHTMLWidth, true);

编辑:这解决了问题,所以我将其标记为答案。现在接下来要做的就是根据 HTML 的宽度和高度以纵向或横向模式创建 pdf,以便在 pdf 页面上使用最大空间。

关于.net - 告诉 abcPdf 缩放 html 以适合单个 pdf 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2132382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com