小编kuu*_*nbo的帖子

让PdfStamper使用MemoryStreams(c#,itextsharp)

我找到了重新编写旧代码的方法,这些代码将PDF文件签名为新文件,这标志着来自Web服务的MemoryStreams(字节数组).简单吧?那是昨天.今天我无法让它发挥作用.

这是旧代码,它使用FileStreams并且它可以工作:

    public static string OldPdfSigner(PdfReader pdfReader, string destination, string password, string reason, string location, string pathToPfx)
    {
        using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
        {
            ...

            using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, new FileStream(destination, FileMode.Create, FileAccess.Write), '\0'))
            {
                PdfSignatureAppearance sap = st.SignatureAppearance;
                sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
                sap.Reason = reason;
                sap.Location = location;
                return destination;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

下面是我自己重做的内容,它抛出System.ObjectDisposedException:无法访问已关闭的Stream.

    public static byte[] PdfSigner(PdfReader pdfReader, string password, string reason, string location, string pathToPfx)
    {
        using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, …
Run Code Online (Sandbox Code Playgroud)

c# pdf memorystream itext pdfstamper

22
推荐指数
1
解决办法
2万
查看次数

ITextSharp:设置表格单元格边框颜色

如何设置表格单元格的边框颜色.这是我的代码:

// create and define table
var table = new PdfPTable(8);
table.HorizontalAlignment = Element.ALIGN_CENTER;

//table.HeaderRows = 1;

// the cell object
PdfPCell cell;
var f = FontFactory.GetFont("Tahoma", 11, Font.BOLD);

cell = new PdfPCell(new Phrase("Source Review", f));
cell.BorderColorLeft = new BaseColor(255, 255, 255);
cell.BorderColorRight = new iTextSharp.text.BaseColor(255, 255, 255);
table.AddCell(cell);
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我正在以两种不同的方式设置颜色,两种方式都不起作用.渲染表格时,边框始终为黑色.我怎样才能解决这个问题.

c# itext

19
推荐指数
1
解决办法
6万
查看次数

iTextSharp如何旋转/切换页面从横向到纵向

我正在使用iTextSharp将多个PDF文件合并为一个Pdf.我发现了一个代码示例2在网络上对如何完成这个任务.

它们都可以工作,没有明显的问题,因为我能够将多个PDF文件合并为一个PDF.

我所遇到的问题是我希望所有页面都在PORTRAIT中,因为一些PDF文件在LANDSCAPE中有页面,我希望它们可以旋转到PORTRAIT.我不介意他们要么颠倒,要么侧身,但他们都必须是肖像.

查看列出的示例中的代码部分:

page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
Run Code Online (Sandbox Code Playgroud)

始终将页面旋转值返回为0(零),因此代码部分

if (rotation == 90 rotation == 270)
{
    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, 
                         reader.GetPageSizeWithRotation(i).Height);
}
Run Code Online (Sandbox Code Playgroud)

永远不会被执行(如果这是应该做的,旋转页面).

那么,基于第一个代码示例链接中的代码,在将其添加到新的合并PDF文档之前,我将page = writer.GetImportedPage(reader, i)如何更改page从Landscape到Portrait 的页面布局cb.AddTemplate...

PS.确定页面是横向还是纵向我使用从SO获得的以下代码(适用于上面的代码示例):

float pageXYRatio = page.Width / page.Height;
if (XYRatio > 1f)
{
    //page is landscape
}
else
{
    //page is portrait
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

谢谢

c# itext

16
推荐指数
2
解决办法
3万
查看次数

ItextSharp尝试解析html进行pdf转换时出错

我使用ItextSharp模块将下面列出的html转换为pdf页面.

        <div style="font-size: 18pt; font-weight: bold;">
    mma<br>mmar</div><br> <br>
    <div style="font-size: 14pt;">Click to View Pricing
    </div>
    <br>
    <div>
    <table>
    <tr><td> <a href="http://www.mma.com/fci" style="color: Blue; font-size: 10pt; text-decoration: underline;"> FCI</a>:</td> 
<td><a href="http://www.mma.com/access/?pn=78211-014" style="color: Blue; font-size: 10pt; text-decoration: underline;"> 78211-014</a></td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-009" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-009</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-006" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-006</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-007" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-007</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-003" 
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-003</td></tr><tr><td></td> <td>
<a href="http://www.mma.com/access/?pn=78211-005" 
style="color: Blue; font-size: 10pt; …
Run Code Online (Sandbox Code Playgroud)

c# itext html-parsing html-agility-pack xmlworker

12
推荐指数
1
解决办法
5万
查看次数

使用iTextSharp生成PDF

我正在尝试将图像添加到现有PDF的每个页面的顶部.我尝试过使用PdfStamp,但出于某种原因,当我尝试从Chrome打印pdf时,我得到的是一个黑页.Adobe Reader也只显示原始文档.有没有人对如何使其工作有任何想法?这是代码.

public partial class MakePdf : System.Web.UI.Page
{
    public MemoryStream m = new MemoryStream();
    protected void Page_Load(object sender, EventArgs e)
    {
        Document document = new Document(PageSize.LETTER);

        Response.ContentType = "application/pdf";
        string RESULT = @"C:\Users\maitchison\Documents\Pdf\Service Report Search - 650-10-067 4114.pdf";
        PdfReader reader = new PdfReader(RESULT);
        PdfStamper stamp = new PdfStamper(reader, m);
        try
        {
            // Set ContentType and create an instance of the Writer.

            Response.ContentType = "application/pdf";
            PdfWriter writer = PdfWriter.GetInstance(document, m);
            writer.CloseStream = false;

            // Open Document

            document.Open();

            int n = …
Run Code Online (Sandbox Code Playgroud)

c# pdf asp.net barcode itext

11
推荐指数
1
解决办法
1万
查看次数

itextsharp - CSS未应用 - C#.NET

iTextSharp用来转换HTML页面PDF.我正在使用这里给出的辅助类,我也尝试使用StyleSheet.LoadTagStyle()CSS来应用CSS.但似乎没有任何效果.任何见解?

编辑

我可以添加这样的样式 -

.mystyle
{
   color: red;
   width: 400px;
}
Run Code Online (Sandbox Code Playgroud)

使用以下代码 -

StyleSheet css = new StyleSheet();
css.LoadStyle("mystyle", "color", "red");
css.LoadStyle("mystyle", "width", "400px");
Run Code Online (Sandbox Code Playgroud)

但是当我有这样复杂的风格时会发生什么?

div .myclass
{
    /*some styles*/
}

td a.hover
{
    /*some styles*/
}

td .myclass2
{
    /*some styles*/
}    
.myclass .myinnerclass
{
    /*some styles*/
}
Run Code Online (Sandbox Code Playgroud)

如何使用iTextSharp添加它?

.net css c# itext

10
推荐指数
1
解决办法
6万
查看次数

使用itextsharp xmlworker将html转换为pdf并垂直写入文本

是否有可能在xmlworker中实现自下而上的文本方向编写?我想在表格中使用它.我的代码是

     <table border=1>
     <tr>
     <td style="padding-right:18px">
          <p style="writing-mode:sideways-lr;text-align:center">First</p</td>
     <td style="padding-right:18px">
          <p style="writing-mode:sideways-lr;text-align:center">Second</p></td></tr>
     <tr><td><p style="text-align:center">1</p>  </td>
         <td><p style="text-align:center">2</p></td> 
     </tr>
        </table>
Run Code Online (Sandbox Code Playgroud)

但它从html转换为pdf后无法正常工作.文本FIRST和SECOND不是从下到上的方向.

html c# asp.net itextsharp xmlworker

10
推荐指数
1
解决办法
2万
查看次数

使用iTextSharp锁定PDF以防止编辑

我使用iTextSharp创建了一个C#程序,用于读取PDF,附加社交DRM内容,然后保存文件.如何锁定此新PDF以防止进一步编辑?

我希望用户能够在不输入密码的情况下查看文件,我不介意选择/复制操作,但我确实能够删除社交DRM.

c# pdf pdf-generation itext

9
推荐指数
1
解决办法
2万
查看次数

iTextSharp HTMLWorker.ParseToList()抛出NullReferenceException

我正在使用iTextSharp v.4来合并一大堆html文件.它工作正常,直到我需要升级到iTextSharp的第5版.

当我将流读取器(读取html文件的内容)传递到HTMLWorker对象的ParseToList方法时,问题就出现了.它抛出一个空引用异常.在调试它时,我可以访问streamReader并确认读取了正确的文件内容.

这是代码:

List<IElement> objects;
try
{
    objects = HTMLWorker.ParseToList(new StringReader(htmlString), null);
}
catch (Exception e)
{
    htmlString = "<html><head></head><body><br/><br/><h2 style='color:#FF0000'>ERROR READING FILE!</h2><h3>File Excluded From Stitched Document!</h3><br/><br/><p>There was an error while trying to read the following file:</p><p><span style='color:#FF0000'>" + fileName + "</span></p></body></html>";
    objects = HTMLWorker.ParseToList(new StringReader(htmlString), null);
}
Run Code Online (Sandbox Code Playgroud)

在catch块中,您将看到我然后使用几乎相同的代码将文本添加到pdf中以表示存在问题.这段代码工作正常.这当然让我觉得问题在于原始html字符串的内容,所以这里是字符串的内容,因为它是在传递给解析器之前的内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="cache-control" content="no-cache" />
</head>
<body style="font-family: Arial, Helvetica, sans-serif; …
Run Code Online (Sandbox Code Playgroud)

.net html c# itext xmlworker

9
推荐指数
1
解决办法
2万
查看次数

PDF使用iTextSharp进行压缩

我目前正在尝试重新压缩已经创建的pdf,我试图找到一种方法来重新压缩文档中的图像,以减小文件大小.

我一直在尝试使用DataLogics PDE和iTextSharp库来执行此操作,但我找不到对项目进行流重新压缩的方法.

我有关于循环xobjects并获取图像,然后将DPI降低到96或使用libjpeg C#implimentation来改变图像的质量但是将其恢复到pdf流似乎总是最终,内存损坏或其他一些问题.

任何样品将不胜感激.

谢谢

c# pdf itext

9
推荐指数
3
解决办法
3万
查看次数