我有一个PDF,我想添加一个额外的页面,理想情况下作为第一页.我已经能够用PDFSharp实现这一点,但问题是原始PDF包含书签,我想维护.使用PDFSharp似乎删除书签,或者至少我不知道使用包含附加页面的新创建的PDF保存原始TOC的任何选项或命令.
有没有人知道如何保持TOC与PDFSharp或任何其他.NET库,理想是免费的,这将允许我添加页面到现有的PDF并维护其书签?(我知道添加页面作为第一页会使页面引用无效,这就是为什么在最后一页添加页面也没问题.)
谢谢大家!
我正在使用 PDFSharp/Migradoc 从我的 Web 应用程序生成 PDF,并且我已经设法为文档的第一页获取背景图像。
我首先在文档中创建一个部分,然后为其渲染图像。然后,我将文档内容写入同一节对象内的段落对象。
但是,我需要为文档中的所有后续页面显示不同的背景图像。
这可能吗?如果是这样,我该怎么办?
答案: 为了提供完整的答案,以下是使其正常工作的基本代码:
Section section = this.document.AddSection();
section.PageSetup.DifferentFirstPageHeaderFooter = true;
section.PageSetup.OddAndEvenPagesHeaderFooter = false;
Image firstPageImage = section.Headers.FirstPage.AddImage("firstPage.jpg");
// ...configure image...
Image otherPageImage = section.Headers.Primary.AddImage("everyOtherPage.jpg");
// ...configure image...
Run Code Online (Sandbox Code Playgroud) 在我的解决方案中,我使用PDF文件中的项目符号列表.
看起来像这样:
• Solcellepaneler kræver hverken autoriseret service eller tidskrævende vedligehold.
• Solceller er støjfri, forurener ikke og har ingen bevægelige dele, hvilket mindsker
service og vedligehold
• Solceller kan integreres i bygningers arkitektur eller anvendes som
bygningselement i form af tag, facader eller solafskærmning
• Solceller har lang levetid, med en produktionsgaranti på hele 25 år
• 10 kvadrameter solceller sparer ca. ½ ton CO2 om året
Run Code Online (Sandbox Code Playgroud)
我想要的是 :
• Solcellepaneler kræver hverken autoriseret service eller tidskrævende vedligehold.
• …Run Code Online (Sandbox Code Playgroud) 我经常在一个OutOfMemoryException内部创建并处理一些字节数组的方法.代码如下所示:
当这个方法被调用20-30次时,我得到OutOfMemoryException了分配字节数组的位置.但我不认为这是系统内存问题.应用程序内存使用量约为500MB(专用工作集),测试机器为64位,内存为4GB.
字节数组使用的内存是否可能MemoryStream在方法完成后未释放?但是,由于私有工作集只有500MB左右,因此看起来这个内存不会分配给进程.
OutOfMemoryException在物理内存不足的情况下,什么时候可能导致创建大字节数组(60MB)?
[编辑添加代码示例] 源来自PdfSharp lib
排除异常byte[] imageBits = new byte[streamLength];它确实看起来像LOH碎片问题.
/// <summary>
/// Reads images that are returned from GDI+ without color palette.
/// </summary>
/// <param name="components">4 (32bpp RGB), 3 (24bpp RGB, 32bpp ARGB)</param>
/// <param name="bits">8</param>
/// <param name="hasAlpha">true (ARGB), false (RGB)</param>
private void ReadTrueColorMemoryBitmap(int components, int bits, bool hasAlpha)
{
int pdfVersion = Owner.Version;
MemoryStream memory = new MemoryStream(); …Run Code Online (Sandbox Code Playgroud) 如何使用 PDFSharp 从 PDF 文档中提取经过 FlateDecoded(例如 PNG)的图像?
我在 PDFSharp 示例中发现了该评论:
// TODO: You can put the code here that converts vom PDF internal image format to a
// Windows bitmap
// and use GDI+ to save it in PNG format.
// [...]
// Take a look at the file
// PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.
Run Code Online (Sandbox Code Playgroud)
有人有解决这个问题的办法吗?
感谢您的回复。
编辑:因为我无法在 8 小时内回答我自己的问题,所以我这样做:
感谢您的快速回复。
我在方法“ExportAsPngImage”中添加了一些代码,但没有得到想要的结果。它只是提取了更多图像(png),它们没有正确的颜色并且扭曲了。
这是我的实际代码:
PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
byte[] decodedBytes = flate.Decode(bytes);
System.Drawing.Imaging.PixelFormat pixelFormat; …Run Code Online (Sandbox Code Playgroud) 我正在使用 PDFSharp 生成一个填充了字段的 PDF 文档。保存文档后,我希望它是只读的,也就是扁平化的。我已经尝试了下面的方法,但是在 Adobe 中打开 PDF 时,这些字段仍然是可编辑的。
using (PdfDocument form = PdfReader.Open(outputFormLocation , PdfDocumentOpenMode.Modify))
{
//do stuff...
//Save
PdfSecuritySettings securitySettings = form.SecuritySettings;
securitySettings.PermitFormsFill = false;
securitySettings.PermitModifyDocument = false;
securitySettings.PermitPrint = true;
form.Save(outputFormLocation);
Run Code Online (Sandbox Code Playgroud) 我正在使用 MigraDoc 在部署到 Azure 云的 ASP.NET5 MVC6 Web 应用程序中生成 PDF。我使用的是 1.50 beta-2 版本,但我也尝试过使用 v1.50 beta-1 和 v1.32。
\n\n当应用程序在本地运行时,我已成功生成 PDF。然而,当应用程序在云服务器上运行时,由于无法访问任何字体,我在生成 PDF 时遇到了很大的问题。 按照 PDFsharp 文档,我尝试通过在代码中嵌入字体来创建“私有字体”。
\n\n我直接使用 PDFsharp 在云上成功生成了 PDF
\n\n public static MyResolver FontResolver = new MyResolver();\n public void RenderPdf(CreateDocumentViewModel viewModel)\n {\n GlobalFontSettings.FontResolver = FontResolver;\n //...\n XFont font = new XFont("times-roman", 12, XFontStyle.Regular);\n //This font is then used in DrawString.\n }\nRun Code Online (Sandbox Code Playgroud)\n\n不过,我现在想利用 MigraDoc,这样我就不必自己完成所有排版工作。
\n\n我在这里遵循了 Thomas H\xc3\xb6vel\ 博客上的优秀指南(这是 beta-2 的新指南,尽管我之前也关注过他之前关于 beta-1 的帖子)。他的项目在本地非常适合我。
\n\n …PDFSharp 中是否有任何方法可以将数字签名嵌入 pdf 文件中。
我目前正在评估如何从 Xamarin.Forms 生成 PDF(当前仅在 Android 上运行该应用程序)并检查PdfSharp 的 .NET Standard 端口。
绘制 PDF 并显示它可以工作,但我在向文档写入文本时遇到问题。当我尝试XFont使用以下代码加载时
var font = new XFont("sans-serif", 20);
Run Code Online (Sandbox Code Playgroud)
它失败了,但有例外
System.InvalidOperationException:找不到合适的字体。
根据这些示例,它应该以这种方式工作,但它们适用于 PdfSharp.Xamarin 而不是 PdfSharp .NET Standard。根据这个答案,字体"sans-serif"系列应该是正确的,但我拼命尝试了其他选项,例如"Roboto",但无济于事。
PdfSharp for .NET Standard 是否与 Xamarin 兼容?(它将 PdfSharp.Xamarin 列为创建它的来源,因此我假设它。)还有什么我错过的吗?
编辑
我尝试了 PdfSharp.Xamarin,它确实有效。显然这是 .NET Standard 端口的问题。