在使用itextsharp库进行pdf生成时,我遇到了这种方法:-
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(itextsharp.text.pdf.PdfTemplate);
在哪里,我们可以从PdfTemplate获取Image实例。但是,我不知道如何创建PdfTemplate,也没有构造函数采用pdf文件名或流。
为什么要这样:我想从PDF文件创建一个图像,然后将该图像插入另一个pdf文件。
有人知道如何创建PdfTemplate对象吗?
参考方案
不幸的是,PdfTemplate
并非您真正想的那样。 iText和iTextSharp是PDF生成器,但不是PDF渲染器,这是将PDF转换为图像所需要的。
也就是说,您仍然可以根据所需的质量来实现目标。
PdfTemplate
的更常见用法之一是子类PdfImportedPage
。如果从Image
创建PdfImportedPage
,则不会创建JPG或PNG或任何栅格,实际上,页面的完整版本将包含在Image
对象中。这意味着您可以应用诸如ScaleAbsolute()
之类的转换或任何您想要的转换,但是当将其添加到输出PDF时,任何文本仍将是真实文本(因此可以选择)。这是质量的体现。如果开始缩放Image
,它将(在数学上)完美缩放,但在视觉上,它可能在Adobe Reader之类的图形中呈现不完美的效果。如果放大,则可以,但是许多屏幕程序无法很好地渲染小字体。我不知道这是否对您有问题。
无论如何,下面的代码是针对iTextSharp 5.1.1.0的完整工作示例。它从现有的PDF读取页面,将页面缩放50%并将其添加到输出PDF。
using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//PDF file to pull the first page from
string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Input.pdf");
//PDF file to output
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (Document doc = new Document())
{
using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
{
//Open our PDF for writing
doc.Open();
//We need a reader to pull pages from
PdfReader r = new PdfReader(inputFile);
//Get the first page of our source PDF
PdfImportedPage importedPage = w.GetImportedPage(r, 1);
//Insert a new page into our output PDF
doc.NewPage();
//Create an Image from the imported page
iTextSharp.text.Image Img = iTextSharp.text.Image.GetInstance(importedPage);
//Just to show why we are using an Image, scale the Image to 50% width and height of the original page
Img.ScaleAbsolute(importedPage.Width / 2, importedPage.Height / 2);
//Add the Image to the page
doc.Add(Img);
//Close our output PDF
doc.Close();
}
}
}
this.Close();
}
}
}
合并PDF iTextSharp - c#我在网上查看了一些示例,并想出了这段代码来使用iTextSharp合并pdf。但我得到一个错误:。{“文件没有页。”}它在Page = writer.GetImportedPage(reader,X);处失败;这是堆栈跟踪: at iTextSharp.text.pdf.PdfPages.WritePageTree() at iTextSharp.text.…
Codeigniter如何创建PDF - php我将创建一个发票系统,现在正在为此做准备。我要使用Codeigniter。问题是我想以PDF格式创建发票,以便可以通过电子邮件发送。你们有什么建议?我当时正在考虑将HTML转换为PDF,或在屏幕上显示发票并安装pdf打印机(当您点击print并选择打印为PDF文件时)或创建一个word文档并使用odbc连接到我认为的MySQL服务器。你们中有人有经验吗? 参…
LeetCode题解计算机为什么是基于二进制的?可以是三进制么?二进制有什么好处?题解:为什么叫电子计算机?算盘应该没有二进制
LeetCode题解统计城市的所有灯泡这个是我刚毕业的时候,一个真实的面试题,这是一个开放题。题目描述:想办法,将一个城市的所有灯泡数量统计出来。题解:费米估算法1、如果某个城市常驻人口有1000万2、假设每5人居住在一套房里,每套房有灯泡5只,那么住宅灯泡共有1000万只3、假设公众场所每10人共享一只灯泡,那么共有100万只4、主要的这两者相加就得出了1100万只当然实际上这是估算的,具体应…
LeetCode题解黑白圆盘一个圆盘被涂上了黑白二色,两种颜色各占一个半圆。圆盘以一个未知的速度、按一个未知的方向旋转。你有一种特殊的相机可以让你即时观察到圆上的一个点的颜色。你需要多少个相机才能确定圆盘旋转的方向?题解:可以用一个相机即可