用c语言怎么获得PDF文档页数,用C语言生成数百页的Word/PDF太慢了

博主在C#中遇到生成600多页文档速度慢的问题,尝试了DocX库和HTML转Word方法,但都存在不足。DocX使用InsertDocument功能导致效率低下,HTML转Word则无法内嵌图片。考虑过HTML到PDF的转换,如iTextSharp,但依然耗时。寻求快速生成1-5秒内的解决方案,对商业产品持保留态度。
摘要由CSDN通过智能技术生成

I'm having speed issue generating documentation in C#.

I am basically trying to create documents with 600+ pages. But the tools I have used handle this very slowly.

I first tried using DocX by Novacode. Creation of this document with 600+ pages takes upwards to 3 minutes. I learned that there could be an issue with the function "InsertDocument" so I tried to find a different solution.

I started looking into opening a HTML document into word. While this is a fast solution, images are not embedded into the document. The HTML syntax (src="https://img-blog.csdnimg.cn/2022010709473182358.png") is not supported in MS Word.

I could use URLs to the images, but then if the internet connection is down, the images would not display.

I then started looking into a HTML->PDF solution. iTextSharp is a little faster than the DocX solution, but still takes 1-2 minutes to generate this document.

I am simply out of ideas. I'm not sure a commercial product would be better, and I don't want to shell out that kind of cash, to just have the same speed issue.

Has anyone had experience with creating Word/PDF documents with 600+ pages in C# that is fairly quick (1-5 seconds).

要在C语言生成PDF文档,可以使用一些第三方库,如libharu、PDFlib、cairo等。这里以libharu为例介绍如何生成PDF文档。 1. 下载并安装libharu库:可以从libharu官网(http://libharu.org/)下载源码,然后按照README文件中的说明进行编译和安装。 2. 编写C程序:在C语言中使用libharu生成PDF文档的基本步骤如下: (1) 定义一个HPDF_Doc对象,表示PDF文档。 (2) 调用HPDF_New函数创建一个新的PDF文档,并设置一些文档属性。 (3) 调用HPDF_AddPage函数向文档添加一。 (4) 在每一上绘制一些图形或文本。 (5) 调用HPDF_SaveToFile函数将PDF文档保存到文件。 (6) 调用HPDF_Free函数释放HPDF_Doc对象。 下面是一个简单的例子: ```c #include <stdio.h> #include <stdlib.h> #include <libharu/hpdf.h> int main(void) { HPDF_Doc pdf; HPDF_Page page; HPDF_Font font; HPDF_REAL width, height; const char* text = "Hello, PDF!"; /* Create new PDF document */ pdf = HPDF_New(NULL, NULL); if (!pdf) { printf("Error: cannot create PDF document\n"); return EXIT_FAILURE; } /* Add a new page */ page = HPDF_AddPage(pdf); if (!page) { printf("Error: cannot add new page\n"); HPDF_Free(pdf); return EXIT_FAILURE; } /* Set font and font size */ font = HPDF_GetFont(pdf, "Helvetica", NULL); HPDF_Page_SetFontAndSize(page, font, 24); /* Get page size */ width = HPDF_Page_GetWidth(page); height = HPDF_Page_GetHeight(page); /* Draw text */ HPDF_Page_BeginText(page); HPDF_Page_MoveTextPos(page, (width - HPDF_Page_TextWidth(page, text))/2, height - 50); HPDF_Page_ShowText(page, text); HPDF_Page_EndText(page); /* Save PDF document to file */ if (HPDF_SaveToFile(pdf, "output.pdf") != HPDF_OK) { printf("Error: cannot write PDF document to file\n"); HPDF_Free(pdf); return EXIT_FAILURE; } /* Free PDF document */ HPDF_Free(pdf); return EXIT_SUCCESS; } ``` 3. 编译并运行程序:将上述代码保存为一个C源文件(如pdfgen.c),然后使用下面的命令编译: ``` gcc -o pdfgen pdfgen.c -lhpdf ``` 运行程序: ``` ./pdfgen ``` 就会在当前目录下生成一个名为output.pdfPDF文档。 需要注意的是,使用libharu生成PDF文档的过程中,需要注意设置文档属性、字体、文字大小、颜色等参数,以确保生成PDF文档满足要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值