我有一个ASPX页面,它可以生成报告。我有一个使用ITextSharp构建pdf文件的打印按钮。现在我要打印该文件。
我有两个问题:
如何在不保存文件的情况下打印它?
如果无法执行此操作,是否可以至少打印保存的文件?
提前致谢。
所以,我在使用C#(.NET 4.0 + WinForms)和iTextSharp 5.1.2时遇到了这个问题.
我有一些扫描图像存储在数据库中,需要与这些图像一起构建.有些文件只有一页,其他有几百页.这工作正常使用:
foreach (var page in pages)
{
Image pageImage = Image.GetInstance(page.Image);
pageImage.ScaleToFit(document.PageSize.Width,document.PageSize.Height);
pageImage.Alignment = Image.ALIGN_TOP | Image.ALIGN_CENTER;
document.Add(pageImage);
document.NewPage();
//...
}
Run Code Online (Sandbox Code Playgroud)
问题是:
我需要在最后一页的底部添加一个小表.
我尝试:
foreach (var page in pages)
{
Image pageImage = Image.GetInstance(page.Image);
pageImage.ScaleToFit(document.PageSize.Width,document.PageSize.Height);
pageImage.Alignment = Image.ALIGN_TOP | Image.ALIGN_CENTER;
document.Add(pageImage);
document.NewPage();
//...
}
Table t = new table....
document.Add(t);
Run Code Online (Sandbox Code Playgroud)
表已成功添加,但如果图像的大小符合文档的页面大小,则表格将添加到下一页.
我需要调整文档的最后一个图像(如果它有多个,或第一个if只有1),以便将表直接放在该页面上(带有图像),并且两个都只是一页.
我尝试按百分比缩放图像,但是假设最后一页上的图像的图像大小是未知的,并且它必须填充页面的最大部分,我需要做到这一点.
任何的想法?
我正在C#MVC应用程序中创建PDF发票,我附加到电子邮件并发送,理想情况下,一旦发送电子邮件,我想删除发票以释放服务器空间并提高隐私/安全性.我已编写代码来执行此操作,但它在50%的时间内失败,因为该文件被另一个进程锁定(我不确定是创建/写入进程是锁定它还是电子邮件发送).我正在异步发送电子邮件(即删除的代码在电子邮件发送之前不会执行).
我很欣赏一些如何处理这个问题的技巧.我可以找一份工作来清理旧文件,但我更愿意在我去的时候清理它们...
我忘了提到我正在使用iTextSharp生成PDF - 它的关键是这个代码用于生成最终发票(我能够删除作为参数传入的文件列表而不用戏剧):
/// <summary>
/// http://stackoverflow.com/questions/4276019/itextsharp-pdfcopy-use-examples
/// </summary>
/// <param name="fileNames"></param>
/// <param name="outFile"></param>
private void CombineMultiplePDFs(List<string> files, string outFile)
{
int pageOffset = 0;
int f = 0;
iTextSharp.text.Document document = null;
PdfCopy writer = null;
foreach (string file in files)
{
// we create a reader for a certain document
PdfReader reader = new PdfReader(file);
reader.ConsolidateNamedDestinations();
// we retrieve the total number of pages
int n = reader.NumberOfPages;
pageOffset += n;
if (f == 0) …Run Code Online (Sandbox Code Playgroud) 我想使用UTF-8而不是CP1250来显示国家字符.我找到了支持UTF-8的AddParagraph方法,但我找不到任何压模示例.
这是CP1250的代码片段:
PdfReader reader = new PdfReader(templatePath);
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
PdfContentByte cb = stamper.GetOverContent(1);
BaseFont bf = BaseFont.CreateFont(
BaseFont.COURIER_BOLD,
BaseFont.CP1250,
true);
//Begin text command
cb.BeginText();
//Set the font information
cb.SetFontAndSize(bf,12f);
//Position the cursor for drawing
cb.MoveText(field.X, field.Y);
//Write some text
cb.ShowText(field.Text);
//End text command
cb.EndText();
//Flush the PdfStamper's buffer
stamper.Close();
//Get the raw bytes of the PDF
bytes = ms.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使用UTF-8?
我有一个可填写的pdf.我几乎没有文本框.
我使用以下代码填充这些字段(itextsharp).
DataTable dt = new DataTable();
String pdfPath1 = Server.MapPath("pdfs\\transmittal2.pdf");
if (File.Exists(pdfPath1))
{
dt = objClsTransmittal.GetTransmittal(jobid, cid);
String comment = "Correspondence generated for " + dt.Rows[0]["Recipient"].ToString();
var formfield = PDFHelper.GetFormFieldNames(pdfPath1);
formfield["DocDate"] = DateTime.Now.ToLongDateString();
formfield["Address1"] = dt.Rows[0]["Company"].ToString();
formfield["Address2"] = dt.Rows[0]["Address1"].ToString();
formfield["PropertyAddress"] = dt.Rows[0]["PropertyAddress"].ToString();
formfield["Job"] = dt.Rows[0]["JobID"].ToString();
formfield["Name"] = dt.Rows[0]["Recipient"].ToString();
formfield["CityStateZip"] = dt.Rows[0]["address2"].ToString();
formfield["E-mail"] = dt.Rows[0]["Email"].ToString();
var pdfcontent = PDFHelper.GeneratePDF(pdfPath1, formfield);
PDFHelper.ReturnPDF(pdfcontent, "Transmittal.pdf");
}
Run Code Online (Sandbox Code Playgroud)
目前其下载为只读pdf.
当这个pdf下载时,我希望所有字段仍然可填写,文本我填写了pdf.这样我就可以编辑文本了.
我很期待你的回复.
谢谢.
编辑
PdfHelper是我的自定义类.我用过以下代码:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using …Run Code Online (Sandbox Code Playgroud) 我怎么会合并多个PDF页面转换成一个与iTextSharp它同时还支持有合并的网页形式的元素,如textboxes,checkboxes等.
我通过谷歌搜索尝试了这么多,但没有什么运作良好.
我正在使用itextsharp来创建文档.我在文档中创建了一个页脚部分,但页脚顶部的内容重叠.
我怎样才能解决这个问题?
我想在每个页面上添加页码如何才能这样做?
请检查此链接 Pdf文档图像
public class PdfNote
{
public int Create(int id, string path)
{
try
{
var file = path;
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(file, FileMode.Create));
document.Open();
writer.PageEvent = new Footer();
// main content
document.Close();
return 1;
}
catch (Exception ex)
{
throw ex;
}
}
public partial class Footer : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
{
PdfPTable fTbl = new …Run Code Online (Sandbox Code Playgroud) 现在我正在使用iTextSharp从PDF中提取线条和矩形.我使用的方法如下:
PdfReader reader = new PdfReader(strPDFFileName);
var pageSize = reader.GetPageSize(1);
var cropBox = reader.GetCropBox(1);
byte[] pageBytes = reader.GetPageContent(1);
PRTokeniser tokeniser = new PRTokeniser(new(RandomAccessFileOrArray(pageBytes));
PRTokeniser.TokType tokenType;
string tokenValue;
CoordinateCollection cc = new CoordinateCollection();
while (tokeniser.NextToken())
{
tokenType = tokeniser.TokenType;
tokenValue = tokeniser.StringValue;
if (tokenType == PRTokeniser.TokType.OTHER)
{
if (tokenValue == "re")
{
if (buf.Count < 5)
{
continue;
}
float x = float.Parse(buf[buf.Count - 5]);
float y = float.Parse(buf[buf.Count - 4]);
float w = float.Parse(buf[buf.Count - 3]);
float h = float.Parse(buf[buf.Count …Run Code Online (Sandbox Code Playgroud) 使用itextsharp在浏览器中不显示突出显示的单词.
土砖
浏览器
码
List<iTextSharp.text.Rectangle> MatchesFound = strategy.GetTextLocations(splitText[i].Trim(), StringComparison.CurrentCultureIgnoreCase);
foreach (Rectangle rect in MatchesFound)
{
float[] quad = { rect.Left - 3.0f, rect.Bottom, rect.Right, rect.Bottom, rect.Left - 3.0f, rect.Top + 1.0f, rect.Right, rect.Top + 1.0f };
//Create our hightlight
PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad);
//Set the color
highlight.Color = BaseColor.YELLOW;
//Add the annotation
stamper.AddAnnotation(highlight, pageno);
}Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题.
更新代码
private void highlightPDF()
{
//Create a simple test file
string outputFile = Server.MapPath("~/pdf/16193037V_Dhana-FI_NK-QA_Completed.pdf");
string filename = "HL" + Convert.ToString(Session["Filename"]) …Run Code Online (Sandbox Code Playgroud) 我在ASP.NET C#中使用iTextSharp生成PDF文件。
我尝试为几个元素生成一个标题+表。在每个元素之后,我想开始一个新页面。我这样做,doc.NewPage()但是当我生成PDF文件时,打开文件时出现以下错误:
加载PDF文档时出错
尝试在最新版本的Google Chrome浏览器中打开PDF文件时收到此错误。当我尝试在Adobe Reader中打开文件时,出现以下错误消息:
打开此文档时出错。该文件已损坏,无法修复。
PDF档案的大小也只有1kb ...
这是我如何生成文件的代码:
//Create a byte array that will eventually hold our final PDF
//must be outside of the foreach loop (and everything else), because we store every single generated table in here for the final pdf!!
Byte[] bytes;
List<TableObject> myTables = getTables();
TableObject currentTable = new TableObject();
//Boilerplate iTextSharp setup here
//Create a stream that we can write to, in this case a MemoryStream
using (MemoryStream ms = new …Run Code Online (Sandbox Code Playgroud) itextsharp ×10
c# ×8
pdf ×6
asp.net ×3
itext ×2
asp.net-mvc ×1
delete-file ×1
html ×1
image ×1
printing ×1
utf-8 ×1