我有一堆PDF,我只是想打开,调整页面大小,然后保存.我也希望文件大小会显着缩小.我正在使用iTextSharp并且调整大小工作正常,但文件大小几乎相同,实际上稍大一点.这是我现在的功能:
Dim reader As New PdfReader(inPDF)
Dim doc As New Document(PageSize.LETTER)
Document.Compress = True
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(outPDF, FileMode.Create))
doc.Open()
Dim cb As PdfContentByte = writer.DirectContent
Dim page As PdfImportedPage
For pageNumber As Long = 1 To reader.NumberOfPages
page = writer.GetImportedPage(reader, pageNumber)
cb.AddTemplate(page, PageSize.LETTER.Width / reader.GetPageSize(pageNumber).Width, 0, 0, PageSize.LETTER.Height / reader.GetPageSize(pageNumber).Height, 0, 0)
doc.NewPage()
Next pageNumber
doc.Close()
Run Code Online (Sandbox Code Playgroud)
有谁知道我可能缺少什么实际上也减少了文件大小?
谢谢.
我正在尝试从HTML页面中创建PDF.我正在使用的CMS是EPiServer.
到目前为止这是我的代码:
protected void Button1_Click(object sender, EventArgs e)
{
naaflib.pdfDocument(CurrentPage);
}
public static void pdfDocument(PageData pd)
{
//Extract data from Page (pd).
string intro = pd["MainIntro"].ToString(); // Attribute
string mainBody = pd["MainBody"].ToString(); // Attribute
// makae ready HttpContext
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
// Create PDF document
Document pdfDocument = new Document(PageSize.A4, 80, 50, 30, 65);
//PdfWriter pw = PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);
PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);
pdfDocument.Open();
pdfDocument.Add(new Paragraph(pd.PageName));
pdfDocument.Add(new Paragraph(intro));
pdfDocument.Add(new Paragraph(mainBody));
pdfDocument.Close();
HttpContext.Current.Response.End();
}
Run Code Online (Sandbox Code Playgroud)
这将输出文章名称,简介和主体的内容.但它没有解析文章文本中的HTML,也没有布局.
我试过看看http://itextsharp.sourceforge.net/tutorial/index.html,而不是更聪明.
任何指向正确方向的指针都非常感谢:)
我目前正在使用iTextSharp转换一些遗留代码来创建PDF文件.我们正在创建一个包含大量图像的大型PDF文件,我正在插入这样的图像:
Document doc = new Document(PageSize.A4, 50, 50, 25, 25);
PdfWriter writer = PdfWriter.GetInstance(doc, myStream);
writer.SetFullCompression();
doc.Open();
Image frontCover = iTextSharp.text.Image.GetInstance(@"C:\MyImage.png");
//Scale down from a 96 dpi image to standard itextsharp 72 dpi
frontCover.ScalePercent(75f);
frontCover.SetAbsolutePosition(0, 0);
doc.Add(frontCover);
doc.Close();
Run Code Online (Sandbox Code Playgroud)
插入图像(20.8 KB png文件)似乎会使PDF文件大小增加近100 KB.
有没有办法在进入前压缩图像(记住这需要合理的打印质量),或进一步压缩整个PDF?我在上面的例子中是否进行了任何压缩?
我将简单地提出这个问题.
我有这个pdf:
_____
|abcd |
| |
| |
|_____|
Run Code Online (Sandbox Code Playgroud)
还有这个:
_____
|1234 |
|4567 |
| |
|_____|
Run Code Online (Sandbox Code Playgroud)
我想将它们合并以获得:
_____
|abcd |
|1234 |
|4567 |
|_____|
Run Code Online (Sandbox Code Playgroud)
可以使用iTextSharp或任何其他免费工具吗?
提前致谢
我必须创建一个带有两个表的pdf文件。并且这两张桌子应该水平放置在文档中。我已经尝试过这样,
var doc1 = new Document(PageSize.A4);
PdfWriter.GetInstance(doc1, new FileStream(path + "/" + pdf_name + "", FileMode.Create));
doc1.Open();
var table1 = new PdfPTable(1); //table1
table1.HorizontalAlignment = Element.ALIGN_LEFT;
table1.SpacingBefore = 50;
table1.DefaultCell.Border = 1;
table1.WidthPercentage = 40;
PdfPCell cell = new PdfPCell(new Phrase(student_name, boldTableFont));
// cell.Border = 1;
// cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(cell);
doc1.Add(table1);
var table2= new PdfPTable(1); //table2
table2.DefaultCell.Border = 1;
table2.HorizontalAlignment = 2;
table2.SpacingBefore = 50;
table2.WidthPercentage = 40;
PdfPCell cell21 = new PdfPCell(new …Run Code Online (Sandbox Code Playgroud) 我使用以下代码在我的WPF应用程序中使用iTextSharp XMLWorker解析HTML字符串:
var css = "";
using (var htmlMS = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
{
//Create a stream to read our CSS
using (var cssMS = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css)))
{
//Get an instance of the generic XMLWorker
var xmlWorker = XMLWorkerHelper.GetInstance();
//Parse our HTML using everything setup above
xmlWorker.ParseXHtml(writer, doc, htmlMS, cssMS, System.Text.Encoding.UTF8, fontProv);
}
}
Run Code Online (Sandbox Code Playgroud)
解析工作正常,但它真的很慢,解析HTML大约需要2秒.因此,对于50页的pdf,大约需要2分钟.我在我的HTML字符串中使用内联样式.这是自然行为还是可以优化?
我尝试使用ItextSharp在pdf表中对齐单元格内容。不知何故,它根本不起作用,始终在左侧对齐。
var pageSize = PageSize.A4;
if (_pdfSettings.LetterPageSizeEnabled)
{
pageSize = PageSize.LETTER;
}
var doc = new Document(pageSize);
PdfWriter.GetInstance(doc, stream);
doc.Open();
//fonts
var normalFont = GetFont();
normalFont.Color = BaseColor.BLACK;
normalFont.Size = 14;
//..titlefont, smallfont,largefont....
var addressTable = new PdfPTable(1);
addressTable.WidthPercentage = 100f;
cell = new PdfPCell();
cell.AddElement(new Paragraph("Ng??i G?i", titleFont));
cell.AddElement(new Paragraph("TAKARA.VN", largeFont));
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
addressTable.AddCell(cell);
doc.Add(addressTable);
doc.Add(new Paragraph("", normalFont));
Run Code Online (Sandbox Code Playgroud)
更新:我找到了答案
您在混淆文本模式和复合模式。
文字模式:
Phrase p = New Phrase("value");
PdfPCell cell = new PdfPCell(p);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
Run Code Online (Sandbox Code Playgroud)
复合模式:
PdfPCell cell …Run Code Online (Sandbox Code Playgroud) 我使用nuget在我的项目中成功安装了iTextSharp XMLWorker扩展.
但是,如果没有"添加引用"到iTextSharp XML Worker dll,我也无法使用它.
目前,在我的项目文件的解决方案资源管理器中,我右键单击"引用"并选择添加引用.从可用程序集列表中,我找不到任何名为iTextSharp或XML Worker的内容.
有谁知道我在这里做错了什么?
我正在尝试将iTextSharp与ASP.NET 5 Core一起使用.但是,当我尝试使用iTextSharp 5.5.5构建ASP.NET应用程序时,我收到了这些错误
码:
using Microsoft.AspNet.Mvc;
using System.IO;
using System;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace MyNamespace
{
public class GenerateFileController : Controller
{
// GET: /<controller>/
public string Index()
{
PdfReader reader = new PdfReader("template.pdf");
return "SomeText";
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
错误CS0012类型"Uri"在未引用的程序集中定义.您必须添加对程序集'System,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用.MyProject.ASP.NET 5.0 MyProject/Controllers\GenerateFileController.cs 17
错误CS0012类型"Stream"在未引用的程序集中定义.您必须添加对程序集'mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用.MyProject.ASP.NET Core 5.0 MyProject/Controllers\GenerateFileController.cs 17
当我尝试使用ASP.NET 4.6模板做同样的事情时,它工作正常.问题是我想在这个项目中使用ASP.NET 5 …
我有一个webform,我使用了ajax工具包HtmlEditorExtender1.它工作正常.现在我尝试在点击按钮时使用iTextSharp将整个webform转换为pdf.但是我收到了这个错误:
Extender control 'HtmlEditorExtender1' is not a registered extender control. Extender controls must be registered using RegisterExtenderControl() before calling RegisterScriptDescriptors().
Parameter name: extenderControl
Run Code Online (Sandbox Code Playgroud)
我相信这与它有关 HtmlEditorExtender1
<ajaxToolkit:HtmlEditorExtender ID="HtmlEditorExtender1" runat="server" TargetControlID="txtkeyresultforyear" EnableSanitization="false" />
Run Code Online (Sandbox Code Playgroud)
我的webform的顶部如下所示:
<%@ Page Title="" Language="C#" MasterPageFile="~/ModuleMain.master" AutoEventWireup="true"
ValidateRequest="false" CodeBehind="xyz.aspx.cs" EnableEventValidation = "false" Inherits="abc.xyz" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
Run Code Online (Sandbox Code Playgroud)
这个按钮上的代码点击:
protected void btntopdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr …Run Code Online (Sandbox Code Playgroud) itextsharp ×10
c# ×8
asp.net ×4
pdf ×2
.net ×1
asp.net-core ×1
asp.net-mvc ×1
vb.net ×1
webforms ×1